From 463a5f089a5851f560aae09f4d821aedb748466c Mon Sep 17 00:00:00 2001 From: Donal McBreen Date: Fri, 21 Nov 2025 12:31:25 +0000 Subject: [PATCH] Consolidate scopes and extract search_fields --- app/models/search/record.rb | 9 ++------- app/models/search/record/sqlite.rb | 23 ++++++----------------- app/models/search/record/trilogy.rb | 14 +++++++------- 3 files changed, 15 insertions(+), 31 deletions(-) diff --git a/app/models/search/record.rb b/app/models/search/record.rb index 5f1f19bc2..52bc80522 100644 --- a/app/models/search/record.rb +++ b/app/models/search/record.rb @@ -23,20 +23,15 @@ class Search::Record < ApplicationRecord end end - scope :matching, ->(query, account_id) do - matching_scope(query, account_id) - end - scope :for_user, ->(user) do where(account_id: user.account_id, board_id: user.board_ids) end scope :search, ->(query:, user:) do - relation = for_query(query: query, user: user) + for_query(query: query, user: user) .includes(:searchable, card: [ :board, :creator ]) .order(created_at: :desc) - - search_scope(relation, query) + .select(:id, :account_id, :searchable_type, :searchable_id, :card_id, :board_id, :title, :content, :created_at, *(search_fields(query))) end def source diff --git a/app/models/search/record/sqlite.rb b/app/models/search/record/sqlite.rb index 989f8d834..e3ffcf3bf 100644 --- a/app/models/search/record/sqlite.rb +++ b/app/models/search/record/sqlite.rb @@ -8,34 +8,23 @@ module Search::Record::SQLite after_save :upsert_to_fts5_table after_destroy :delete_from_fts5_table - end - class_methods do - def matching_scope(query, account_id) + scope :matching, ->(query, account_id) do joins("INNER JOIN search_records_fts ON search_records_fts.rowid = #{table_name}.id") .where("search_records_fts MATCH ?", query) end + end - def search_scope(relation, query) + class_methods do + def search_fields(query) opening_mark = connection.quote(Search::Highlighter::OPENING_MARK) closing_mark = connection.quote(Search::Highlighter::CLOSING_MARK) ellipsis = connection.quote(Search::Highlighter::ELIPSIS) - relation.select( - "#{table_name}.id", - "#{table_name}.account_id", - "#{table_name}.searchable_type", - "#{table_name}.searchable_id", - "#{table_name}.card_id", - "#{table_name}.board_id", - "#{table_name}.title", - "#{table_name}.content", - "#{table_name}.created_at", - "highlight(search_records_fts, 0, #{opening_mark}, #{closing_mark}) AS highlighted_title", + [ "highlight(search_records_fts, 0, #{opening_mark}, #{closing_mark}) AS highlighted_title", "highlight(search_records_fts, 1, #{opening_mark}, #{closing_mark}) AS highlighted_content", "snippet(search_records_fts, 1, #{opening_mark}, #{closing_mark}, #{ellipsis}, 20) AS content_snippet", - "#{connection.quote(query.terms)} AS query" - ) + "#{connection.quote(query.terms)} AS query" ] end end diff --git a/app/models/search/record/trilogy.rb b/app/models/search/record/trilogy.rb index 269bb6dab..e598c62e5 100644 --- a/app/models/search/record/trilogy.rb +++ b/app/models/search/record/trilogy.rb @@ -5,6 +5,11 @@ module Search::Record::Trilogy included do before_save :set_account_key, :stem_content + + scope :matching, ->(query, account_id) do + full_query = "+account#{account_id} +(#{Search::Stemmer.stem(query)})" + where("MATCH(#{table_name}.account_key, #{table_name}.content, #{table_name}.title) AGAINST(? IN BOOLEAN MODE)", full_query) + end end class_methods do @@ -20,13 +25,8 @@ module Search::Record::Trilogy Zlib.crc32(account_id.to_s) % SHARD_COUNT end - def matching_scope(query, account_id) - full_query = "+account#{account_id} +(#{Search::Stemmer.stem(query)})" - where("MATCH(#{table_name}.account_key, #{table_name}.content, #{table_name}.title) AGAINST(? IN BOOLEAN MODE)", full_query) - end - - def search_scope(relation, query) - relation.select(:id, :searchable_type, :searchable_id, :card_id, :board_id, :account_id, :created_at, "#{connection.quote(query.terms)} AS query") + def search_fields(query) + "#{connection.quote(query.terms)} AS query" end end