How do I make the QueryParser in Lucene handle numeric ranges?

前端 未结 5 1115
闹比i
闹比i 2020-12-16 05:43
new QueryParser(.... ).parse (somequery);

it works only for string indexed fields. Say i have a field called count where count is a integer field (

5条回答
  •  攒了一身酷
    2020-12-16 06:20

    In Lucene 6, the protected method QueryParser#getRangeQuery still exists with the argument list (String fieldName, String low, String high, boolean startInclusive, boolean endInclusive), and overriding it to interpret the range as a numeric range is indeed possible, as long as that information is indexed using one of the new Point fields.

    When indexing your field:

    document.add(new FloatPoint("_point_count", value)); // index for efficient range based retrieval
    document.add(new StoredField("count", value)); // if you need to store the value itself
    

    At your custom query parser (extending queryparser.classic.QueryParser), override the method with something like this:

    @Override
    protected Query getRangeQuery(String field, String low, String high, boolean startInclusive, boolean endInclusive) throws ParseException
    {
        if («isNumericField»(field)) // context dependent
        {
            final String pointField = "_point_" + field;
            return FloatPoint.newRangeQuery(pointField,
                    Float.parseFloat(low),
                    Float.parseFloat(high));
        }
    
        return super.getRangeQuery(field, low, high, startInclusive, endInclusive);
    }
    

提交回复
热议问题