Getting the number of hits in a document (doc) in lucene

How can I get the number of hits per document in Lucene in Java. I have

 
   IndexReader reader;
   reader = IndexReader.open (FSDirectory.open (new File (index)), true);
   Searcher searcher = new IndexSearcher (reader);
   String feild = "contents"
   QueryParser parser = new QueryParser (Version.LUCENE_CURRENT, field, analyzer);
   Query query = parser.parse ("test");
   TopScoreDocCollector collector = TopScoreDocCollector.create (
                    5 * hitsPerPage, false);
   searcher.search (query, collector);
   ScoreDoc [] hits = collector.topDocs (). ScoreDocs;
   Searcher searcher = new IndexSearcher (reader);
   int numTotalHits = collector.getTotalHits ();
   System.out.println (numTotalHits + "total matching documents"); 

for (int i = start; i <end; i ++) {int id = hit [i] .doc; TermFreqVector [] Tfv = reader.getTermFreqVectors (id);

TFV becomes null :( Someone can specify how to get hits in each document from there.

EDIT:

If we install TermVector.YES during indexing, this will work.

+4
source share
2 answers

You can write a custom Similarity implementation. You will get access to the frequency of the term, which will give you the number of times specified conditions in this document.

0
source

This is a duplicate Get the search term Hits (number of occurrences) per document in Lucene

, freq vector. jarekrozanski , , .

0

Source: https://habr.com/ru/post/1783951/


All Articles