525e53a2f7
Needs some special handling for the FTS virtual table format. Set the primary key to rowid and manually specify the columns.
60 lines
1.9 KiB
Ruby
60 lines
1.9 KiB
Ruby
module Search::Record::SQLite
|
|
extend ActiveSupport::Concern
|
|
|
|
included do
|
|
# Override default UUID id attribute, as FTS5 uses rowid integer primary key
|
|
attribute :id, :integer, default: nil
|
|
attribute :result_title, :string
|
|
attribute :result_content, :string
|
|
|
|
has_one :search_records_fts, -> { with_rowid }, class_name: "Search::Record::SQLite::Fts", foreign_key: :rowid, primary_key: :id
|
|
|
|
after_save :upsert_to_fts5_table
|
|
after_destroy :delete_from_fts5_table
|
|
|
|
scope :matching, ->(query, account_id) { joins(:search_records_fts).where("search_records_fts MATCH ?", query) }
|
|
end
|
|
|
|
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)
|
|
|
|
[ "highlight(search_records_fts, 0, #{opening_mark}, #{closing_mark}) AS result_title",
|
|
"snippet(search_records_fts, 1, #{opening_mark}, #{closing_mark}, #{ellipsis}, 20) AS result_content",
|
|
"#{connection.quote(query.terms)} AS query" ]
|
|
end
|
|
end
|
|
|
|
def card_title
|
|
escape_fts_highlight(result_title || card.title)
|
|
end
|
|
|
|
def card_description
|
|
escape_fts_highlight(result_content) unless comment
|
|
end
|
|
|
|
def comment_body
|
|
escape_fts_highlight(result_content) if comment
|
|
end
|
|
|
|
private
|
|
def escape_fts_highlight(html)
|
|
return nil unless html.present?
|
|
|
|
CGI.escapeHTML(html)
|
|
.gsub(CGI.escapeHTML(Search::Highlighter::OPENING_MARK), Search::Highlighter::OPENING_MARK.html_safe)
|
|
.gsub(CGI.escapeHTML(Search::Highlighter::CLOSING_MARK), Search::Highlighter::CLOSING_MARK.html_safe)
|
|
.html_safe
|
|
end
|
|
|
|
def upsert_to_fts5_table
|
|
Fts.upsert(id, title, content)
|
|
end
|
|
|
|
def delete_from_fts5_table
|
|
search_records_fts&.destroy
|
|
end
|
|
end
|