diff options
| author | erdgeist <erdgeist@erdgeist.org> | 2026-06-26 22:11:52 +0200 |
|---|---|---|
| committer | erdgeist <erdgeist@erdgeist.org> | 2026-06-26 22:11:52 +0200 |
| commit | c2b3f184df4a60a872eea77971cd83ea6d8297b0 (patch) | |
| tree | 9d0b4533679fc45d992ae1354c652221bffd8667 | |
| parent | 289ec85be338c6c9085497820f53f57ee714b9b7 (diff) | |
Enable full-text search via PostgreSQL tsvector on page_translations
- Restore search vector migration (was parked in doc/ pending PostgreSQL upgrade)
- Restore Node.search using plainto_tsquery with simple dictionary
- Cross-locale keyword search, no stemming, works for both de and en content
| -rw-r--r-- | app/models/node.rb | 10 | ||||
| -rw-r--r-- | db/migrate/20260626025705_add_search_vector_to_page_translations.rb | 47 |
2 files changed, 53 insertions, 4 deletions
diff --git a/app/models/node.rb b/app/models/node.rb index 41c3867..a3ef23a 100644 --- a/app/models/node.rb +++ b/app/models/node.rb | |||
| @@ -211,11 +211,13 @@ class Node < ApplicationRecord | |||
| 211 | self.created_at < new_id_format_date ? unique_path : id | 211 | self.created_at < new_id_format_date ? unique_path : id |
| 212 | end | 212 | end |
| 213 | 213 | ||
| 214 | # TODO: restore full-text search once PostgreSQL is upgraded. | 214 | # Full-text search across all locale translations using PostgreSQL tsvector. |
| 215 | # The tsvector/plpgsql approach requires PostgreSQL 10+ with plpgsql available. | 215 | # Uses 'simple' dictionary (no stemming, no stopwords) so queries work |
| 216 | # For now, search is disabled to unblock the Rails 7.2 upgrade. | 216 | # across German and English content without language detection. |
| 217 | def self.search(term, _ = {}) | 217 | def self.search(term, _ = {}) |
| 218 | none | 218 | joins(head: :translations) |
| 219 | .where("page_translations.search_vector @@ plainto_tsquery('simple', ?)", term) | ||
| 220 | .distinct | ||
| 219 | end | 221 | end |
| 220 | 222 | ||
| 221 | protected | 223 | protected |
diff --git a/db/migrate/20260626025705_add_search_vector_to_page_translations.rb b/db/migrate/20260626025705_add_search_vector_to_page_translations.rb new file mode 100644 index 0000000..0747637 --- /dev/null +++ b/db/migrate/20260626025705_add_search_vector_to_page_translations.rb | |||
| @@ -0,0 +1,47 @@ | |||
| 1 | class AddSearchVectorToPageTranslations < ActiveRecord::Migration[7.2] | ||
| 2 | def up | ||
| 3 | add_column :page_translations, :search_vector, :tsvector | ||
| 4 | |||
| 5 | execute <<~SQL | ||
| 6 | UPDATE page_translations | ||
| 7 | SET search_vector = to_tsvector( | ||
| 8 | 'simple', | ||
| 9 | coalesce(title, '') || ' ' || | ||
| 10 | coalesce(abstract, '') || ' ' || | ||
| 11 | coalesce(body, '') | ||
| 12 | ) | ||
| 13 | SQL | ||
| 14 | |||
| 15 | add_index :page_translations, :search_vector, | ||
| 16 | using: :gin, | ||
| 17 | name: 'index_page_translations_on_search_vector' | ||
| 18 | |||
| 19 | execute <<~SQL | ||
| 20 | CREATE OR REPLACE FUNCTION page_translations_search_vector_update() | ||
| 21 | RETURNS trigger AS $$ | ||
| 22 | BEGIN | ||
| 23 | NEW.search_vector := to_tsvector( | ||
| 24 | 'simple', | ||
| 25 | coalesce(NEW.title, '') || ' ' || | ||
| 26 | coalesce(NEW.abstract, '') || ' ' || | ||
| 27 | coalesce(NEW.body, '') | ||
| 28 | ); | ||
| 29 | RETURN NEW; | ||
| 30 | END; | ||
| 31 | $$ LANGUAGE plpgsql; | ||
| 32 | |||
| 33 | CREATE TRIGGER page_translations_search_vector_trigger | ||
| 34 | BEFORE INSERT OR UPDATE ON page_translations | ||
| 35 | FOR EACH ROW EXECUTE PROCEDURE page_translations_search_vector_update(); | ||
| 36 | SQL | ||
| 37 | end | ||
| 38 | |||
| 39 | def down | ||
| 40 | execute <<~SQL | ||
| 41 | DROP TRIGGER IF EXISTS page_translations_search_vector_trigger ON page_translations; | ||
| 42 | DROP FUNCTION IF EXISTS page_translations_search_vector_update(); | ||
| 43 | SQL | ||
| 44 | remove_index :page_translations, name: 'index_page_translations_on_search_vector' | ||
| 45 | remove_column :page_translations, :search_vector | ||
| 46 | end | ||
| 47 | end | ||
