Consolidate scopes and extract search_fields
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user