I am trying to build a search engine in Google App Engine and wants to display the number of results obtained from that search query. I am using fetch() to display few results at a time but dont know how to display the number of results obtained from the search. The count() function has a limit of 1000 only and my results can exceed even 10,00,000. So, if someone know how to do so please suggest me.........
2 Answers
As others have told you, count()
doesn't scale efficiently.
To retrieve more than 1000 entities, despite of what the documentation says, you have to specify an upper limit:
your_model.all().count(100000000)

- 71,966
- 47
- 171
- 241
count function doesn't have a limit. It's description:
count(limit): Returns the number of results this query fetches. count() is somewhat faster than retrieving all of the data by a constant factor, but the running time still grows with the size of the result set. It's best to only use count() in cases where the count is expected to be small, or specify a limit. count() has no maximum limit. If you don't specify a limit, the datastore continues counting until it finishes counting or times out. From: GQL
So, you can use count() for it.
EDIT: You can do it starting from version 1.3.6 (released Aug-17-2010).
Have a look to changelog.
Also this should be helpful: How to fetch more than 1000?
If you still struggle with fetching no more that 1000 than try to iterate over the query fetching 1000 at a time and setting the offset to the next 1000 as explained at previous link.
-
I tried it but even if i dont provide the limit it counts to only 1000 at max – niteshb Feb 15 '11 at 09:41
-
I have edited my answer, have a look it. Also, If you think that this is a possible duplicate then it is better to delete question. – kamaci Feb 15 '11 at 09:58
-
i can fetch all the results but i want to display the count of results in a second I post the query. – niteshb Feb 15 '11 at 11:09
-
3Just because count can count more than 1000 records doesn't mean you _should_. It will be extremely inefficient for large numbers of results. – Nick Johnson Feb 15 '11 at 11:55
-
Consider Google's Search Engine it displays number of results and also first few results. What should I do to have the same thing? – niteshb Feb 15 '11 at 12:02
-
There are very few situations in which using count() is not an anti-pattern. I am assuming that are some at all. – Adam Crossland Feb 15 '11 at 14:07
-
Also consider that Google doesn't serve up more than 1,000 results. Try that: http://www.google.com.tr/search?hl=en&client=firefox-a&rls=org.mozilla:en-US:official&hs=qhu&q=1000+results&start=1000&sa=N – kamaci Feb 16 '11 at 08:51