# Query builder in Hibernate Search 6

**URL:** <https://discourse.hibernate.org/t/query-builder-in-hibernate-search-6/5787>\
**Category:** Hibernate Search\
**Created:** [October 19, 2021, 9:16pm UTC](https://discourse.hibernate.org/t/query-builder-in-hibernate-search-6/5787 "2021-10-19T21:16:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dimitri](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/dimitri/32/513_2.png) [@Dimitri](https://discourse.hibernate.org/u/Dimitri)\
**Post date:** [October 19, 2021, 9:16pm UTC](https://discourse.hibernate.org/t/query-builder-in-hibernate-search-6/5787/1 "2021-10-19T21:16:31Z")

</div>

The Hibernate Search 6 documentation shows one example of how to dynamically generate a single bool() in the where clause:

```auto
MySearchParameters searchParameters = getSearchParameters(); 
List<Book> hits = searchSession.search( Book.class )
        .where( f -> f.bool( b -> { 
            b.must( f.matchAll() ); 
            if ( searchParameters.getGenreFilter() != null ) { 
                b.must( f.match().field( "genre" )
                        .matching( searchParameters.getGenreFilter() ) );
            }
            if ( searchParameters.getFullTextFilter() != null ) {
                b.must( f.match().fields( "title", "description" )
                        .matching( searchParameters.getFullTextFilter() ) );
            }
            if ( searchParameters.getPageCountMaxFilter() != null ) {
                b.must( f.range().field( "pageCount" )
                        .atMost( searchParameters.getPageCountMaxFilter() ) );
            }
        } ) )
        .fetchHits( 20 );

```

1 . How would I modify your above example, from a **single** bool()  
_f → f.bool_  
to conditionally nest **multiple** bools, using **if** or **for** -loops?

2 . Similarly, how can I **conditionally** decide if I want to add .sort() to the above example, when “if (searchParameters.getGenreFilter() != null)” is true?

```auto
Thanks for the assist.
```

---

<div class="post-metadata">

**Author:** ![yrodiere](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/yrodiere/32/1760_2.png) [@yrodiere](https://discourse.hibernate.org/u/yrodiere)\
**Post date:** [October 20, 2021, 6:47am UTC](https://discourse.hibernate.org/t/query-builder-in-hibernate-search-6/5787/2 "2021-10-20T06:47:07Z")

</div>

> [@Dimitri](#):
>
> 1 . How would I modify your above example, from a **single** bool()  
> _f → f.bool_  
> to conditionally nest **multiple** bools, using **if** or **for** -loops?

Boolean predicates are just like any other predicate. You can call `f.bool()` again to create another boolean predicate and nest it.

```java
MySearchParameters searchParameters = getSearchParameters(); 
List<Book> hits = searchSession.search( Book.class )
        .where( f -> f.bool( b -> { 
            b.must( f.matchAll() ); 
            List<Genre> genresFilter = searchParameters.getGenresFilter();
            if ( !genresFilter.isEmpty() ) { 
                b.must( f.bool( b2 -> {
                    for ( Genre genre : genresFilter ) {
                        b2.should( f.match().field( "genre" ).matching( genre ) );
                    } ) );
            }
        } ) )
        .fetchHits( 20 );

```

And if you have a pre-determined number of clauses, you don’t even need a lambda:

```java
MySearchParameters searchParameters = getSearchParameters(); 
List<Book> hits = searchSession.search( Book.class )
        .where( f -> f.bool( b -> { 
            b.must( f.matchAll() ); 
            GenreFilter genresFilter = searchParameters.getGenresFilter();
            if ( genresFilter != null ) { 
                b.must( f.bool()
                    .should( f.match().field( "genre" )
                                .matching( genresFilter.getGenre1() ) )
                    .should( f.match().field( "genre" )
                                .matching( genresFilter.getGenre2() ) ) );
            }
        } ) )
        .fetchHits( 20 );

```

> [@Dimitri](#):
>
> 2 . Similarly, how can I **conditionally** decide if I want to add .sort() to the above example, when “if (searchParameters.getGenreFilter() != null)” is true?

This is [documented as well](https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#search-dsl-sort-composite-lambda):

```java
MySearchParameters searchParameters = getSearchParameters(); 
List<Book> hits = searchSession.search( Book.class )
        .where( f -> f.matchAll() )
        .sort( f -> f.composite( b -> { 
            for ( MySort mySort : searchParameters.getSorts() ) { 
                switch ( mySort.getType() ) {
                    case GENRE:
                        b.add( f.field( "genre_sort" ).order( mySort.getOrder() ) );
                        break;
                    case TITLE:
                        b.add( f.field( "title_sort" ).order( mySort.getOrder() ) );
                        break;
                    case PAGE_COUNT:
                        b.add( f.field( "pageCount" ).order( mySort.getOrder() ) );
                        break;
                }
            }
        } ) )
        .fetchHits( 20 ); 

```
