How to use numeric range query with AND/OR using lucene search query

152 Views Asked by At

Below is the table

ID NAME HEIGHT HEIGHT_UNIT
1  aaaa 12          CM
2  bbbb 12          CM
3  ccaa 100         MM
4  dddd 130         MM
5  ddaa 140         MM

I need to write a lucene query where name like "%aa%" height ranges between (12-15) when unit is CM and between (120-150) when unit is MM

I am using QueryBuilder and hibernate-search version 5.5.5 so any help using that is much appreciated.

I tried

queryBuilder.keyword().wildcard().onFields("NAME").matching("*".concat("aa").concat("*")).createQuery(),BooleanClause.Occur.FILTER);

    booleanQueryBuilder.add(NumericRangeQuery.newDoubleRange("HEIGHT", from,to,true,true), BooleanClause.Occur.MUST);
     booleanQueryBuilder.add(queryBuilder.keyword().onField("HEIGHT_UNIT).matching("MM").createQuery(), BooleanClause.Occur.MUST);
        booleanJunction.should(booleanQueryBuilder.build());
    
     booleanQueryBuilder.add(NumericRangeQuery.newDoubleRange("HEIGHT", fromInCM,toInCM,true,true), BooleanClause.Occur.MUST);
     booleanQueryBuilder.add(queryBuilder.keyword().onField("HEIGHT_UNIT).matching("CM").createQuery(), BooleanClause.Occur.MUST);
        booleanJunction.should(booleanQueryBuilder.build());
0

There are 0 best solutions below