Skip to content
This repository has been archived by the owner on Aug 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #49 from Kinto/45-take-query-size-into-account
Browse files Browse the repository at this point in the history
Fix the number of results when specified in query (ref #45)
  • Loading branch information
leplatrem authored Jun 14, 2017
2 parents acae3da + 0dd4c4b commit 67d82bb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ Changelog
0.3.0 (unreleased)
------------------

- Nothing changed yet.
**Bug fixes**

- Fix the number of results when specified in query (ref #45)


0.2.0 (2017-06-13)
Expand Down
13 changes: 12 additions & 1 deletion kinto_elasticsearch/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import logging

import elasticsearch
Expand Down Expand Up @@ -34,7 +35,17 @@ def search_view(request, **kwargs):
max_fetch_size = request.registry.settings["storage_max_fetch_size"]
if paginate_by is None or paginate_by <= 0:
paginate_by = max_fetch_size
kwargs.setdefault("size", min(paginate_by, max_fetch_size))
configured = min(paginate_by, max_fetch_size)
# If the size is specified in query, ignore it if larger than setting.
specified = None
if "body" in kwargs:
try:
body = json.loads(kwargs["body"].decode("utf-8"))
specified = body.get("size")
except json.decoder.JSONDecodeError:
pass
if specified is None or specified > configured:
kwargs.setdefault("size", configured)

# Access indexer from views using registry.
indexer = request.registry.indexer
Expand Down
22 changes: 22 additions & 0 deletions tests/test_elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,28 @@ def test_the_number_of_responses_is_limited_by_only_defined_limit(self):
result = resp.json
assert len(result["hits"]["hits"]) == 2

def test_size_specified_in_query_is_taken_into_account(self):
app = self.get_app({"paginate_by": 3})
query = {
"from": 0, "size" : 2,
"query": {"match_all": {}}
}
resp = app.post_json("/buckets/bid/collections/cid/search", query,
headers=self.headers)
result = resp.json
assert len(result["hits"]["hits"]) == 2

def test_size_specified_in_query_is_caped_by_setting(self):
app = self.get_app({"paginate_by": 3})
query = {
"size" : 4,
"query": {"match_all": {}}
}
resp = app.post_json("/buckets/bid/collections/cid/search", query,
headers=self.headers)
result = resp.json
assert len(result["hits"]["hits"]) == 3


class PermissionsCheck(BaseWebTest, unittest.TestCase):
def test_search_is_allowed_if_write_on_bucket(self):
Expand Down

0 comments on commit 67d82bb

Please sign in to comment.