Search code examples
javaapachelucenemorelikethis

How to get Lucene's retrieveTerms working?


I'm trying to leverage the retrieveTerms method from http://svn.apache.org/repos/asf/lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/mlt/MoreLikeThis.java and I'm hitting a NullPointerException on the following line

final Fields vectors = ir.getTermVectors(docNum);

I don't exactly know why this is happening, so any explanation would be awesome. For clarity, I'll post the whole method below, as well as the earlier declaration of 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);

}


Solution

  • Silly me, I had not correctly initialized my index reader. Got it working by doing so!