diff --git a/app/controllers/searches_controller.rb b/app/controllers/searches_controller.rb index 221218d..735da0c 100644 --- a/app/controllers/searches_controller.rb +++ b/app/controllers/searches_controller.rb @@ -20,7 +20,7 @@ class SearchesController < ApplicationController private def set_messages if query.present? - @messages = Current.user.reachable_messages.search(query).with_presentation.last_page_of(100) + @messages = Current.user.reachable_messages.search(query).with_presentation.last_page_of_matches(100) else @messages = Message.none end diff --git a/app/models/message/searchable.rb b/app/models/message/searchable.rb index 58ea47f..749180f 100644 --- a/app/models/message/searchable.rb +++ b/app/models/message/searchable.rb @@ -9,6 +9,14 @@ module Message::Searchable 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 diff --git a/test/models/message/searchable_test.rb b/test/models/message/searchable_test.rb index d55bedf..6a8f4ab 100644 --- a/test/models/message/searchable_test.rb +++ b/test/models/message/searchable_test.rb @@ -20,6 +20,21 @@ class Message::SearchableTest < ActiveSupport::TestCase assert_equal messages, rooms(:designers).messages.search("cat") end + test "the last page of matches holds the newest ones, read off the index without sorting every match" do + messages = [ "first cat", "second cat", "third cat" ].map do |body| + rooms(:designers).messages.create! body: body, client_message_id: body, creator: users(:david) + end + + queries = [] + collect = ->(*, payload) { queries << payload[:sql] if payload[:sql].include?("message_search_index") } + page = ActiveSupport::Notifications.subscribed(collect, "sql.active_record") do + rooms(:designers).messages.search("cat").last_page_of_matches(2) + end + + assert_equal messages.last(2), page + assert_no_match(/TEMP B-TREE/, Message.connection.select_rows("EXPLAIN QUERY PLAN #{queries.sole}").map(&:last).join(" | ")) + end + test "rich text body is converted to plain text for indexing" do message = rooms(:designers).messages.create! body: "My hovercraft is full of eels", client_message_id: "earth", creator: users(:david)