如何在Lucene 4.0中更改TermFreVector

时间:2014-03-26 00:49:15

标签: lucene

以下是代码:

public int docLength(String filename) throws IOException {
    int length = 0;
    TermFreqVector t = indexReader.getTermFreqVector(0, "contents");
    for (int i = 0; i < t.getTermFrequencies().length; i++) {
        length += t.getTermFrequencies()[i];
    }
    return length;
}

public int docLength(int id) throws IOException {
    int length = 0;
    TermFreqVector t = indexReader.getTermFreqVector(id, "contents");
    for (int i = 0; i < t.getTermFrequencies().length; i++) {
        length += t.getTermFrequencies()[i];
    }
    return length;
}

错误是:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    TermFreqVector cannot be resolved to a type
    The method getTermFreqVector(int, String) is undefined for the type IndexReader

    at BM25.docLength(BM25.java:96)
    at BM25.avgDocLength(BM25.java:130)

1 个答案:

答案 0 :(得分:1)

访问和遍历术语向量在4.0中发生了显着变化。 Migration Guide是一个非常有用的资源,其中包括从3.6到4.0的其他变化。

在这种情况下,您需要通过致电Terms来访问IndexReader.getTermVector个实例:

int length = 0;
TermEnum terms = indexReader.getTermVector(id, "contents").iterator();
while (terms.next())
    length += terms.totalTermFreq(null);
return length;