mirror of
https://github.com/basecamp/once-campfire.git
synced 2026-10-24 06:36:48 +09:00
Rewrite a message's search entry only when its body is saved
Boosting and unboosting touch the message, so after_update_commit rewrote its row in the full-text index with the same text, loading the rich text again to rebuild it, on every boost. The index only needs a new row when the rich text body was saved. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0142qgjggdJ2KDdGk7RF9Xm9
This commit is contained in:
1 parent
2393f01ba2
commit
78a84a6ab7
2 files changed
+31
-1
No files matched your search
@@ -3,7 +3,7 @@ module Message::Searchable
|
|||||||
|
|
||||||
included do
|
included do
|
||||||
after_create_commit :create_in_index
|
after_create_commit :create_in_index
|
||||||
after_update_commit :update_in_index
|
after_update_commit :update_in_index, if: :body_saved?
|
||||||
after_destroy_commit :remove_from_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 ?", match_terms(query)).ordered }
|
scope :search, ->(query) { joins("join message_search_index idx on messages.id = idx.rowid").where("idx.body match ?", match_terms(query)).ordered }
|
||||||
@@ -22,6 +22,11 @@ module Message::Searchable
|
|||||||
execute_sql_with_binds "insert into message_search_index(rowid, body) values (?, ?)", id, plain_text_body
|
execute_sql_with_binds "insert into message_search_index(rowid, body) values (?, ?)", id, plain_text_body
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Boosts touch their message too: rewrite the index only when the body was saved.
|
||||||
|
def body_saved?
|
||||||
|
association(:rich_text_body).target&.saved_change_to_body?
|
||||||
|
end
|
||||||
|
|
||||||
def update_in_index
|
def update_in_index
|
||||||
execute_sql_with_binds "update message_search_index set body = ? where rowid = ?", plain_text_body, id
|
execute_sql_with_binds "update message_search_index set body = ? where rowid = ?", plain_text_body, id
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
require "test_helper"
|
require "test_helper"
|
||||||
|
require "active_record/testing/query_assertions"
|
||||||
|
|
||||||
class Message::SearchableTest < ActiveSupport::TestCase
|
class Message::SearchableTest < ActiveSupport::TestCase
|
||||||
|
include ActionDispatch::TestProcess, ActiveRecord::Assertions::QueryAssertions
|
||||||
|
|
||||||
test "message body is indexed and searchable" do
|
test "message body is indexed and searchable" do
|
||||||
message = rooms(:designers).messages.create! body: "My hovercraft is full of eels", client_message_id: "earth", creator: users(:david)
|
message = rooms(:designers).messages.create! body: "My hovercraft is full of eels", client_message_id: "earth", creator: users(:david)
|
||||||
assert_equal [ message ], rooms(:designers).messages.search("eel")
|
assert_equal [ message ], rooms(:designers).messages.search("eel")
|
||||||
@@ -12,6 +15,28 @@ class Message::SearchableTest < ActiveSupport::TestCase
|
|||||||
assert_equal [], rooms(:designers).messages.search("sharks")
|
assert_equal [], rooms(:designers).messages.search("sharks")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "boosting a message leaves the search index alone, whether or not its body is loaded" do
|
||||||
|
attachment_message = rooms(:designers).messages.create! attachment: fixture_file_upload("moon.jpg", "image/jpeg"), client_message_id: "moon", creator: users(:david)
|
||||||
|
|
||||||
|
[ messages(:first), Message.with_rich_text_body.find(messages(:first).id), Message.with_rich_text_body.find(attachment_message.id) ].each do |message|
|
||||||
|
assert_no_queries_match(/message_search_index/) do
|
||||||
|
message.boosts.create!(content: "🦞", booster: users(:jason)).destroy!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
test "saving a new body replaces the old words in the index" do
|
||||||
|
message = rooms(:designers).messages.create! body: "My hovercraft is full of eels", client_message_id: "earth", creator: users(:david)
|
||||||
|
|
||||||
|
Message.find(message.id).update! body: "My hovercraft is full of sharks"
|
||||||
|
assert_equal [ message ], rooms(:designers).messages.search("sharks")
|
||||||
|
assert_equal [], rooms(:designers).messages.search("eels")
|
||||||
|
|
||||||
|
Message.find(message.id).body.update! body: "My hovercraft is full of whales"
|
||||||
|
assert_equal [ message ], rooms(:designers).messages.search("whales")
|
||||||
|
assert_equal [], rooms(:designers).messages.search("sharks")
|
||||||
|
end
|
||||||
|
|
||||||
test "search results are returned in message order" do
|
test "search results are returned in message order" do
|
||||||
messages = [ "first cat", "second cat", "third cat", "cat cat cat" ].map do |body|
|
messages = [ "first cat", "second cat", "third cat", "cat cat cat" ].map do |body|
|
||||||
rooms(:designers).messages.create! body: body, client_message_id: body, creator: users(:david)
|
rooms(:designers).messages.create! body: body, client_message_id: body, creator: users(:david)
|
||||||
|
|||||||
Reference in new issue
Block a user