diff --git a/elasticsearch_django/migrations/0001_initial.py b/elasticsearch_django/migrations/0001_initial.py index 45d0b38..97a1297 100644 --- a/elasticsearch_django/migrations/0001_initial.py +++ b/elasticsearch_django/migrations/0001_initial.py @@ -22,7 +22,7 @@ class Migration(migrations.Migration): ('total_hits', models.IntegerField(default=0, help_text='Total number of matches found for the query (!= the hits returned).')), ('reference', models.CharField(default='', help_text='Custom reference used to identify and group related searches.', max_length=100, blank=True)), ('executed_at', models.DateTimeField(help_text='When the search was executed - set via execute() method.')), - ('user', models.ForeignKey(related_name='search_queries', blank=True, to=settings.AUTH_USER_MODEL, help_text='The user who made the search query (nullable).', null=True)), + ('user', models.ForeignKey(related_name='search_queries', blank=True, to=settings.AUTH_USER_MODEL, help_text='The user who made the search query (nullable).', null=True, on_delete=models.SET_NULL)), ], ), ] diff --git a/elasticsearch_django/models.py b/elasticsearch_django/models.py index 756b432..f8c2d07 100644 --- a/elasticsearch_django/models.py +++ b/elasticsearch_django/models.py @@ -117,7 +117,7 @@ def from_search_query(self, search_query): """ hits = search_query.hits - score_sql = self._raw_sql([(h['id'], h['score']) for h in hits]) + score_sql = self._raw_sql([(h['id'], h['score'] or 0) for h in hits]) rank_sql = self._raw_sql([(hits[i]['id'], i) for i in range(len(hits))]) return ( self.get_queryset() @@ -367,7 +367,8 @@ class SearchQuery(models.Model): settings.AUTH_USER_MODEL, related_name='search_queries', blank=True, null=True, - help_text="The user who made the search query (nullable)." + help_text="The user who made the search query (nullable).", + on_delete=models.SET_NULL ) index = models.CharField( max_length=100, diff --git a/elasticsearch_django/tests/test_models.py b/elasticsearch_django/tests/test_models.py index 12915c5..00a38b9 100644 --- a/elasticsearch_django/tests/test_models.py +++ b/elasticsearch_django/tests/test_models.py @@ -220,6 +220,7 @@ def test__raw_sql(self): @mock.patch('django.db.models.query.QuerySet') def test_from_search_query(self, mock_qs): """Test the from_search_query method.""" + self.maxDiff = None sq = SearchQuery(hits=[{'id': 1, 'score': 1}, {'id': 2, 'score': 2}]) qs = TestModel.objects.from_search_query(sq) self.assertEqual( @@ -231,6 +232,18 @@ def test_from_search_query(self, mock_qs): '"elasticsearch_django_testmodel"."id" IN (1, 2) ORDER BY "search_rank" ASC' ) + # test with a null score - new in v5 + sq = SearchQuery(hits=[{'id': 1, 'score': None}, {'id': 2, 'score': 2}]) + qs = TestModel.objects.from_search_query(sq) + self.assertEqual( + str(qs.query), + 'SELECT "elasticsearch_django_testmodel"."id", ' + '(SELECT CASE elasticsearch_django_testmodel."id" WHEN 1 THEN 0 WHEN 2 THEN 2 ELSE 0 END) ' # noqa + 'AS "search_score", (SELECT CASE elasticsearch_django_testmodel."id" WHEN 1 THEN 0 WHEN 2 ' # noqa + 'THEN 1 ELSE 0 END) AS "search_rank" FROM "elasticsearch_django_testmodel" WHERE ' + '"elasticsearch_django_testmodel"."id" IN (1, 2) ORDER BY "search_rank" ASC' + ) + class SearchQueryTests(TestCase): diff --git a/urls.py b/urls.py index 039739a..5d43f33 100644 --- a/urls.py +++ b/urls.py @@ -5,5 +5,5 @@ admin.autodiscover() urlpatterns = [ - url(r'^admin/', include(admin.site.urls)), + url(r'^admin/', include(admin.site.urls,)), ]