允许在Lucene 4中对数字字段进行范围查询的最简单方法

时间:2014-06-23 09:16:57

标签: java lucene

我正在重新访问最初为Lucene 3.0编写的代码(然后我更新了它,因此它适用于4.0但尚未利用所有4.0改进)。

对于我希望能够进行范围查询的数字字段,我将字段添加到文档中,如下所示:

BytesRef bytes = new BytesRef(NumericUtils.BUF_SIZE_INT);
NumericUtils.intToPrefixCoded(value, 0, bytes);
doc.add(new Field(fieldname, bytes.utf8ToString(),
    new FieldType(StringField.TYPE_NOT_STORED)));

然后我必须有一个自定义查询解析器覆盖方法newTermQuery()和新的RangeQuery()

public class ReleaseQueryParser extends MultiFieldQueryParser
{

    public ReleaseQueryParser(String[] strings, Analyzer a)
    {
        super(LuceneVersion.LUCENE_VERSION, strings, a);
    }

    protected Query newTermQuery(Term term)
    {
        if (
                (term.field().equals(ReleaseIndexField.NUM_TRACKS.getName())) ||
                        (term.field().equals(ReleaseIndexField.NUM_TRACKS_MEDIUM.getName())) ||
                        (term.field().equals(ReleaseIndexField.NUM_MEDIUMS.getName()))
                )
        {
            try
            {

                int number = Integer.parseInt(term.text());
                BytesRef bytes = new BytesRef(NumericUtils.BUF_SIZE_INT);
                NumericUtils.intToPrefixCoded(number, 0, bytes);
                TermQuery tq = new TermQuery(new Term(term.field(), bytes.utf8ToString()));
                return tq;
            }
            catch (NumberFormatException nfe)
            {
                //If not provided numeric argument just leave as is, won't give matches
                return super.newTermQuery(term);
            }
        }
        else
        {
            return super.newTermQuery(term);

        }
    }

    @Override
    public Query newRangeQuery(String field,
                               String part1,
                               String part2,
                               boolean startInclusive,
                               boolean endInclusive)
    {
        if (
                (field.equals(ReleaseIndexField.NUM_TRACKS.getName())) ||
                        (field.equals(ReleaseIndexField.NUM_TRACKS_MEDIUM.getName())) ||
                        (field.equals(ReleaseIndexField.NUM_MEDIUMS.getName()))
                )
        {
            BytesRef bytes1 = new BytesRef(NumericUtils.BUF_SIZE_INT);
            BytesRef bytes2 = new BytesRef(NumericUtils.BUF_SIZE_INT);
            NumericUtils.intToPrefixCoded(Integer.parseInt(part1), 0, bytes1);
            NumericUtils.intToPrefixCoded(Integer.parseInt(part2), 0, bytes2);
            part1 = bytes1.utf8ToString();
            part2 = bytes2.utf8ToString();
        }
        TermRangeQuery query = (TermRangeQuery)
                super.newRangeQuery(field, part1, part2, startInclusive, endInclusive);
        return query;

    }
}

我一直认为这段代码比较笨重,并且想知道现在是否可以简化一下?

更新

尝试使用IntField如下

doc.add(new IntField(field.getName(),value,new FieldType(StringField.TYPE_NOT_STORED)));

编译好了,但我的索引构建测试方法

Fields fields = MultiFields.getFields(ir);
Terms terms = fields.terms(field.getName());
TermsEnum termsEnum = terms.iterator(null);
termsEnum.next();
assertEquals(value, NumericUtils.prefixCodedToInt(termsEnum.term()));

在terms.iterator()行上出现NullPointerException失败。

已更改为

doc.add(new IntField(field.getName(), value, new FieldType(IntField.TYPE_NOT_STORED)));

并且有效,但我很惊讶那条线

NumericUtils.prefixCodedToInt(termsEnum.term())

仍然有效,我猜IntField只是我最初拥有的ByteRef代码的包装。

然后重写QueryParser如下

public class ReleaseQueryParser extends MultiFieldQueryParser
{

    public ReleaseQueryParser(String[] strings, Analyzer a)
    {
        super(LuceneVersion.LUCENE_VERSION, strings, a);
    }

    protected Query newTermQuery(Term term)
    {
        if (
                (term.field().equals(ReleaseIndexField.NUM_TRACKS.getName())) ||
                        (term.field().equals(ReleaseIndexField.NUM_TRACKS_MEDIUM.getName())) ||
                        (term.field().equals(ReleaseIndexField.NUM_MEDIUMS.getName()))
                )
        {
            return NumericRangeQuery.newIntRange(term.field(), Integer.parseInt(term.text()), Integer.parseInt(term.text()), true, true);
        }
        else
        {
            return super.newTermQuery(term);

        }
    }

    @Override
    public Query newRangeQuery(String field,
                               String part1,
                               String part2,
                               boolean startInclusive,
                               boolean endInclusive)
    {
        if (
                (field.equals(ReleaseIndexField.NUM_TRACKS.getName())) ||
                        (field.equals(ReleaseIndexField.NUM_TRACKS_MEDIUM.getName())) ||
                        (field.equals(ReleaseIndexField.NUM_MEDIUMS.getName()))
                )
        {
            return NumericRangeQuery.newIntRange(field, Integer.parseInt(part1), Integer.parseInt(part2),startInclusive, endInclusive);
        }
        else
        {
            return super.newRangeQuery(field, part1, part2, startInclusive, endInclusive);
        }
    }
}

这很有用,我真的不想在newTermQuery(Term term)中使用范围查询,但我看不到更简单的方法。

1 个答案:

答案 0 :(得分:0)

使用lucene提供的IntFieldLongField类。然后,您可以使用指定类型的标准NumericRangeQuery

查询索引