简体   繁体   中英

Lucene. how do I count the number of hits per document?

At StackOverflow I found this one In a Lucene / Lucene.net search, how do I count the number of hits per document?

But I can not define SpanQuery from code below. I'm using Lucene 4.4.0(spanquery defining is different than older versions)

IndexReader indexReader = // define your index reader here
SpanQuery spanQuery = // define your span query here
Spans spans = spanQuery.getSpans(indexReader);
int occurrenceCount = 0;
while (spans.next()) {
    occurrenceCount++;
}

Anybody can help? I would really appreciate full answer with an example

The easier way to get the total number of occurances of a term in the index, as per the linked-to answer, would be:

Term term = new Term("myfield", "myterm");
long numOccurances = indexReader.totalTermFreq(term);

For example

SpanQuery spanQuery = new SpanTermQuery(new Term("myfield", "myterm"); // define your span query here`enter code here`

can be used when you are interested in a single term (This example looks for the term "myterm" in the field "myfield").

Look at the other SpanQuery implementations also (SpanOrQuery, SpanNearQuery, SpanNotQuery, ...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM