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

Support tsearch followed by operator (<->) #417

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
12 changes: 10 additions & 2 deletions lib/pg_search/features/tsearch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module PgSearch
module Features
class TSearch < Feature # rubocop:disable Metrics/ClassLength
def self.valid_options
super + %i[dictionary prefix negation any_word normalization tsvector_column highlight]
super + %i[dictionary prefix negation any_word followed_by normalization tsvector_column highlight]
end

def conditions
Expand Down Expand Up @@ -133,7 +133,15 @@ def tsquery

query_terms = query.split(" ").compact
tsquery_terms = query_terms.map { |term| tsquery_for_term(term) }
tsquery_terms.join(options[:any_word] ? ' || ' : ' && ')
join_op =
if options[:any_word]
' || '
elsif options[:followed_by]
' <-> '
else
' && '
end
tsquery_terms.join(join_op)
end

def tsdocument
Expand Down
22 changes: 22 additions & 0 deletions spec/integration/pg_search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,28 @@
end
end

context "searching followed_by option" do
before do
ModelWithPgSearch.pg_search_scope :search_title_with_followed_by,
against: :title,
using: {
tsearch: { followed_by: true }
}
end

it "returns all results containing word followed by another word in their title" do
numbers = %w[one two three four].each_cons(2) { |word_sequence| ModelWithPgSearch.create!(title: word_sequence.join(' ')) }

results = ModelWithPgSearch.search_title_with_followed_by("one two")

expect(results.map(&:title)).to eq(["one two"])

results = ModelWithPgSearch.search_title_with_followed_by("one three")

expect(results.map(&:title)).to eq([])
end
end

context "with :negation" do
before do
ModelWithPgSearch.pg_search_scope :search_with_negation,
Expand Down