【问题标题】:need to display all the terms for a given index需要显示给定索引的所有术语
【发布时间】:2014-10-04 04:47:17
【问题描述】:

我需要显示给定 Lucene 索引的所有术语。

    public void addDocuments(IndexWriter indexWriter) throws IOException {
    Document doc1 = new Document();
    doc1.add(new TextField("title", "harrypotter", Field.Store.YES));
    indexWriter.addDocument(doc1);

    Document doc2 = new Document();
    doc2.add(new TextField("title", "luceneinaction", Field.Store.YES));
    indexWriter.addDocument(doc2);

    Document doc3 = new Document();
    doc3.add(new TextField("title", "harrypotter", Field.Store.YES));
    indexWriter.addDocument(doc3);
}

我正在尝试这个:

    Fields fields = MultiFields.getFields(reader);
    Terms terms = fields.terms("title");
    TermsEnum iterator = terms.iterator(null);          
    BytesRef byteRef = null;
    while((byteRef = iterator.next()) != null) {
        System.out.println(byteRef.utf8ToString());
    }

然而,这给了我唯一的术语:

harrypotter
luceneinaction

有没有得到所有的条款(也是重复的)?还是术语总是唯一的?

谢谢。

PS:Lucene版本是4.0。

【问题讨论】:

  • 这似乎不是主要问题。你到底想做什么?

标签: java search lucene full-text-search


【解决方案1】:

它会给你独特的条款。但是,您可以通过以下方式获取包含该术语的文档的计数:

while ((byteRef = iterator.next()) != null) {
    System.out.println(byteRef.utf8ToString() + " - " + iterator.docFreq());
}

【讨论】:

    【解决方案2】:

    Lucene 是一个inverted index,因此它存储对术语的引用,如下所示:

    harrypotter -> doc1, doc3
    luceneinaction -> doc2
    

    如上所示,每个术语都指向文档。

    如果您需要获取每个文档的术语,请通过所需的分析器分别运行它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-13
      • 1970-01-01
      • 2015-03-30
      • 2020-09-01
      • 2012-02-22
      • 1970-01-01
      • 2016-04-27
      • 1970-01-01
      相关资源
      最近更新 更多