Dear All,
I am trying to get the term frequencies (through TermFreqVector) of a
document (using Lucene 2.9.1). In order to do that I have used the following
code. But there is a compile time error in the code and I can't figure it
out. Could somebody can guide me what's wrong with it.
Compile time error I got:
Cannot make a static reference to the non-static method
getTermFreqVector(int, String) from the type IndexReader.
Code:
*import* org.apache.lucene.analysis.standard.StandardAnalyzer;
*import* org.apache.lucene.document.Document;
*
import* org.apache.lucene.document.Field;
*
import* org.apache.lucene.index.IndexWriter;
*
import* org.apache.lucene.queryParser.ParseException;
*
import* org.apache.lucene.queryParser.QueryParser;
*
import* org.apache.lucene.search.*;
*
import* org.apache.lucene.store.Directory;
*
import* org.apache.lucene.store.RAMDirectory;
*
import* org.apache.lucene.util.Version;
*
import* org.apache.lucene.index.IndexReader;
*
import* org.apache.lucene.index.TermEnum;
*
import* org.apache.lucene.index.Term;
*
import* org.apache.lucene.index.TermFreqVector;
*
import* java.io.IOException;
*
public* *class* DemoTest {
*public* *static* *void* main(String[] args) {
StandardAnalyzer analyzer = *new* StandardAnalyzer(Version.*LUCENE_CURRENT*
);
*try* {
Directory directory = *new* RAMDirectory();
IndexWriter iwriter = *new* IndexWriter(directory, analyzer,
*true*,*new*IndexWriter.MaxFieldLength(25000));
Document doc = *new* Document();
String text = "This is the text to be indexed.";
doc.add(*new* Field("fieldname", text, Field.Store.*YES*,Field.Index.*
ANALYZED*,Field.TermVector.*WITH_POSITIONS_OFFSETS*));
iwriter.addDocument(doc);
TermFreqVector vector = IndexReader.getTermFreqVector(0, "fieldname" );
*
int* size = vector.size();
*for* ( String term : vector.getTerms() )
System.*out*.println( "size = " + size );
iwriter.close();
IndexSearcher isearcher = *new* IndexSearcher(directory, *true*);
QueryParser parser = *new* QueryParser(Version.*LUCENE_CURRENT*, "fieldname",
analyzer);
Query query = parser.parse("text");
ScoreDoc[] hits = isearcher.search(query, *null*, 1000).scoreDocs;
System.*out*.println("hits.length(1) = " + hits.length);
// Iterate through the results:
*for* (*int* i = 0; i < hits.length; i++) {
Document hitDoc = isearcher.doc(hits.doc);
System.*out*.println("hitDoc.get(\"fieldname\") (This is the text to be
indexed) = " +
hitDoc.get("fieldname"));
}
isearcher.close();
directory.close();
} *catch* (Exception ex) {
ex.printStackTrace();
}
}
}
Thanks in advance
Manjula