如何在Lucene 4中搜索int字段? [英] How to search an int field in Lucene 4?

查看:101
本文介绍了如何在Lucene 4中搜索int字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现文档索引(对应于DB行的rougly),其中一个字段是整数。我将它们添加到索引中:

I am trying to implement an index of documents (rougly corresponding to DB rows), where one of the fields is an integer. I'm adding them to index like:

Document doc = new Document();
doc.add(new StringField("ticket_number", rs.getString("ticket_number"),
        Field.Store.YES));
doc.add(new IntField("ticket_id", rs.getInt("ticket_id"),
        Field.Store.YES));
doc.add(new StringField("id_s", rs.getString("ticket_id"),
        Field.Store.YES));
w.addDocument(doc);

好像我无法查询 ticket_id 字段,而 id_s 工作得很好。

It seems I can't query the ticket_id field at all, while id_s works just fine.

其中一个文件是(我添加了空格以便于阅读):

One of the documents is (I added whitespace for readability):

Document<
    stored,indexed,tokenized,omitNorms,indexOptions=DOCS_ONLY<ticket_number:230114W> 
    stored<ticket_id:152> 
    stored,indexed,tokenized,omitNorms,indexOptions=DOCS_ONLY<id_s:152>>

所以我的int字段已存储,但未编入索引。此查询按预期工作: id_s:152 ,而此查询永远不会返回任何内容: ticket_id:152

So my int field is stored, but not indexed. This query works as expected: id_s:152, while this one never returns anything: ticket_id:152.

我做错了什么?如何将这样的字段添加到索引并使其可搜索?

What am I doing wrong? How can I add such a field to the index and make it searchable?

推荐答案

以下对我有效:

    RAMDirectory idx = new RAMDirectory();
    IndexWriter writer = new IndexWriter(
            idx,
            new IndexWriterConfig(Version.LUCENE_40, new ClassicAnalyzer(Version.LUCENE_40))
    );
    Document document = new Document();
    document.add(new StringField("ticket_number", "t123", Field.Store.YES));
    document.add(new IntField("ticket_id", 234, Field.Store.YES));
    document.add(new StringField("id_s", "234", Field.Store.YES));
    writer.addDocument(document);
    writer.commit();

    IndexReader reader = DirectoryReader.open(idx);
    IndexSearcher searcher = new IndexSearcher(reader);

    Query q1 = new TermQuery(new Term("id_s", "234"));
    TopDocs td1 = searcher.search(q1, 1);
    System.out.println(td1.totalHits);  // prints "1"

    Query q2 = NumericRangeQuery.newIntRange("ticket_id", 1, 234, 234, true, true);
    TopDocs td2 = searcher.search(q2, 1);
    System.out.println(td2.totalHits);  // prints "1"

正如femtoRgon所指出的,对于数值(长,日,浮点数,等)你需要 NumericRangeQuery 并指定精度。否则Lucene不知道你想如何定义相似性。

As femtoRgon pointed out, for numeric values (longs, dates, floats, etc.) you need to have NumericRangeQuery and specify precision. Otherwise Lucene has no idea how do you want to define similarity.

这篇关于如何在Lucene 4中搜索int字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆