Skip to content

Commit

Permalink
Switch author search from TrigramSimilarity to SearchQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
BartSchuurmans committed Mar 19, 2024
1 parent d304ceb commit 5809935
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
13 changes: 6 additions & 7 deletions bookwyrm/book_search.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
""" using a bookwyrm instance as a source of book data """

from __future__ import annotations
from dataclasses import asdict, dataclass
from functools import reduce
Expand All @@ -21,8 +22,7 @@ def search(
min_confidence: float = 0,
filters: Optional[list[Any]] = None,
return_first: Literal[False],
) -> QuerySet[models.Edition]:
...
) -> QuerySet[models.Edition]: ...


@overload
Expand All @@ -32,8 +32,7 @@ def search(
min_confidence: float = 0,
filters: Optional[list[Any]] = None,
return_first: Literal[True],
) -> Optional[models.Edition]:
...
) -> Optional[models.Edition]: ...


# pylint: disable=arguments-differ
Expand Down Expand Up @@ -91,9 +90,9 @@ def format_search_result(search_result):
title=search_result.title,
key=search_result.remote_id,
author=search_result.author_text,
year=search_result.published_date.year
if search_result.published_date
else None,
year=(
search_result.published_date.year if search_result.published_date else None
),
cover=cover,
confidence=search_result.rank if hasattr(search_result, "rank") else 1,
connector="",
Expand Down
33 changes: 18 additions & 15 deletions bookwyrm/views/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import re

from django.contrib.postgres.search import TrigramSimilarity
from django.contrib.postgres.search import TrigramSimilarity, SearchRank, SearchQuery
from django.core.paginator import Paginator
from django.db.models import F
from django.db.models.functions import Greatest
from django.http import JsonResponse
from django.template.response import TemplateResponse
Expand Down Expand Up @@ -94,26 +95,28 @@ def book_search(request):

def author_search(request):
"""search for an author"""
query = request.GET.get("q")
query = query.strip()
data = {"type": "author", "query": query}
query = request.GET.get("q").strip()
search_query = SearchQuery(query, config="simple")
min_confidence = 0

results = (
models.Author.objects.annotate(
similarity=TrigramSimilarity("name", query),
)
.filter(
similarity__gt=0.1,
)
.order_by("-similarity")
models.Author.objects.filter(search_vector=search_query)
.annotate(rank=SearchRank(F("search_vector"), search_query))
.filter(rank__gt=min_confidence)
.order_by("-rank")
)

paginated = Paginator(results, PAGE_LENGTH)
page = paginated.get_page(request.GET.get("page"))
data["results"] = page
data["page_range"] = paginated.get_elided_page_range(
page.number, on_each_side=2, on_ends=1
)

data = {
"type": "author",
"query": query,
"results": page,
"page_range": paginated.get_elided_page_range(
page.number, on_each_side=2, on_ends=1
),
}
return TemplateResponse(request, "search/author.html", data)


Expand Down

0 comments on commit 5809935

Please sign in to comment.