Java 使用lucene获取文档中的单词位置

Java 使用lucene获取文档中的单词位置,java,lucene,Java,Lucene,我想知道如何使用Lucene获取文档中单词的位置 我已经生成了索引文件,我想从索引中提取一些信息,如索引词、该词在文档中的位置等 我创建了这样一个读者: public void readIndex(Directory indexDir) throws IOException { IndexReader ir = IndexReader.open(indexDir); Fields fields = MultiFields.getFields(ir); System.ou

我想知道如何使用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()来读取术语是否有位置,其中返回true
但是不知道哪个函数可以为我提供位置???

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


:获取当前学期的DocsAndPositionsEnum。当枚举未定位时,不要调用此函数。如果未对位置进行索引,此方法将返回null。

Lucene 4.2最新版本“在尝试检索位置信息之前,您必须首先确保在启用位置信息的情况下进行索引”如何简单:“如果未对位置进行索引,上述方法将返回null。”。我不熟悉最新版本,但在以前的版本中,我们向字段构造函数提供了“Field.TermVector with_POSITIONS”,或者使用Luke检查Lucene索引是否有位置信息。在Lucene 4.x中,您将把a传递给
字段
ctor,您已经在其上设置了
FieldType.setStoreTermVectors(true);
FieldType.setStoreTermVectorPositions(true);
Field.TermVector
已被弃用)。@phani:Luke开发已经停止。它无法识别Apache 4.2我很抱歉模棱两可,我提到这两种方法适用于以前的版本。