Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for trigram strict_word_similarity function #539

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions lib/pg_search/features/trigram.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module PgSearch
module Features
class Trigram < Feature
def self.valid_options
super + %i[threshold word_similarity]
super + %i[threshold word_similarity strict_word_similarity]
end

def conditions
Expand All @@ -29,23 +29,31 @@ def rank

private

def strict_word_similarity?
options[:strict_word_similarity]
end

def word_similarity?
options[:word_similarity]
end

def similarity_function
if word_similarity?
"word_similarity"
if strict_word_similarity?
'strict_word_similarity'
elsif word_similarity?
'word_similarity'
else
"similarity"
'similarity'
end
end

def infix_operator
if word_similarity?
"<%"
if strict_word_similarity?
'<<%'
elsif word_similarity?
'<%'
else
"%"
'%'
end
end

Expand Down
23 changes: 23 additions & 0 deletions spec/lib/pg_search/features/trigram_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@
end
end

context "when searching by strict_word_similarity" do
let(:options) do
{strict_word_similarity: true}
end

it 'uses the "<<%" operator when searching by word_similarity' do
config.ignore = []
expect(feature.conditions.to_sql).to eq("('#{query}' <<% (#{coalesced_columns}))")
end
end

context "when ignoring accents" do
it "escapes the search document and query, but not the accent function" do
config.ignore = [:accents]
Expand Down Expand Up @@ -81,6 +92,18 @@
)
end
end

context "when searching by strict_word_similarity" do
let(:options) do
{threshold: 0.5, strict_word_similarity: true}
end

it 'uses a minimum similarity expression instead of the "<<%" operator' do
expect(feature.conditions.to_sql).to eq(
"(strict_word_similarity('#{query}', (#{coalesced_columns})) >= 0.5)"
)
end
end
end

context "when only certain columns are selected" do
Expand Down