You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@lucene.apache.org by jm <jm...@gmail.com> on 2010/02/23 17:22:46 UTC

get the total number of hits of a query

Hi,

I need to find out how many hits a query will get, is this a valid
way? (Lucene 3.0)

        Query lucquery = ...;
        IndexSearcher[] indexes = ...
        MultiSearcher ms = new MultiSearcher(indexes);
        TopDocs tp = ms.search(lucquery, Integer.MAX_VALUE);
        int hits = tp.totalHits;

Then depending on the number of hits I will run it again to get all
results or not.

I have found some posts talking about something similar but I could
not see a clear answer...

thanks
javi

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Re: get the total number of hits of a query

Posted by Cristian Vat <cr...@gmail.com>.
You shouldn't set the number of documents wanted to Integer.MAX_VALUE.
The number of documents to the search method gives the size of the hit queue
where the results are stored so you would end up consuming a lot of memory.

Much easier to set it to something very small. Lucene must traverse all the
hits in any case to give you the highest scoring documents so totalHits
should be correct.

-
Cristian Vat

On Tue, Feb 23, 2010 at 18:22, jm <jm...@gmail.com> wrote:

> Hi,
>
> I need to find out how many hits a query will get, is this a valid
> way? (Lucene 3.0)
>
>        Query lucquery = ...;
>        IndexSearcher[] indexes = ...
>        MultiSearcher ms = new MultiSearcher(indexes);
>        TopDocs tp = ms.search(lucquery, Integer.MAX_VALUE);
>        int hits = tp.totalHits;
>
> Then depending on the number of hits I will run it again to get all
> results or not.
>
> I have found some posts talking about something similar but I could
> not see a clear answer...
>
> thanks
> javi
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org
>
>

Re: get the total number of hits of a query

Posted by jm <jm...@gmail.com>.
perfect, thanks guys

On Tue, Feb 23, 2010 at 5:28 PM, Uwe Schindler <uw...@thetaphi.de> wrote:
> Hi,
>
> TopDocs tp = ms.search(lucquery, Integer.MAX_VALUE);
>
> ^^^This will crash and throw OutOfMemoryException
>
> The simpliest way ist:
> TopDocs tp = ms.search(lucquery, 1);
> And then the total count is in tp.totalHits -- simple. The above query will still count all hits, but return only 1. Adjust according to your needs (e.g. 10).
>
> -----
> Uwe Schindler
> H.-H.-Meier-Allee 63, D-28213 Bremen
> http://www.thetaphi.de
> eMail: uwe@thetaphi.de
>
>
>> -----Original Message-----
>> From: jm [mailto:jmuguruza@gmail.com]
>> Sent: Tuesday, February 23, 2010 5:23 PM
>> To: java-user@lucene.apache.org
>> Subject: get the total number of hits of a query
>>
>> Hi,
>>
>> I need to find out how many hits a query will get, is this a valid
>> way? (Lucene 3.0)
>>
>>         Query lucquery = ...;
>>         IndexSearcher[] indexes = ...
>>         MultiSearcher ms = new MultiSearcher(indexes);
>>         TopDocs tp = ms.search(lucquery, Integer.MAX_VALUE);
>>         int hits = tp.totalHits;
>>
>> Then depending on the number of hits I will run it again to get all
>> results or not.
>>
>> I have found some posts talking about something similar but I could
>> not see a clear answer...
>>
>> thanks
>> javi
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
>> For additional commands, e-mail: java-user-help@lucene.apache.org
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


RE: get the total number of hits of a query

Posted by Uwe Schindler <uw...@thetaphi.de>.
Hi,

TopDocs tp = ms.search(lucquery, Integer.MAX_VALUE);

^^^This will crash and throw OutOfMemoryException

The simpliest way ist:
TopDocs tp = ms.search(lucquery, 1);
And then the total count is in tp.totalHits -- simple. The above query will still count all hits, but return only 1. Adjust according to your needs (e.g. 10).

-----
Uwe Schindler
H.-H.-Meier-Allee 63, D-28213 Bremen
http://www.thetaphi.de
eMail: uwe@thetaphi.de


> -----Original Message-----
> From: jm [mailto:jmuguruza@gmail.com]
> Sent: Tuesday, February 23, 2010 5:23 PM
> To: java-user@lucene.apache.org
> Subject: get the total number of hits of a query
> 
> Hi,
> 
> I need to find out how many hits a query will get, is this a valid
> way? (Lucene 3.0)
> 
>         Query lucquery = ...;
>         IndexSearcher[] indexes = ...
>         MultiSearcher ms = new MultiSearcher(indexes);
>         TopDocs tp = ms.search(lucquery, Integer.MAX_VALUE);
>         int hits = tp.totalHits;
> 
> Then depending on the number of hits I will run it again to get all
> results or not.
> 
> I have found some posts talking about something similar but I could
> not see a clear answer...
> 
> thanks
> javi
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org