Haystack and Elasticsearch: Limit number of results
Haystack works kinda different from Django ORM. After limiting the queryset, you should call get_results() in order to get limited results. This is actually smart, because it avoids multiple requests from Elastic.
Example:
# Assume you have 800 records.sqs = SearchQuerySet()sqs.query.set_limits(low=0, high=4)len(sqs) # Will return 800 recordslen(sqs.get_results()) # Will return first 4 records.
Hope that it helps.
Adding on to the Yigit answer, if you want to have these offsets on filtered records just add filter condition when you form the SearchQuerySet
.
Also remember once the limits are set you can't change them by setting them again. You would need to form the SearchQuerySet()
again, or there is a method to clear the limits.
results = SearchQuerySet().filter(content="keyword")#we have a filtered resultSet now let's find specific recordsresults.query.set_limits(0,4)return results.query.get_results()