0

following are two fields defined in the entity

@Column(name = "min_price")
@Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
@NumericField
@FieldBridge(impl = IntegerBridge.class)
private Integer minPrice;

@Column(name = "max_price")
@Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
@NumericField
@FieldBridge(impl = IntegerBridge.class)
private Integer maxPrice;

full text query is generated using entityManager as following

BooleanJunction priceBooleanJunction = queryBuilder.bool();
    //Building range query for price
    if (minPrice != null) {
        Query rangeQueryMinPrice = queryBuilder.range().onField("minPrice").above(minPrice).createQuery();
        priceBooleanJunction.must(rangeQueryMinPrice);
    }
    if (maxPrice != null) {
        Query rangeQueryMaxPrice = queryBuilder.range().onField("maxPrice").below(maxPrice).createQuery();
        priceBooleanJunction.must(rangeQueryMaxPrice);
    }

    BooleanJunction combainedBoolean = queryBuilder.bool();

    if (!priceBooleanJunction.isEmpty()) {
        combainedBoolean.must(priceBooleanJunction.createQuery());
    }

    Query searchQuery = combainedBoolean.createQuery();
    FullTextQuery query = fullTextEntityManager.createFullTextQuery(searchQuery, ServiceProvider.class);

    LOGGER.info(query.toString());

the last log prints the following for 40, 90 input

FullTextQueryImpl(+minPrice:[40 TO *] +maxPrice:[* TO 90])

database contains the values (min,max) as (50, 80) and (80, 90)

still result is empty.

Dax Joshi
  • 143
  • 11

2 Answers2

3

The IntegerBridge you are using is using string encoding, but you want to make sure to use numeric encoding when using a range query. There is no need to add @FieldBridge in your case.

Also the behavior will depend on which version of Search you are using. Pre version 5 you needed to add @NumericField, but as of Hibernate Search 5, numbers will be automatically indexed numerically, in which case @FieldBridge as well as @NumericField is not required anymore.

Hardy
  • 18,659
  • 3
  • 49
  • 65
0

Are minPrice and maxPrice needed in your entity ? Your article has a minPrice and a maxPrice ? If you do it for full text search, you don't need it.

try to use IntegerNumericFieldBridge instead of IntegerBridge like this :

@Field @FieldBridge(impl=IntegerNumericFieldBridge.class)
private Integer price;

After that use NumericRangeQuery instead of hibernate search range query. Like this :

NumericRangeQuery<Integer> integerQuery = NumericRangeQuery.newIntRange("price", minRangePrice, maxRangePrice,  true, true);

If you have many queries, you will not be able to use hibernate search bool() method, use lucene "BooleanQuery" instead. Like this :

BooleanQuery luceneBooleanQuery = new BooleanQuery();
luceneBooleanQuery.add(integerQuery, BooleanClause.Occur.MUST); // MUST or SHOULD
luceneBooleanQuery.add(anotherQuery, BooleanClause.Occur.MUST); 

You can mix lucene queries like NumericRangeQuery and queries built by the QueryBuilder of hibernate search.

Then, you can do this :

FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(luceneBooleanQuery, YourClass.class);

It should work.

Bilal BBB
  • 1,154
  • 13
  • 20