Use triagram similarity for user search

This commit is contained in:
Mouse Reeve 2020-10-14 10:04:03 -07:00
parent aa2e4da6f0
commit 7a9d6099fc
2 changed files with 19 additions and 8 deletions

View file

@ -0,0 +1,15 @@
# Generated by Django 3.0.7 on 2020-10-14 17:00
from django.contrib.postgres.operations import TrigramExtension
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('bookwyrm', '0052_auto_20201005_2145'),
]
operations = [
TrigramExtension()
]

View file

@ -2,7 +2,7 @@
import re import re
from django.contrib.auth.decorators import login_required, permission_required from django.contrib.auth.decorators import login_required, permission_required
from django.contrib.postgres.search import SearchRank, SearchVector from django.contrib.postgres.search import TrigramSimilarity
from django.db.models import Avg, Count, Q from django.db.models import Avg, Count, Q
from django.http import HttpResponseBadRequest, HttpResponseNotFound,\ from django.http import HttpResponseBadRequest, HttpResponseNotFound,\
JsonResponse JsonResponse
@ -159,15 +159,11 @@ def search(request):
outgoing.handle_remote_webfinger(query) outgoing.handle_remote_webfinger(query)
# do a local user search # do a local user search
vector = SearchVector('localname', weight='A') + \
SearchVector('username', wieght='A')
user_results = models.User.objects.annotate( user_results = models.User.objects.annotate(
search=vector similarity=TrigramSimilarity('username', query),
).annotate(
rank=SearchRank(vector, query)
).filter( ).filter(
rank__gt=0 similarity__gt=0.1,
).order_by('-rank')[:10] ).order_by('-similarity')[:10]
book_results = books_manager.search(query) book_results = books_manager.search(query)
data = { data = {