Skip to content

Commit

Permalink
Fix #1223: Cap search results to 10000
Browse files Browse the repository at this point in the history
  • Loading branch information
seav committed Mar 8, 2017
1 parent 9e1ca6a commit 5927972
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 1 deletion.
1 change: 1 addition & 0 deletions cadasta/config/settings/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,3 +523,4 @@
ES_SCHEME = 'http'
ES_HOST = 'localhost'
ES_PORT = '9200'
ES_MAX_RESULTS = 10000
41 changes: 41 additions & 0 deletions cadasta/search/tests/test_views_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,47 @@ def test_post_with_results(self, mock_post, mock_get):
)
mock_get.assert_not_called()

@patch('requests.get')
@patch('requests.post')
def test_post_with_over_max_results(self, mock_post, mock_get):
mock_post.return_value.status_code = 200
mock_post.return_value.json.return_value = {
'hits': {
'total': settings.ES_MAX_RESULTS + 1000,
'hits': [{
'_type': 'spatial',
'_source': {
'id': self.su.id,
'type': 'AP',
'@timestamp': 'TIMESTAMP',
},
}],
},
}

response = self.request(user=self.user, method='POST')
expected_html = render_to_string(
'search/search_result_item.html',
context={'result': {
'entity_type': self.su.ui_class_name,
'url': self.su.get_absolute_url(),
'main_label': "Apartment",
'attributes': {},
}}
)
assert response.status_code == 200
assert response.content['data'] == [[expected_html]]
assert response.content['recordsTotal'] == settings.ES_MAX_RESULTS
assert response.content['recordsFiltered'] == settings.ES_MAX_RESULTS
assert response.content['draw'] == 40
assert response.content['timestamp'] == 'TIMESTAMP'
mock_post.assert_called_once_with(
self.es_endpoint,
data=self.es_body,
headers={'content-type': 'application/json'},
)
mock_get.assert_not_called()

@patch('requests.get')
@patch('requests.post')
def test_post_with_no_results(self, mock_post, mock_get):
Expand Down
3 changes: 3 additions & 0 deletions cadasta/search/tests/test_views_default.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import pytest
import json

from django.conf import settings
from django.http import Http404
from django.test import TestCase

Expand Down Expand Up @@ -40,6 +42,7 @@ def setup_models(self):
def setup_template_context(self):
return {
'object': self.project,
'max_num_results': settings.ES_MAX_RESULTS,
}

def setup_url_kwargs(self):
Expand Down
3 changes: 2 additions & 1 deletion cadasta/search/views/async.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ def post(self, request, *args, **kwargs):
'error': 'unavailable',
})

num_hits = raw_results['hits']['total']
num_hits = min(raw_results['hits']['total'],
settings.ES_MAX_RESULTS)
results = raw_results['hits']['hits']

if len(results) == 0:
Expand Down
3 changes: 3 additions & 0 deletions cadasta/search/views/default.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from django.conf import settings

from core.views.generic import TemplateView
from core.mixins import LoginPermissionRequiredMixin
from organization import messages as org_messages
Expand All @@ -15,6 +17,7 @@ class Search(LoginPermissionRequiredMixin,
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['object'] = self.get_object()
context['max_num_results'] = settings.ES_MAX_RESULTS
return context

def get_object(self, queryset=None):
Expand Down
1 change: 1 addition & 0 deletions cadasta/templates/search/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ <h2>{% trans "Search" %}</h2>
<ul>
<li>Review your wording, make sure that there aren’t any typos in your search query</li>
<li>If no results match your query, try removing some search terms (<kbd>John</kbd>, instead of <kbd>John White</kbd>)</li>
<li>Search results are limited to a maximum of {{ max_num_results }} entries</li>
</ul>
{% endblocktrans%}
</div>
Expand Down

0 comments on commit 5927972

Please sign in to comment.