Exclude null values from searching

Hi.

I have 2 cases with the same property.

  1. property value = null.
    So, annotate the property with indexNullAs
@Indexed
public class Object {
    @GenericField(sortable = Sortable.YES, indexNullAs = "999999999")
	private BigDecimal property;
}

pf.match().field(property).matching(999999999).toPredicate()

this works fine.

  1. property value > 1000

pf.range().field(property).greaterThan(new BigDecimal(1000)).toPredicate();

In the 2nd case, get also null values(due to indexNullAs = “999999999”)
Could i exclude the null values ?

Custom solution is to use greatherThan 1000 and lessThan 999999999 or range between(1000, 999999999).

Thank you.

Hi,

No you cannot. If you use indexNullAs, the null value gets replaced at indexing time and all knowledge of the value being null is lost.

My recommendation would be not to use indexNullAs to begin with. There are better alternatives, such as the exists predicate (which will only work if you don’t use indexNullAs).

1 Like

and how can i use the combination exists and match for same property ?
i need both
pf.exists().field(property).matching(token).toPredicate();
and
pf.match().field(property).matching(token).toPredicate();

Use a bool predicate to emulate a logical OR: Hibernate Search 6.1.7.Final: Reference Documentation

There’s also AND, NOT, etc. I’m sure you’ll find what you need.

1 Like

Thank you.

With bool predicate, all my cases work fine !

Thank you again ! :slight_smile:

1 Like