When you call IndexSearcher#search(Query query, int n), there are two cases:
 - either your query matches n hits or more, and the TopDocs object will
have a ScoreDoc[] array that contains the n best scoring hits sorted by
descending score,
 - or your query matches less then n hits and then the TopDocs object will
have all matches in the ScoreDoc[] array, sorted by descending score.

In both cases, TopDocs#totalHits gives information about the total number
of matches of the query. On older versions of Lucene (<7.0) this is an
integer that is always accurate, while on more recent versions of Lucene
(>= 8.0) it is a lower bound of the total number of matches. It typically
returns the number of collected documents indeed, though this is an
implementation detail that might change in the future.

If you want to count the number of matches of a Query precisely, you can
use IndexSearcher#count.

On Tue, Jun 8, 2021 at 7:51 AM <baris.ka...@oracle.com> wrote:

>
> https://stackoverflow.com/questions/50368313/relation-between-topdocs-totalhits-and-parameter-n-of-indexsearcher-search
>
> looks like someone else also had this problem, too.
>
> Any suggestions please?
>
> Best regards
>
>
> On 6/8/21 1:36 AM, baris.ka...@oracle.com wrote:
> > Hi,-
> >
> >  I use IndexSearcher.search API with two parameters like Query and int
> > number (i set as 20).
> >
> > However, when i look at the TopDocs object which is the result of this
> > above API call
> >
> > i see thousands of hits from totalhits. Is this inaccurate or Lucene
> > is doing actually search based on that many results?
> >
> > But when i iterate over result of above API call's scoreDocs object i
> > get int number of hits (ie, 20 hits).
> >
> >
> > I am trying to find out why org.apache.lucene.search.Topdocs.TotalHits
> > report a number of collected results than
> >
> > the actual number of results. I see on the order of couple of
> > thousands vs 20.
> >
> >
> > Best regards
> >
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscr...@lucene.apache.org
> For additional commands, e-mail: java-user-h...@lucene.apache.org
>
>

-- 
Adrien

Reply via email to