I tried out the following code to find the number of occurrences of a word in a
document using Lucene. It doesnt work...can someone help me?
{
IndexReader reader = IndexReader.open("index");
Searcher searcher = new IndexSearcher("index");
Analyzer analyzer = new StandardAnalyzer();
BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
// CODE TO TAKE A LINE FROM THE USER
Query query = QueryParser.parse(line, "contents", analyzer);
Hits hits = searcher.search(query);
Term term = new Term("body", line); /* line is the actual text being searched */
TermDocs help = reader.termDocs(term);
/* THIS LOOP IS NEVER ENTERED WHEN THE CODE IS EXECUTED!*/
while (help.next() == true)
{
System.out.println ("\nFreq: " + help.freq());
}
int [] docs = new int [hits.length()];
int [] frequency = new int [hits.length()];
help.read (docs,frequency);
// PRINT THE ARRAYS DOCS AND FREQUENCY. ALL VALUES ARE ZERO!
}
Can you help me please? The while loop is never entered and the array of document
numbers and word frequencies contains all zeroes. Why is this happening?