Skip to content

Commit 9cb7a22

Browse files
committed
Support tsearch followed by operator (<->)
1 parent a8c3ac5 commit 9cb7a22

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

lib/pg_search/features/tsearch.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module PgSearch
77
module Features
88
class TSearch < Feature # rubocop:disable Metrics/ClassLength
99
def self.valid_options
10-
super + %i[dictionary prefix negation any_word normalization tsvector_column highlight]
10+
super + %i[dictionary prefix negation any_word followed_by normalization tsvector_column highlight]
1111
end
1212

1313
def conditions
@@ -133,7 +133,14 @@ def tsquery
133133

134134
query_terms = query.split(" ").compact
135135
tsquery_terms = query_terms.map { |term| tsquery_for_term(term) }
136-
tsquery_terms.join(options[:any_word] ? ' || ' : ' && ')
136+
join_op = if options[:any_word]
137+
' || '
138+
elsif options[:followed_by]
139+
' <-> '
140+
else
141+
' && '
142+
end
143+
tsquery_terms.join(join_op)
137144
end
138145

139146
def tsdocument

spec/integration/pg_search_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,29 @@
785785
end
786786
end
787787

788+
context "searching followed_by option" do
789+
before do
790+
ModelWithPgSearch.pg_search_scope :search_title_with_followed_by,
791+
against: :title,
792+
using: {
793+
tsearch: { followed_by: true }
794+
}
795+
end
796+
797+
it "returns all results containing word followed by another word in their title" do
798+
numbers = %w[one two three four].each_cons(2) { |word_sequence| ModelWithPgSearch.create!(title: word_sequence.join(' ')) }
799+
800+
results = ModelWithPgSearch.search_title_with_followed_by("one two")
801+
802+
expect(results.map(&:title)).to eq(["one two"])
803+
804+
results = ModelWithPgSearch.search_title_with_followed_by("one three")
805+
806+
expect(results.map(&:title)).to eq([])
807+
end
808+
809+
end
810+
788811
context "with :negation" do
789812
before do
790813
ModelWithPgSearch.pg_search_scope :search_with_negation,

0 commit comments

Comments
 (0)