# Prepare Qyuery using Predicates

**URL:** <https://discourse.hibernate.org/t/prepare-qyuery-using-predicates/10529>\
**Category:** Hibernate Search\
**Created:** [October 29, 2024, 6:34am UTC](https://discourse.hibernate.org/t/prepare-qyuery-using-predicates/10529 "2024-10-29T06:34:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![shrinivasva](https://avatars.discourse-cdn.com/v4/letter/s/a87d85/32.png) [@shrinivasva](https://discourse.hibernate.org/u/shrinivasva)\
**Post date:** [October 29, 2024, 6:34am UTC](https://discourse.hibernate.org/t/prepare-qyuery-using-predicates/10529/1 "2024-10-29T06:34:54Z")

</div>

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

```auto
		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

---

<div class="post-metadata">

**Author:** ![mbekhta](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/mbekhta/32/2044_2.png) [@mbekhta](https://discourse.hibernate.org/u/mbekhta)\
**Post date:** [October 29, 2024, 8:07am UTC](https://discourse.hibernate.org/t/prepare-qyuery-using-predicates/10529/2 "2024-10-29T08:07:19Z")

</div>

Hey,

Here are the examples in the reference documentation: [Hibernate Search 7.2.1.Final: Reference Documentation](https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#search-dsl-predicate-boolean) ?

```java
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](https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#search-dsl-predicate-boolean-lambda)

---

<div class="post-metadata">

**Author:** ![shrinivasva](https://avatars.discourse-cdn.com/v4/letter/s/a87d85/32.png) [@shrinivasva](https://discourse.hibernate.org/u/shrinivasva)\
**Post date:** [October 29, 2024, 10:19am UTC](https://discourse.hibernate.org/t/prepare-qyuery-using-predicates/10529/3 "2024-10-29T10:19:52Z")

</div>

Can you give me example of query , which I can put in .must(query)  
considering your example

```auto
SearchPredicate query = ...;

```

In my case can I write as below, with casting ?

note pf is SearchPredicateFactory

```auto
SearchPredicate predicate = (SearchPredicate) pf.match().field(criterion.getField()).matching(criterionValueInt);

```

---

<div class="post-metadata">

**Author:** ![shrinivasva](https://avatars.discourse-cdn.com/v4/letter/s/a87d85/32.png) [@shrinivasva](https://discourse.hibernate.org/u/shrinivasva)\
**Post date:** [October 29, 2024, 10:25am UTC](https://discourse.hibernate.org/t/prepare-qyuery-using-predicates/10529/4 "2024-10-29T10:25:23Z")

</div>

Ignore My question, I achieved using .toPredicate()

```auto
SearchPredicate predicate = pf.match().field(criterion.getField()).matching(criterionValueInt).toPredicate();

```

Thank you
