Skip to content

Commit

Permalink
Support tsearch followed by operator (<->)
Browse files Browse the repository at this point in the history
  • Loading branch information
nurey committed Sep 5, 2019
1 parent a8c3ac5 commit 4188ef7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
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
23 changes: 23 additions & 0 deletions spec/integration/pg_search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,29 @@
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

0 comments on commit 4188ef7

Please sign in to comment.