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 #46 from Kinto/45-limit-response-size
Browse files Browse the repository at this point in the history
Limit response size (fixes #45)
  • Loading branch information
leplatrem authored Jun 13, 2017
2 parents 9ea6c1a + 22db03c commit ef3d9b8
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ Changelog
0.2.0 (unreleased)
------------------

- Nothing changed yet.
**Bug fixes**

- Limit the number of results returned by default (fixes #45)

0.1.0 (2017-05-26)
------------------
Expand Down
4 changes: 2 additions & 2 deletions kinto_elasticsearch/indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ def create_index(self, bucket_id, collection_id, schema=None):
body = {"mappings": {indexname: schema}}
else:
body = None
self.client.indices.create(index=indexname, body=body)
return self.client.indices.create(index=indexname, body=body)
else:
self.update_index(bucket_id, collection_id, schema)
return self.update_index(bucket_id, collection_id, schema)

def update_index(self, bucket_id, collection_id, schema=None):
indexname = self.indexname(bucket_id, collection_id)
Expand Down
7 changes: 7 additions & 0 deletions kinto_elasticsearch/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ def search_view(request, **kwargs):
bucket_id = request.matchdict['bucket_id']
collection_id = request.matchdict['collection_id']

# Limit the number of results to return, based on existing Kinto settings.
paginate_by = request.registry.settings.get("paginate_by")
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))

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


class LimitedResults(BaseWebTest, unittest.TestCase):
def get_app(self, settings):
app = self.make_app(settings=settings)
app.put("/buckets/bid", headers=self.headers)
app.put("/buckets/bid/collections/cid", headers=self.headers)
requests = [{
"method": "POST",
"path": "/buckets/bid/collections/cid/records",
"body": {"data": {"age": i}}
} for i in range(5)]
app.post_json("/batch", {"requests": requests}, headers=self.headers)
return app

def test_the_number_of_responses_is_limited_by_paginate_by_setting(self):
app = self.get_app({"paginate_by": 2})
resp = app.get("/buckets/bid/collections/cid/search", headers=self.headers)
result = resp.json
assert len(result["hits"]["hits"]) == 2

def test_the_number_of_responses_is_limited_by_max_fetch_size_setting(self):
app = self.get_app({"storage_max_fetch_size": 2})
resp = app.get("/buckets/bid/collections/cid/search", headers=self.headers)
result = resp.json
assert len(result["hits"]["hits"]) == 2

def test_the_number_of_responses_is_limited_by_smaller_limit(self):
app = self.get_app({"paginate_by": 4, "storage_max_fetch_size": 2})
resp = app.get("/buckets/bid/collections/cid/search", headers=self.headers)
result = resp.json
assert len(result["hits"]["hits"]) == 2

def test_the_number_of_responses_is_limited_by_only_defined_limit(self):
app = self.get_app({"paginate_by": 0, "storage_max_fetch_size": 2})
resp = app.get("/buckets/bid/collections/cid/search", headers=self.headers)
result = resp.json
assert len(result["hits"]["hits"]) == 2


class PermissionsCheck(BaseWebTest, unittest.TestCase):
def test_search_is_allowed_if_write_on_bucket(self):
body = {"permissions": {"write": ["system.Everyone"]}}
Expand Down

0 comments on commit ef3d9b8

Please sign in to comment.