From 2de35f3fc7ef1a1418ed4638cce89d2b7dfae999 Mon Sep 17 00:00:00 2001 From: Bart Schuurmans Date: Wed, 20 Mar 2024 16:14:26 +0100 Subject: [PATCH] Calculate Author search vector with name and aliases --- .../migrations/0197_author_search_vector.py | 41 +++++++++++++++++++ bookwyrm/models/author.py | 21 +++++++++- 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 bookwyrm/migrations/0197_author_search_vector.py diff --git a/bookwyrm/migrations/0197_author_search_vector.py b/bookwyrm/migrations/0197_author_search_vector.py new file mode 100644 index 000000000..baa540cc0 --- /dev/null +++ b/bookwyrm/migrations/0197_author_search_vector.py @@ -0,0 +1,41 @@ +# Generated by Django 3.2.25 on 2024-03-20 15:15 + +import django.contrib.postgres.indexes +from django.db import migrations +import pgtrigger.compiler +import pgtrigger.migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ("bookwyrm", "0196_merge_pr3134_into_main"), + ] + + operations = [ + migrations.AddIndex( + model_name="author", + index=django.contrib.postgres.indexes.GinIndex( + fields=["search_vector"], name="bookwyrm_au_search__b050a8_gin" + ), + ), + pgtrigger.migrations.AddTrigger( + model_name="author", + trigger=pgtrigger.compiler.Trigger( + name="update_search_vector_on_author_edit", + sql=pgtrigger.compiler.UpsertTriggerSql( + func="new.search_vector := setweight(to_tsvector('simple', new.name), 'A') || setweight(to_tsvector('simple', coalesce(array_to_string(new.aliases, ' '), '')), 'B');RETURN NEW;", + hash="b97919016236d74d0ade51a0769a173ea269da64", + operation='INSERT OR UPDATE OF "name", "aliases", "search_vector"', + pgid="pgtrigger_update_search_vector_on_author_edit_c61cb", + table="bookwyrm_author", + when="BEFORE", + ), + ), + ), + migrations.RunSQL( + # Calculate search vector for all Authors. + sql="UPDATE bookwyrm_author SET search_vector = NULL;", + reverse_sql="UPDATE bookwyrm_author SET search_vector = NULL;", + ), + ] diff --git a/bookwyrm/models/author.py b/bookwyrm/models/author.py index 154b00ccb..9c3621c3d 100644 --- a/bookwyrm/models/author.py +++ b/bookwyrm/models/author.py @@ -3,6 +3,7 @@ import re from typing import Tuple, Any from django.db import models +from django.contrib.postgres.indexes import GinIndex import pgtrigger from bookwyrm import activitypub @@ -71,7 +72,25 @@ class Author(BookDataModel): class Meta: """sets up indexes and triggers""" + # pylint: disable=line-too-long + + indexes = (GinIndex(fields=["search_vector"]),) triggers = [ + pgtrigger.Trigger( + name="update_search_vector_on_author_edit", + when=pgtrigger.Before, + operation=pgtrigger.Insert + | pgtrigger.UpdateOf("name", "aliases", "search_vector"), + func=format_trigger( + """new.search_vector := + -- author name, with priority A + setweight(to_tsvector('simple', new.name), 'A') || + -- author aliases, with priority B + setweight(to_tsvector('simple', coalesce(array_to_string(new.aliases, ' '), '')), 'B'); + RETURN new; + """ + ), + ), pgtrigger.Trigger( name="reset_search_vector_on_author_edit", when=pgtrigger.After, @@ -89,7 +108,7 @@ class Author(BookDataModel): RETURN new; """ ), - ) + ), ] activity_serializer = activitypub.Author