我试图利用http://svn.apache.org/repos/asf/lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/mlt/MoreLikeThis.java中的retrieveTerms方法,并且我在下一行中遇到NullPointerException
final Fields vectors = ir.getTermVectors(docNum);
我并不确切知道为什么会这样,所以任何解释都会很棒。为清楚起见,我将发布下面的整个方法,以及之前的ir。
声明private static IndexReader ir;
public static PriorityQueue<Object[]> retrieveTerms(int docNum) throws IOException {
Map<String, Int> termFreqMap = new HashMap<>();
for (String fieldName : fieldNames) {
final Fields vectors = ir.getTermVectors(docNum);
final Terms vector;
if (vectors != null) {
vector = vectors.terms(fieldName);
} else {
vector = null;
}
// field does not store term vector info
if (vector == null) {
Document d = ir.document(docNum);
StorableField[] fields = (StorableField[]) d.getFields(fieldName);
for (StorableField field : fields) {
final String stringValue = field.stringValue();
if (stringValue != null) {
addTermFrequencies(new StringReader(stringValue), termFreqMap, fieldName);
}
}
} else {
addTermFrequencies(termFreqMap, vector);
}
}
return createQueue(termFreqMap);
}
答案 0 :(得分:0)
愚蠢的我,我没有正确初始化我的索引阅读器。通过这样做得到它!