Fixes search query syntax

mad about the lack of django docs on this, jeez
This commit is contained in:
Mouse Reeve 2021-06-26 16:20:18 -07:00
parent 789f823d5d
commit 6470033ac9
3 changed files with 5 additions and 4 deletions

View file

@ -143,7 +143,7 @@ def search_title_author(query, min_confidence, *filters):
"""searches for title and author"""
query = SearchQuery(query, config="simple") | SearchQuery(query, config="english")
results = (
models.Edition.objects.annotate(rank=SearchRank("search_vector", query))
models.Edition.objects.annotate(rank=SearchRank(F("search_vector"), query))
.filter(*filters, search_vector=query, rank__gt=min_confidence)
.order_by("-rank")
)

View file

@ -44,7 +44,7 @@ class Migration(migrations.Migration):
setweight(to_tsvector('simple', coalesce(new.title, '')), 'A')
) ||
setweight(to_tsvector('english', coalesce(new.subtitle, '')), 'B') ||
(SELECT setweight(to_tsvector('simple', coalesce(array_to_string(array_agg(bookwyrm_author.name), ' '), '')), 'B')
(SELECT setweight(to_tsvector('simple', coalesce(array_to_string(array_agg(bookwyrm_author.name), ' '), '')), 'C')
FROM bookwyrm_book
LEFT OUTER JOIN bookwyrm_book_authors
ON bookwyrm_book.id = bookwyrm_book_authors.book_id

View file

@ -70,15 +70,16 @@ class SelfConnector(TestCase):
# title is rank A
models.Edition.objects.create(title="Anonymous")
# doesn't rank in this search
edition = models.Edition.objects.create(
models.Edition.objects.create(
title="An Edition", parent_work=models.Work.objects.create(title="")
)
results = self.connector.search("Anonymous")
self.assertEqual(len(results), 3)
self.assertEqual(len(results), 4)
self.assertEqual(results[0].title, "Anonymous")
self.assertEqual(results[1].title, "More Editions")
self.assertEqual(results[2].title, "Edition of Example Work")
self.assertEqual(results[3].title, "Another Edition")
@patch("bookwyrm.preview_images.generate_edition_preview_image_task.delay")
def test_search_multiple_editions(self, _):