Calculate Author search vector with name and aliases

This commit is contained in:
Bart Schuurmans 2024-03-20 16:14:26 +01:00
parent bac52eef3e
commit 2de35f3fc7
2 changed files with 61 additions and 1 deletions

View file

@ -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;",
),
]

View file

@ -3,6 +3,7 @@ import re
from typing import Tuple, Any from typing import Tuple, Any
from django.db import models from django.db import models
from django.contrib.postgres.indexes import GinIndex
import pgtrigger import pgtrigger
from bookwyrm import activitypub from bookwyrm import activitypub
@ -71,7 +72,25 @@ class Author(BookDataModel):
class Meta: class Meta:
"""sets up indexes and triggers""" """sets up indexes and triggers"""
# pylint: disable=line-too-long
indexes = (GinIndex(fields=["search_vector"]),)
triggers = [ 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( pgtrigger.Trigger(
name="reset_search_vector_on_author_edit", name="reset_search_vector_on_author_edit",
when=pgtrigger.After, when=pgtrigger.After,
@ -89,7 +108,7 @@ class Author(BookDataModel):
RETURN new; RETURN new;
""" """
), ),
) ),
] ]
activity_serializer = activitypub.Author activity_serializer = activitypub.Author