• <i id='rEuw4'><tr id='rEuw4'><dt id='rEuw4'><q id='rEuw4'><span id='rEuw4'><b id='rEuw4'><form id='rEuw4'><ins id='rEuw4'></ins><ul id='rEuw4'></ul><sub id='rEuw4'></sub></form><legend id='rEuw4'></legend><bdo id='rEuw4'><pre id='rEuw4'><center id='rEuw4'></center></pre></bdo></b><th id='rEuw4'></th></span></q></dt></tr></i><div id='rEuw4'><tfoot id='rEuw4'></tfoot><dl id='rEuw4'><fieldset id='rEuw4'></fieldset></dl></div>

    1. <legend id='rEuw4'><style id='rEuw4'><dir id='rEuw4'><q id='rEuw4'></q></dir></style></legend>
        <bdo id='rEuw4'></bdo><ul id='rEuw4'></ul>
    2. <small id='rEuw4'></small><noframes id='rEuw4'>

      <tfoot id='rEuw4'></tfoot>

        如何在 Lucene 4 中搜索 int 字段?

        时间:2023-06-29
          <tbody id='45V27'></tbody>

        <legend id='45V27'><style id='45V27'><dir id='45V27'><q id='45V27'></q></dir></style></legend>
        1. <tfoot id='45V27'></tfoot>

              • <bdo id='45V27'></bdo><ul id='45V27'></ul>
                <i id='45V27'><tr id='45V27'><dt id='45V27'><q id='45V27'><span id='45V27'><b id='45V27'><form id='45V27'><ins id='45V27'></ins><ul id='45V27'></ul><sub id='45V27'></sub></form><legend id='45V27'></legend><bdo id='45V27'><pre id='45V27'><center id='45V27'></center></pre></bdo></b><th id='45V27'></th></span></q></dt></tr></i><div id='45V27'><tfoot id='45V27'></tfoot><dl id='45V27'><fieldset id='45V27'></fieldset></dl></div>

                <small id='45V27'></small><noframes id='45V27'>

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

                  问题描述

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

                  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 字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:基于数字字段在Lucene中对搜索结果进行排序 下一篇:lucene - 越接近标题的开头赋予更多的权重

                  相关文章

                  • <bdo id='OPoy0'></bdo><ul id='OPoy0'></ul>

                    <legend id='OPoy0'><style id='OPoy0'><dir id='OPoy0'><q id='OPoy0'></q></dir></style></legend>

                      <small id='OPoy0'></small><noframes id='OPoy0'>

                      <i id='OPoy0'><tr id='OPoy0'><dt id='OPoy0'><q id='OPoy0'><span id='OPoy0'><b id='OPoy0'><form id='OPoy0'><ins id='OPoy0'></ins><ul id='OPoy0'></ul><sub id='OPoy0'></sub></form><legend id='OPoy0'></legend><bdo id='OPoy0'><pre id='OPoy0'><center id='OPoy0'></center></pre></bdo></b><th id='OPoy0'></th></span></q></dt></tr></i><div id='OPoy0'><tfoot id='OPoy0'></tfoot><dl id='OPoy0'><fieldset id='OPoy0'></fieldset></dl></div>
                      <tfoot id='OPoy0'></tfoot>