mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-29 04:51:11 +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
|
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.core.paginator import Paginator
|
||||||
|
from django.db.models import F
|
||||||
from django.db.models.functions import Greatest
|
from django.db.models.functions import Greatest
|
||||||
from django.http import JsonResponse
|
from django.http import JsonResponse
|
||||||
from django.template.response import TemplateResponse
|
from django.template.response import TemplateResponse
|
||||||
|
@ -94,26 +95,28 @@ def book_search(request):
|
||||||
|
|
||||||
def author_search(request):
|
def author_search(request):
|
||||||
"""search for an author"""
|
"""search for an author"""
|
||||||
query = request.GET.get("q")
|
query = request.GET.get("q").strip()
|
||||||
query = query.strip()
|
search_query = SearchQuery(query, config="simple")
|
||||||
data = {"type": "author", "query": query}
|
min_confidence = 0
|
||||||
|
|
||||||
results = (
|
results = (
|
||||||
models.Author.objects.annotate(
|
models.Author.objects.filter(search_vector=search_query)
|
||||||
similarity=TrigramSimilarity("name", query),
|
.annotate(rank=SearchRank(F("search_vector"), search_query))
|
||||||
)
|
.filter(rank__gt=min_confidence)
|
||||||
.filter(
|
.order_by("-rank")
|
||||||
similarity__gt=0.1,
|
|
||||||
)
|
|
||||||
.order_by("-similarity")
|
|
||||||
)
|
)
|
||||||
|
|
||||||
paginated = Paginator(results, PAGE_LENGTH)
|
paginated = Paginator(results, PAGE_LENGTH)
|
||||||
page = paginated.get_page(request.GET.get("page"))
|
page = paginated.get_page(request.GET.get("page"))
|
||||||
data["results"] = page
|
|
||||||
data["page_range"] = paginated.get_elided_page_range(
|
data = {
|
||||||
|
"type": "author",
|
||||||
|
"query": query,
|
||||||
|
"results": page,
|
||||||
|
"page_range": paginated.get_elided_page_range(
|
||||||
page.number, on_each_side=2, on_ends=1
|
page.number, on_each_side=2, on_ends=1
|
||||||
)
|
),
|
||||||
|
}
|
||||||
return TemplateResponse(request, "search/author.html", data)
|
return TemplateResponse(request, "search/author.html", data)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue