【问题标题】:Get word position In document with lucene使用 lucene 在文档中获取单词位置
【发布时间】:2013-04-03 23:04:49
【问题描述】:

我想知道如何使用 Lucene 获取文档中单词的位置 我已经生成了索引文件,我想从索引中提取一些信息,例如索引词、词在文档中的位置等

我创建了一个这样的阅读器:

public void readIndex(Directory indexDir) throws IOException {
    IndexReader ir = IndexReader.open(indexDir);
    Fields fields =  MultiFields.getFields(ir);
    System.out.println("TOTAL DOCUMENTS : " + ir.numDocs());

    for(String field : fields) {
        Terms terms = fields.terms(field);
        TermsEnum termsEnum = terms.iterator(null);
        BytesRef text;
        while((text = termsEnum.next()) != null) {
            System.out.println("text = " + text.utf8ToString() + "\nfrequency = " + termsEnum.totalTermFreq());
        }
    }
}

我将作者修改为:

org.apache.lucene.document.Document doc = new org.apache.lucene.document.Document();

                FieldType fieldType = new FieldType();
                fieldType.setStoreTermVectors(true);
                fieldType.setStoreTermVectorPositions(true);
                fieldType.setIndexed(true);

                doc.add(new Field("word", new BufferedReader(new InputStreamReader(fis, "UTF-8")), fieldType));

我试图通过调用 terms.hasPositions() 来读取该术语是否具有位置,它 return true 但是不知道哪个函数可以给我位置??

【问题讨论】:

  • Lucene 4.2 最新版本

标签: java lucene


【解决方案1】:

在您尝试检索位置信息之前,您必须首先确保在启用位置信息的情况下进行索引。

TermsEnum.DocsAndPositionsEnum :获取当前任期的 DocsAndPositionsEnum。当枚举未定位时不要调用它。如果没有索引位置,此方法将返回 null。

【讨论】:

  • “在您尝试检索位置信息之前,您必须首先确保在启用位置信息的情况下进行索引”如何?
  • 简单:“如果没有索引位置,上述方法将返回 null。”。我对最新版本不熟悉,但在之前的版本中,我们为Field构造函数提供了“Field.TermVector WITH_POSITIONS”,或者使用Luke检查Lucene索引是否有位置信息。
  • 在 Lucene 4.x 中,您可以将 FieldType 传递给已设置 FieldType.setStoreTermVectors(true);FieldType.setStoreTermVectorPositions(true);Field ctor(Field.TermVector 已弃用)。跨度>
  • @phani : Luke 的开发已经停止。它不识别 Apache 4.2
  • 我很抱歉含糊不清,我提到这两种方式是针对以前的版本的。
猜你喜欢
  • 1970-01-01
  • 2010-11-20
  • 1970-01-01
  • 2016-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
  • 2010-10-14
相关资源
最近更新 更多