Repository: wagtail/wagtail

Issue:
Empty search in the API leading to TypeError
Steps to reproduce:
- use Wagtail 1.9 & elasticsearch 2
- install Wagtail’s API
- go to /api/v2/pages/?search=

You end up with:
```
File "…/wagtail/api/v2/pagination.py" in paginate_queryset
  36.         self.total_count = queryset.count()

TypeError: count() takes exactly one argument (0 given)
```


Discussion:

Resolving patch:
diff --git a/wagtail/wagtailsearch/backends/base.py b/wagtail/wagtailsearch/backends/base.py
--- a/wagtail/wagtailsearch/backends/base.py
+++ b/wagtail/wagtailsearch/backends/base.py
@@ -186,6 +186,20 @@ def annotate_score(self, field_name):
         return clone
 
 
+class EmptySearchResults(BaseSearchResults):
+    def __init__(self):
+        return super(EmptySearchResults, self).__init__(None, None)
+
+    def _clone(self):
+        return self.__class__()
+
+    def _do_search(self):
+        return []
+
+    def _do_count(self):
+        return 0
+
+
 class BaseSearchBackend(object):
     query_class = None
     results_class = None
@@ -230,11 +244,11 @@ def search(self, query_string, model_or_queryset, fields=None, filters=None,
 
         # Model must be a class that is in the index
         if not class_is_indexed(model):
-            return []
+            return EmptySearchResults()
 
         # Check that theres still a query string after the clean up
         if query_string == "":
-            return []
+            return EmptySearchResults()
 
         # Only fields that are indexed as a SearchField can be passed in fields
         if fields:


Test patch:
diff --git a/wagtail/api/v2/tests/test_pages.py b/wagtail/api/v2/tests/test_pages.py
--- a/wagtail/api/v2/tests/test_pages.py
+++ b/wagtail/api/v2/tests/test_pages.py
@@ -82,7 +82,6 @@ def test_private_pages_dont_appear_in_list(self):
         content = json.loads(response.content.decode('UTF-8'))
         self.assertEqual(content['meta']['total_count'], new_total_count)
 
-
     # TYPE FILTER
 
     def test_type_filter_items_are_all_blog_entries(self):
@@ -739,6 +738,13 @@ def test_search_operator_or(self):
 
         self.assertEqual(set(page_id_list), set([16, 18, 19]))
 
+    def test_empty_searches_work(self):
+        response = self.get_response(search='')
+        content = json.loads(response.content.decode('UTF-8'))
+        self.assertEqual(response.status_code, 200)
+        self.assertEqual(response['Content-type'], 'application/json')
+        self.assertEqual(content['meta']['total_count'], 0)
+
 
 class TestPageDetail(TestCase):
     fixtures = ['demosite.json']
diff --git a/wagtail/contrib/wagtailapi/tests/test_pages.py b/wagtail/contrib/wagtailapi/tests/test_pages.py
--- a/wagtail/contrib/wagtailapi/tests/test_pages.py
+++ b/wagtail/contrib/wagtailapi/tests/test_pages.py
@@ -534,6 +534,13 @@ def test_search_when_filtering_by_tag_gives_error(self):
         self.assertEqual(response.status_code, 400)
         self.assertEqual(content, {'message': "filtering by tag with a search query is not supported"})
 
+    def test_empty_searches_work(self):
+        response = self.get_response(search='')
+        content = json.loads(response.content.decode('UTF-8'))
+        self.assertEqual(response.status_code, 200)
+        self.assertEqual(response['Content-type'], 'application/json')
+        self.assertEqual(content['meta']['total_count'], 0)
+
 
 class TestPageDetail(TestCase):
     fixtures = ['demosite.json']