Hello

 

I am trying to use a TermFreqVector to get a count of all words in a
Document as follows:

 

   // Search.

        int hitsPerPage = 10;

        IndexSearcher searcher = new IndexSearcher(index, true);

        TopScoreDocCollector collector =
TopScoreDocCollector.create(hitsPerPage, true);

        searcher.search(q, collector);

        ScoreDoc[] hits = collector.topDocs().scoreDocs;

 

        // Display results.

        int docId = 0;

        System.out.println("Found " + hits.length + " hits.");

        for (int i = 0; i < hits.length; ++i) {

            docId = hits[i].doc;

            Document d = searcher.doc(docId);

            System.out.println((i + 1) + ". " + d.get("title"));

            IndexReader trd = IndexReader.open(index);

            TermFreqVector tfv = trd.getTermFreqVector(docId, "title");

            System.out.println(tfv.getTerms().toString());

            System.out.println(tfv.getTermFrequencies().toString());

        }

 

The code is very rough as its only an experiment but I'm under the
impression that the getTerms and getTermFrequencies methods for a
TermFreqVector should allow each word and its frequency in the document to
be displayed. All I get though is a NullPointerError. The index consists of
a single document made up of a simple string:

 

IndexWriter w = new IndexWriter(index, analyzer, true,
IndexWriter.MaxFieldLength.UNLIMITED);        

addDoc(w, "Lucene for Dummies"); 

 

And the queryString being used is simply "dummies".  

 

Thanks

 

Martin O'Shea.    

Reply via email to