Skip to content

Commit

Permalink
feat: add searchScore field to pages
Browse files Browse the repository at this point in the history
  • Loading branch information
dopry committed Mar 10, 2023
1 parent 7c32128 commit 614f5b9
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
44 changes: 41 additions & 3 deletions example/example/tests/test_grapple.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import shutil
import unittest
from pydoc import locate
import unittest
from unittest.mock import patch

import wagtail_factories
Expand Down Expand Up @@ -442,24 +443,61 @@ def setUpTestData(cls):
connection.vendor != "sqlite",
"sqlite doesn't support scoring so natural order will be in the order of defintion",
)
def test_natural_order_sqlite(self):
def test_searchQuery_order_by_relevance_sqlite(self):
query = """
query($searchQuery: String, $order: String) {
pages(searchQuery: $searchQuery, order: $order) {
title
searchScore
}
}
"""

executed = self.client.execute(query, variables={"searchQuery": "Alpha"})
page_data = executed["data"].get("pages")

self.assertEquals(len(page_data), 6)
self.assertEquals(page_data[0]["title"], "Alpha")
self.assertEquals(page_data[0]["searchScore"], None)
self.assertEquals(page_data[1]["title"], "Alpha Alpha")
self.assertEquals(page_data[1]["searchScore"], None)
self.assertEquals(page_data[2]["title"], "Alpha Beta")
self.assertEquals(page_data[2]["searchScore"], None)
self.assertEquals(page_data[3]["title"], "Alpha Gamma")
self.assertEquals(page_data[3]["searchScore"], None)
self.assertEquals(page_data[4]["title"], "Beta Alpha")
self.assertEquals(page_data[4]["searchScore"], None)
self.assertEquals(page_data[5]["title"], "Gamma Alpha")
self.assertEquals(page_data[5]["searchScore"], None)

@unittest.skipIf(
connection.vendor != "postgresql",
"postgres supports scoring so natural order will be in the order of score",
)
def test_searchQuery_order_by_relevance_postgres(self):
query = """
query($searchQuery: String, $order: String) {
pages(searchQuery: $searchQuery, order: $order) {
title
searchScore
}
}
"""

executed = self.client.execute(query, variables={"searchQuery": "Alpha"})
page_data = executed["data"].get("pages")
self.assertEquals(len(page_data), 6)
self.assertEquals(page_data[0]["title"], "Alpha Alpha")
self.assertEquals(page_data[0]["searchScore"], 0.23700128495693207)
self.assertEquals(page_data[1]["title"], "Alpha")
self.assertEquals(page_data[1]["searchScore"], 0.18960101902484894)
self.assertEquals(page_data[2]["title"], "Gamma Alpha")
self.assertEquals(page_data[2]["searchScore"], 0.11060059443116188)
self.assertEquals(page_data[3]["title"], "Beta Alpha")
self.assertEquals(page_data[3]["searchScore"], 0.11060059443116188)
self.assertEquals(page_data[4]["title"], "Alpha Gamma")
self.assertEquals(page_data[4]["searchScore"], 0.11060059443116188)
self.assertEquals(page_data[5]["title"], "Alpha Beta")
self.assertEquals(page_data[5]["searchScore"], 0.10533389945824942)

def test_explicit_order(self):
query = """
Expand All @@ -476,9 +514,9 @@ def test_explicit_order(self):

self.assertEquals(len(page_data), 6)
self.assertEquals(page_data[0]["title"], "Gamma Gamma")
self.assertEquals(page_data[3]["title"], "Gamma")
self.assertEquals(page_data[1]["title"], "Gamma Beta")
self.assertEquals(page_data[2]["title"], "Gamma Alpha")
self.assertEquals(page_data[3]["title"], "Gamma")
self.assertEquals(page_data[4]["title"], "Beta Gamma")
self.assertEquals(page_data[5]["title"], "Alpha Gamma")

Expand Down
9 changes: 9 additions & 0 deletions grapple/types/pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class PageInterface(graphene.Interface):
graphene.NonNull(lambda: PageInterface), enable_search=True, required=True
)

search_score = graphene.Float()

@classmethod
def resolve_type(cls, instance, info, **kwargs):
"""
Expand Down Expand Up @@ -149,6 +151,13 @@ def resolve_seo_title(self, info, **kwargs):
"""
return self.seo_title or self.title

def resolve_search_score(self, info, **kwargs):
"""
Get page's search score, will be None if not in a search context.
"""
return getattr(self, "search_score", None)



class Page(DjangoObjectType):
"""
Expand Down
5 changes: 5 additions & 0 deletions grapple/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.conf import settings
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
from django.db import connection
from wagtail.models import Site
from wagtail.search.index import class_is_indexed
from wagtail.search.models import Query
Expand Down Expand Up @@ -112,6 +113,8 @@ def resolve_queryset(
query.add_hit()

qs = qs.search(search_query, order_by_relevance=order_by_relevance)
if connection.vendor != 'sqlite':
qs = qs.annotate_score("search_score")

return _sliced_queryset(qs, limit, offset)

Expand Down Expand Up @@ -197,6 +200,8 @@ def resolve_paginated_queryset(
query.add_hit()

qs = qs.search(search_query, order_by_relevance=order_by_relevance)
if connection.vendor != 'sqlite':
qs = qs.annotate_score("search_score")

return get_paginated_result(qs, page, per_page)

Expand Down

0 comments on commit 614f5b9

Please sign in to comment.