我正在升级到Solr 4.1,并且无法使用新API检索位置和偏移信息.我的索引包含一个文档,其中一个字段包含字符串' 一只快速的棕色狐狸跳过一只懒狗 '.我正在查询我的索引'one'并尝试检索对应于'one'的位置和偏移量.
这是代码片段
Terms terms=reader.getTermVector(docId, fieldName);
TermsEnum termsEnum= terms.iterator(TermsEnum.EMPTY);
BytesRef term;
while((term=termsEnum.next())!=null){
String docTerm = term.utf8ToString();
DocsAndPositionsEnum docPosEnum = termsEnum.docsAndPositions(null, null, DocsAndPositionsEnum.FLAG_OFFSETS);
//Check if the current term is the same as the query term and if so
//retrieve all positions (can be multiple occurrences of a term in a field) corresponding to the term
if (queryTerms.contains(docTerm)) {
int position;
while((position=docPosEnum.nextPosition())!=-1){
int start=docPosEnum.startOffset();
int end=docPosEnum.endOffset();
//Store start, end and position in an a list
}
}
}
Run Code Online (Sandbox Code Playgroud)
内部while循环不正确.任何关于如何遍历DocsAndPositionsEnum中所有位置的指针都将非常感激.
这对我有用
Terms terms=reader.getTermVector(docId, fieldName);
TermsEnum termsEnum= terms.iterator(TermsEnum.EMPTY);
BytesRef term;
while((term=termsEnum.next())!=null){
String docTerm = term.utf8ToString();
//Check if the current term is the same as the query term and if so
//retrieve all positions (can be multiple occurrences of a term in a field) corresponding to the term
if (queryTerms.contains(docTerm)) {
DocsAndPositionsEnum docPosEnum = termsEnum.docsAndPositions(null, null, DocsAndPositionsEnum.FLAG_OFFSETS);
docPosEnum.nextDoc();
//Retrieve the term frequency in the current document
int freq=docPosEnum.freq();
for(int i=0; i<freq; i++){
int position=docPosEnum.nextPosition();
int start=docPosEnum.startOffset();
int end=docPosEnum.endOffset();
//Store start, end and position in a list
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1331 次 |
| 最近记录: |