Skip to content

Commit

Permalink
Merge pull request #19 from /issues/18
Browse files Browse the repository at this point in the history
Fix null score issue (#18)
  • Loading branch information
hugorodgerbrown authored Feb 23, 2018
2 parents 129655f + 11c0992 commit 87c5cf6
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion elasticsearch_django/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
],
),
]
5 changes: 3 additions & 2 deletions elasticsearch_django/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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,
Expand Down
13 changes: 13 additions & 0 deletions elasticsearch_django/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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):

Expand Down
2 changes: 1 addition & 1 deletion urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
admin.autodiscover()

urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^admin/', include(admin.site.urls,)),
]

0 comments on commit 87c5cf6

Please sign in to comment.