I am struggling to prepare predicates, as per my requirement
I need to add MUST and SHOULD depend on user operator like IS,IS_NOT,Empty…etc
On based of operator I want to and MUST and SHOULD in end,
So can you guys help me to achive it , I will try to add some part code here
in hibernate search 5 I were using as below
query = queryBuilder.keyword().onField(criterion.getField()).matching(criterionValue).createQuery(); } if (query != null) { if (criterion.getOperator().equals(SearchOperatorsEnum.IS.getName())) { booleanQuery = queryBuilder.bool() .must(query) .createQuery(); } else if (criterion.getOperator().equals(SearchOperatorsEnum.IS_NOT.getName())) { Query allDocsQuery = queryBuilder.all().createQuery(); booleanQuery = queryBuilder.bool() .must(allDocsQuery) .must(query).not() .createQuery(); } else { throw new IllegalArgumentException ("Operator " + criterion.getOperator() + " for field " + criterion.getField() + " not valid"); }
So how to replace it in HS 6 or 7
Hey,
Here are the examples in the reference documentation: Hibernate Search 7.2.1.Final: Reference Documentation ?
SearchPredicate query = ...; SearchScope<MyEntity> scope = searchSession.scope( MyEntity.class ); SearchPredicate predicate; if ( condition1 ) { predicate = scope.predicate().bool() .must( query ) .toPredicate(); } else if (condition2){ predicate = scope.predicate().bool() .must( scope.predicate().matchAll() ) .mustNot( query ) .toPredicate(); }
or add the clauses dynamically: Hibernate Search 7.2.1.Final: Reference Documentation
Can you give me example of query , which I can put in .must(query)
considering your example
SearchPredicate query = ...;
In my case can I write as below, with casting ?
note pf is SearchPredicateFactory
SearchPredicate predicate = (SearchPredicate) pf.match().field(criterion.getField()).matching(criterionValueInt);
Ignore My question, I achieved using .toPredicate()
SearchPredicate predicate = pf.match().field(criterion.getField()).matching(criterionValueInt).toPredicate();
Thank you