Optional fields match

Hello,
My search offers users the ability to add optional fields to their search to refine their query. However, I can’t find any documentation on this.

I’m looking to do something like this:

searchSession.search(Entity.class).where(entity -> {
	if(fieldValue1Exists){
		entity.match().field("fieldName1").matching(fieldValue1);
	}
	if(fieldValue2Exists){
		entity.match().field("fieldName2").matching(fieldValue2);
	}
})

In essence, I want to be able to build the query inside the where clause. How should I do that?

Hello,

What you are looking for is in this example: Adding clauses dynamically with the lambda syntax:

searchSession.search(Entity.class).where((entity, root) -> {
	if(fieldValue1Exists){
		root.add(entity.match().field("fieldName1").matching(fieldValue1));
	}
	if(fieldValue2Exists){
		root.add(entity.match().field("fieldName2").matching(fieldValue2));
	}
})
1 Like

you are amazing! :heart::heart::heart::heart::heart::heart:

1 Like