GAE Search API. Obtain total amount of matching documents

550 Views Asked by At

Hi,
I am using GAE Search API, and it seems to be a really great feature, which by the way adds so vital functionality lacked in standard datastore queries.
But i have faced a problem to implement a standard pagination, namely to get a total amount of matching the query documents. Certainly, i can implement a list with a "show more" button using Cursor, but it would be also great to be able to obtain a total amount.
Any ideas on how to do this?
Thank you very much in advance!

1

There are 1 best solutions below

1
On BEST ANSWER

Step 1:

set your accuracy

QueryOptions options = QueryOptions.newBuilder()
        ...set other options
        .setNumberFoundAccuracy(1000);
        .build();

Sets the accuracy requirement for Results.getNumberFound(). If set, getNumberFound() will be accurate up to at least that number. For example, when set to 100, any getNumberFound() <= 100 is accurate. This option may add considerable latency / expense, especially when used with setFieldsToReturn(String...).

Step 2 run query

 Query query = Query.newBuilder().setOptions(options).build(queryString);
 Results<ScoredDocument> results = getIndex().search(query);

Step 3 call getNumberFound()

 results.getNumberFound();

The number of results found by the search. If the value is less than or equal to the corresponding QueryOptions.getNumberFoundAccuracy(), then it is accurate, otherwise it is an approximation Returns: the number of results found