mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-26 19:41:11 +00:00
Use triagram similarity for user search
This commit is contained in:
parent
aa2e4da6f0
commit
7a9d6099fc
2 changed files with 19 additions and 8 deletions
15
bookwyrm/migrations/0053_auto_20201014_1700.py
Normal file
15
bookwyrm/migrations/0053_auto_20201014_1700.py
Normal 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()
|
||||
]
|
|
@ -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 = {
|
||||
|
|
Loading…
Reference in a new issue