From 7a9d6099fcd2c5c64e6846077b45c5e24cab9487 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 14 Oct 2020 10:04:03 -0700 Subject: [PATCH] Use triagram similarity for user search --- bookwyrm/migrations/0053_auto_20201014_1700.py | 15 +++++++++++++++ bookwyrm/views.py | 12 ++++-------- 2 files changed, 19 insertions(+), 8 deletions(-) create mode 100644 bookwyrm/migrations/0053_auto_20201014_1700.py diff --git a/bookwyrm/migrations/0053_auto_20201014_1700.py b/bookwyrm/migrations/0053_auto_20201014_1700.py new file mode 100644 index 000000000..d1306dfef --- /dev/null +++ b/bookwyrm/migrations/0053_auto_20201014_1700.py @@ -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() + ] diff --git a/bookwyrm/views.py b/bookwyrm/views.py index f659db364..9217c4b3e 100644 --- a/bookwyrm/views.py +++ b/bookwyrm/views.py @@ -2,7 +2,7 @@ import re 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.http import HttpResponseBadRequest, HttpResponseNotFound,\ JsonResponse @@ -159,15 +159,11 @@ def search(request): outgoing.handle_remote_webfinger(query) # do a local user search - vector = SearchVector('localname', weight='A') + \ - SearchVector('username', wieght='A') user_results = models.User.objects.annotate( - search=vector - ).annotate( - rank=SearchRank(vector, query) + similarity=TrigramSimilarity('username', query), ).filter( - rank__gt=0 - ).order_by('-rank')[:10] + similarity__gt=0.1, + ).order_by('-similarity')[:10] book_results = books_manager.search(query) data = {