Hello,
We are trying to implement nested queries like this provided example:
And was wondering if it is possible that we can add the query objects dynamically depending on the number of the query objects created.
For example if I want to search only byName or byMaxPrice or having both of them byName and byMaxPrice. Is there an easier way of adding the query objects to the search function instead of using the if/else statements?
Example:
if ( name != null ){
Query byName = MatchQuery.of(m -> m
.field("name")
.query(searchText)
)._toQuery();
}
if( price != null ) {
Query byMaxPrice = RangeQuery.of(r -> r
.field("price")
.gte(JsonData.of(maxPrice))
)._toQuery();
}
**if ( name != null && price != null) {**
SearchResponse<Product> response = esClient.search(s -> s
.index("products")
.query(q -> q
.bool(b -> b
**.must(byName) **
** .must(byMaxPrice)**
)
),
Product.class
);
**} else if ( name != null && price == null ) {**
SearchResponse<Product> response = esClient.search(s -> s
.index("products")
.query(q -> q
.bool(b -> b
**.must(byName)**
)
),
Product.class
);
**} else if ( name == null && price != null ) {**
SearchResponse<Product> response = esClient.search(s -> s
.index("products")
.query(q -> q
.bool(b -> b
**.must(byMaxPrice)**
)
),
Product.class
);
}