1

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());
  • Where are you stuck, specifically? Can you [edit] your question to show us what you have already tried, and what happened when you tried? For example, from your question we can't tell if you already know how to write a numeric range query, or a wildcard query, or a query using `and` and `or` conditions, and so on. – andrewJames Aug 02 '21 at 15:46
  • Also, welcome to Stack Overflow - and don't forget to take the [tour] and have a look at [ask] for additional guidance. – andrewJames Aug 02 '21 at 15:46
  • Your sample code appears to be using Hibernate Search, rather than directly using Lucene. If that is the case, you should tag your question as such (`hibernate-search`), and mention the Hibernate Search version you are using. – andrewJames Aug 02 '21 at 21:13

0 Answers0