mirror of
https://github.com/basecamp/once-campfire.git
synced 2026-10-07 07:10:10 +09:00
edaab3e5d1
Search showed the last 100 matches by created_at, so SQLite collected every message containing the words and sorted them before keeping a page. A common word in a large account meant sorting most of its history on every search. Ordering by the full-text index's rowid, which is the message id, lets SQLite walk the index from the newest match and stop once the page is full. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JVFo3Lt9T8M5NR7KxVvsZ2
37 lines
1.2 KiB
Ruby
37 lines
1.2 KiB
Ruby
module Message::Searchable
|
|
extend ActiveSupport::Concern
|
|
|
|
included do
|
|
after_create_commit :create_in_index
|
|
after_update_commit :update_in_index
|
|
after_destroy_commit :remove_from_index
|
|
|
|
scope :search, ->(query) { joins("join message_search_index idx on messages.id = idx.rowid").where("idx.body match ?", query).ordered }
|
|
end
|
|
|
|
class_methods do
|
|
# Orders by the index's rowid, which is the message id, so SQLite walks the full-text index
|
|
# newest first and stops at the page. Ordering by created_at sorted every match before paging.
|
|
def last_page_of_matches(size)
|
|
Message::Pagination::Page.load(reorder("idx.rowid"), :last, size)
|
|
end
|
|
end
|
|
|
|
private
|
|
def create_in_index
|
|
execute_sql_with_binds "insert into message_search_index(rowid, body) values (?, ?)", id, plain_text_body
|
|
end
|
|
|
|
def update_in_index
|
|
execute_sql_with_binds "update message_search_index set body = ? where rowid = ?", plain_text_body, id
|
|
end
|
|
|
|
def remove_from_index
|
|
execute_sql_with_binds "delete from message_search_index where rowid = ?", id
|
|
end
|
|
|
|
def execute_sql_with_binds(*statement)
|
|
self.class.connection.execute self.class.sanitize_sql(statement)
|
|
end
|
|
end
|