Gatschet
Gatschet

Reputation: 1537

Hibernate Search using FlaotBridge for range queries

Im using hibernate search version 5.10.x and i would like to search over a flaot ranges.

My field is:

@Field(index = Index.YES, analyze = Analyze.NO, store = Store.YES)
@FieldBridge(impl = FloatBridge.class)
private Float minusPunkte = -1f;

After generate lucene index, the generate index is like expected a value between -1.0 and 150.0

In Luke:

enter image description here

To search over a range of values i use:

        Float lowerTermFloat = null,
                upperTermFloat = null;
        try {
            lowerTermFloat = StringUtils.isEmpty(lowerTerm) ? null : Float.parseFloat(lowerTerm);
            upperTermFloat = StringUtils.isEmpty(upperTerm) ? null : Float.parseFloat(upperTerm);
        } catch (NumberFormatException e) {
            throw new HibernateSearcheParseException();
        }
        return NumericRangeQuery.newFloatRange(path, 4, lowerTermFloat, upperTermFloat, includeLower, includeUpper);

This generate search string for lucene:

+totalPunkte:[120.0 TO *}

if i would like to have all entries greater than 120

But in the result there are also values smaller than 120:

enter image description here

It looks, that its searching over string values, and not over float values!

But why does the FloatBridge not generate comparebles string values from float value? What do i wrong? Do i need my own Bridge for comparing float values?

Upvotes: 0

Views: 110

Answers (1)

yrodiere
yrodiere

Reputation: 9977

FloatBridge is a legacy bridge, from way back when Lucene didn't have any support for numeric values. See its javadoc:

/**
 * Bridge a {@link Float} to a {@link String}.
 *
 * @see NumericFieldBridge#FLOAT_FIELD_BRIDGE
 * @author Emmanuel Bernard
 */

To index floats as floats, and not as string, simply don't specify a bridge at all. Hibernate Search will do the right thing by default.

So, your mapping becomes:

@Field(index = Index.YES, analyze = Analyze.NO, store = Store.YES)
private Float minusPunkte = -1f;

Upvotes: 1

Related Questions