From babe8a0c09cad1395213c18d3d307ed5b3039a90 Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Thu, 30 Mar 2017 11:23:25 +0100 Subject: [PATCH] Fix annotate_score followed by slice (#3518) This fixes the bug reported by @nimasmi in https://github.com/wagtail/wagtail/issues/3431#issuecomment-288051751 Slicing creates a new SearchResults object but the ``score_field`` (set by ``.annotate_score()`` wasn't being passed along to the new SearchResults causing the score to not be annotated. --- CHANGELOG.txt | 1 + docs/releases/1.10.rst | 1 + wagtail/wagtailsearch/backends/base.py | 1 + wagtail/wagtailsearch/tests/test_elasticsearch_backend.py | 7 +++++++ 4 files changed, 10 insertions(+) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index c134cd71c4..fe56c11d80 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -48,6 +48,7 @@ Changelog * Fix: The page type usage listing now have a translatable page title (Ramon de Jezus) * Fix: Styles for submission filtering form now have a consistent height. (Thijs Kramer) * Fix: Custom user models with a primary key type requiring `get_db_prep_value` conversion are now supported (thenewguy) + * Fix: Slicing a search result set no longer loses the annotation added by `annotate_score` (Karl Hobley) 1.9 (16.02.2017) diff --git a/docs/releases/1.10.rst b/docs/releases/1.10.rst index 7ecd6566d7..01e0d2bbfc 100644 --- a/docs/releases/1.10.rst +++ b/docs/releases/1.10.rst @@ -59,6 +59,7 @@ Bug fixes * List-based fields within form builder form submissions are now displayed as comma-separated strings rather than as Python lists (Christine Ho, Matt Westcott) * The page type usage listing now have a translatable page title (Ramon de Jezus) * Styles for submission filtering form now have a consistent height. (Thijs Kramer) + * Slicing a search result set no longer loses the annotation added by `annotate_score` (Karl Hobley) Upgrade considerations diff --git a/wagtail/wagtailsearch/backends/base.py b/wagtail/wagtailsearch/backends/base.py index 153ba80d0f..a9abca8826 100644 --- a/wagtail/wagtailsearch/backends/base.py +++ b/wagtail/wagtailsearch/backends/base.py @@ -124,6 +124,7 @@ class BaseSearchResults(object): new = klass(self.backend, self.query, prefetch_related=self.prefetch_related) new.start = self.start new.stop = self.stop + new._score_field = self._score_field return new def _do_search(self): diff --git a/wagtail/wagtailsearch/tests/test_elasticsearch_backend.py b/wagtail/wagtailsearch/tests/test_elasticsearch_backend.py index af738ea9c0..9df9751831 100644 --- a/wagtail/wagtailsearch/tests/test_elasticsearch_backend.py +++ b/wagtail/wagtailsearch/tests/test_elasticsearch_backend.py @@ -254,6 +254,13 @@ class TestElasticsearchSearchBackend(BackendTests, TestCase): for result in results: self.assertIsInstance(result._score, float) + def test_annotate_score_with_slice(self): + # #3431 - Annotate score wasn't being passed to new queryset when slicing + results = self.backend.search("Hello", models.SearchTest).annotate_score('_score')[:10] + + for result in results: + self.assertIsInstance(result._score, float) + class TestElasticsearchSearchQuery(TestCase): def assertDictEqual(self, a, b):