# Using exists on list of child records

**URL:** <https://discourse.hibernate.org/t/using-exists-on-list-of-child-records/6013>\
**Category:** Hibernate Search\
**Created:** [January 31, 2022, 3:13pm UTC](https://discourse.hibernate.org/t/using-exists-on-list-of-child-records/6013 "2022-01-31T15:13:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![buncieboy](https://avatars.discourse-cdn.com/v4/letter/b/dec6dc/32.png) [@buncieboy](https://discourse.hibernate.org/u/buncieboy)\
**Post date:** [January 31, 2022, 3:13pm UTC](https://discourse.hibernate.org/t/using-exists-on-list-of-child-records/6013/1 "2022-01-31T15:13:14Z")

</div>

Currently migrating from HS5 and trying to move away from index null as. We have followed the documentations example to use the exists predicate with negation as a null check, however this causes issues when the field we are checking is in a list of child objects.

If parent has two child records with a field called “name”, one of which is null and one of which is not null the above method will not match the parent record as parent.child.name exists, where as for our use case we would want it to.

I am struggling to see a way we could make this work with the existing boolean logic. Is there a way to make this work, or should we revert back to using index null as?

---

<div class="post-metadata">

**Author:** ![yrodiere](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/yrodiere/32/1760_2.png) [@yrodiere](https://discourse.hibernate.org/u/yrodiere)\
**Post date:** [January 31, 2022, 3:26pm UTC](https://discourse.hibernate.org/t/using-exists-on-list-of-child-records/6013/2 "2022-01-31T15:26:32Z")

</div>

I can see two ways.

## Using nested object fields

Make the “child” object field (the one holding the name) [nested](https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#mapper-orm-indexedembedded-structure) and wrap your `exists()` predicate in a [nested predicate](https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#search-dsl-predicate-nested).

E.g. something like this:

```java
@Indexed
public class Parent {

    @IndexedEmbedded(structure = NESTED)
    List<Child> child;

}

public class Child {

    @FullTextField
    String name;

}

public List<Parent> search(...) {
    return searchSession.search( Parent.class )
            .where( f -> f.nested().objectField( "child" ) 
                    .nest( f.bool()
                            .mustNot( f.exists().field( "child.name" ) ) 
                    ) )
            .fetchHits( 20 ); 
}

```

## Use a custom field and bridge

Perhaps simpler, define an additional boolean field next to your `name` field, and use a `match` predicate on that field (you don’t need a negation then):

```java
@Indexed
public class Parent {

    @IndexedEmbedded
    List<Child> child;

}

public class Child {

    @FullTextField
    @GenericField(name = "is_unnamed", valueBridge = @ValueBridgeRef(type = IsEmptyOrNullBridge.class))
    String name;

}

public class IsEmptyOrNullBridge implements ValueBridge<String, Boolean> {

	@Override
	public Boolean toIndexedValue(String value, ValueBridgeToIndexedValueContext context) {
		return value == null || value.isEmpty();
	}

}

public List<Parent> search(...) {
    return searchSession.search( Parent.class )
            .where( f -> f.match().field( "child.is_unnamed" ).matching( true ) )
            .fetchHits( 20 ); 
}

```

---

<div class="post-metadata">

**Author:** ![buncieboy](https://avatars.discourse-cdn.com/v4/letter/b/dec6dc/32.png) [@buncieboy](https://discourse.hibernate.org/u/buncieboy)\
**Post date:** [January 31, 2022, 3:31pm UTC](https://discourse.hibernate.org/t/using-exists-on-list-of-child-records/6013/3 "2022-01-31T15:31:10Z")

</div>

Thanks again for the quick help, I’ll give these a go 🙂

---

<div class="post-metadata">

**Author:** ![yrodiere](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/yrodiere/32/1760_2.png) [@yrodiere](https://discourse.hibernate.org/u/yrodiere)\
**Post date:** [January 31, 2022, 3:36pm UTC](https://discourse.hibernate.org/t/using-exists-on-list-of-child-records/6013/4 "2022-01-31T15:36:22Z")

</div>

No problem.

That being said, you can still use `indexNullAs` if you want.

It’s just that now, you will have to handle the null token explicitly, i.e. decide of a string to index instead of `null` (e.g. “NO\_NAME”) and search for that string instead of null in your search query.  
That’s the main change since 5.x: now it’s clear that `indexNullAs` is just a hack, and it’s clearer that it has limitations (what if someone is named “NO\_NAME”? 🙂 ). But if it works for you, then it’s fine to use it.

EDIT: Ah, and also, `indexNullAs` is not available on full-text fields, due to underlying technical constraints. So, if your field is a `@FullTextField`, then indeed, you should look for alternatives.
