获取Lucene中匹配项的位置

获取Lucene中匹配项的位置,lucene,Lucene,当索引字段未存储时,是否可以找到具有匹配项的单词的位置 例如: 注1:我知道文本可以用Lucene突出显示,但我想知道单词的位置 注2:该字段未设置为由Lucene存储**我这样做并非出于实际目的-只是为了给出一个伪代码和指针,您可以使用它进行实验,以获得正确的解决方案 另外,您还没有指定您的Lucene版本,我将Lucene 6.0.0与Java一起使用 1.编制索引时,为所需位置的特定字段设置这两个布尔值。如果索引已经存储了这些信息,Lucene将能够提供这些数据 FieldType txt

当索引字段未存储时,是否可以找到具有匹配项的单词的位置

例如: 注1:我知道文本可以用Lucene突出显示,但我想知道单词的位置


注2:该字段未设置为由Lucene存储**

我这样做并非出于实际目的-只是为了给出一个伪代码和指针,您可以使用它进行实验,以获得正确的解决方案

另外,您还没有指定您的Lucene版本,我将Lucene 6.0.0与Java一起使用

1.编制索引时,为所需位置的特定字段设置这两个布尔值。如果索引已经存储了这些信息,Lucene将能够提供这些数据

FieldType txtFieldType = new FieldType(
            TextField.TYPE_NOT_STORED);

txtFieldType.setStoreTermVectors(true);

txtFieldType.setStoreTermVectorPositions(true);
2.在您的搜索者处,您需要使用以下术语:

`Terms terms = searcher.getIndexReader().getTermVector(hit.doc, "TEXT_FIELD");`


            if(terms.hasPositions()){
                TermsEnum termsEnum = terms.iterator();
                PostingsEnum postings = null;
                while(termsEnum.next() != null){
                    postings  = termsEnum.postings(postings ,PostingsEnum.ALL);
                    while(postings.nextDoc() != PostingsEnum.NO_MORE_DOCS){
                        System.out.println(postings.nextPosition());
                    }
您需要自己进行一些分析,以获得所需的数据,但首先需要保存元数据,如第1点所述

                }
            }
searcher是IndexSearcher实例,hit.doc是文档id,hit是ScoreDoc

                }
            }