我升级到Solr 4.1,无法使用新API检索位置和偏移量信息。我的索引包含一个文件,其中一个字段包含字符串'一个快速的棕色狐狸跳过一只懒狗'。我是查询我的索引'one'并尝试检索对应于'one'的位置和偏移量。从DocsAndPositionsEnum获取所有期限头寸
这里是代码片段
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
}
}
}
内的while循环不正确。任何有关如何遍历DocsAndPositionsEnum中所有位置的指针都将不胜感激。
你是正确的,我会需要遍历到下一个文档什么工作。但是,您发布的解决方案将无法工作。内部while将导致无限循环,因为docPosEnumEnum.nextPosition()仅当位置未被编入索引时才返回-1。并且我已对索引位置进行了索引。另外,请注意我有docID(代码片段的第1行),这使得不需要外部while循环。 – 2013-03-13 14:22:07
我没有采取,你只是想单个文件的位置枚举。您将答案写入答案的方式,除非我错了,否则应该返回包含该词语的第一个文档的结果。列表中返回的第一个文件可能不是所指的文件。您需要改用[advance(int)](http://lucene.apache.org/core/4_0_0-BETA/core/org/apache/lucene/search/DocIdSetIterator.html#advance(int)),根据我更新的答案。 – femtoRgon 2013-03-13 22:54:44