Skip to content

Commit

Permalink
Don't search using trigrams by default
Browse files Browse the repository at this point in the history
  • Loading branch information
elektronaut committed Oct 22, 2024
1 parent 520e8a1 commit 329e706
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
23 changes: 16 additions & 7 deletions app/models/search_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ class SearchDocument < ApplicationRecord

pg_search_scope :full_text_search_scope, lambda { |query, dictionary|
{ against: %i[name description content tags],
using: { tsearch: { prefix: true,
dictionary:,
tsvector_column: "tsv" },
using: { tsearch: { prefix: true, dictionary:, tsvector_column: "tsv" } },
ignoring: :accents,
order_within_rank: "search_documents.record_updated_at DESC",
query: }
}

pg_search_scope :trigram_search_scope, lambda { |query, dictionary|
{ against: %i[name description content tags],
using: { tsearch: { prefix: true, dictionary:, tsvector_column: "tsv" },
trigram: { only: %i[name] } },
ignoring: :accents,
order_within_rank: "search_documents.record_updated_at DESC",
Expand All @@ -29,11 +35,14 @@ def results
.map(&:localized_searchable)
end

def search(query, locale: nil)
def search(query, locale: nil, trigram: false)
locale ||= I18n.locale
where(locale:)
.includes(:searchable)
.full_text_search_scope(query, search_configuration(locale))
scope = where(locale:).includes(:searchable)
if trigram
scope.trigram_search_scope(query, search_configuration(locale))
else
scope.full_text_search_scope(query, search_configuration(locale))
end
end

def search_configuration(locale)
Expand Down
6 changes: 4 additions & 2 deletions spec/models/search_document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@

describe ".search" do
def query(str)
described_class.search(str, locale:).results
described_class.search(str, locale:, trigram:).results
end

let(:locale) { :en }
let(:trigram) { false }

describe "when searching name" do
describe "when searching with trigrams" do
let(:trigram) { true }
let!(:page) { create(:page, locale:, name: "foobar walking") }

it "supports trigram similarity matching" do
Expand Down

0 comments on commit 329e706

Please sign in to comment.