mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-22 01:21:07 +00:00
Switch author search from TrigramSimilarity to SearchQuery
This commit is contained in:
parent
0795b4d171
commit
36222afa79
1 changed files with 18 additions and 15 deletions
|
@ -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
|
||||
|
@ -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)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue