如何让Lucene的retrieveTerms工作?

时间:2014-07-09 18:02:29

标签: java apache lucene morelikethis

我试图利用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);

}

1 个答案:

答案 0 :(得分:0)

愚蠢的我,我没有正确初始化我的索引阅读器。通过这样做得到它!