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

Limit highlighted attributes when searching against multiple fields #375

Open
wants to merge 3 commits 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
16 changes: 15 additions & 1 deletion lib/pg_search/features/feature.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module PgSearch
module Features
class Feature
def self.valid_options
%i[only sort_only]
%i[only sort_only highlight_only]
end

delegate :connection, :quoted_table_name, :to => :'@model'
Expand Down Expand Up @@ -36,6 +36,20 @@ def columns
end
end

def highlight_document
highlight_columns.map { |column| column.to_sql }.join(" || ' ' || ")
end

def highlight_columns
if options[:highlight_only]
columns.select do |column|
Array.wrap(options[:highlight_only]).map(&:to_s).include? column.name
end
else
columns
end
end

def normalize(expression)
normalizer.add_normalization(expression)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pg_search/features/tsearch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def highlight
def ts_headline
Arel::Nodes::NamedFunction.new("ts_headline", [
dictionary,
arel_wrap(document),
arel_wrap(highlight_document),
arel_wrap(tsquery),
Arel::Nodes.build_quoted(ts_headline_options)
]).to_sql
Expand Down
63 changes: 63 additions & 0 deletions spec/lib/pg_search/features/tsearch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,69 @@

expect(highlight_sql).to eq(expected_sql)
end

context "when options[:highlight_only] has options set" do
it "passes the options to ts_headline" do
query = "query"
columns = [
PgSearch::Configuration::Column.new(:name, nil, Model),
PgSearch::Configuration::Column.new(:content, nil, Model)
]
options = {
highlight: {
StartSel: '<start class="search">',
StopSel: '<stop>',
MaxWords: 123,
MinWords: 456,
ShortWord: 4,
HighlightAll: true,
MaxFragments: 3,
FragmentDelimiter: '&hellip;'
},
highlight_only: [:content]
}

config = double(:config, :ignore => [])
normalizer = PgSearch::Normalizer.new(config)

feature = described_class.new(query, options, columns, Model, normalizer)

expected_sql = %{(ts_headline('simple', (coalesce(#{Model.quoted_table_name}."content"::text, '')), (to_tsquery('simple', ''' ' || 'query' || ' ''')), 'StartSel = "<start class=""search"">", StopSel = "<stop>", MaxFragments = 3, MaxWords = 123, MinWords = 456, ShortWord = 4, FragmentDelimiter = "&hellip;", HighlightAll = TRUE'))}

expect(feature.highlight.to_sql).to eq(expected_sql)
end

it "passes deprecated options to ts_headline" do
query = "query"
columns = [
PgSearch::Configuration::Column.new(:name, nil, Model),
PgSearch::Configuration::Column.new(:content, nil, Model),
]
options = {
highlight: {
start_sel: '<start class="search">',
stop_sel: '<stop>',
max_words: 123,
min_words: 456,
short_word: 4,
highlight_all: false,
max_fragments: 3,
fragment_delimiter: '&hellip;'
},
highlight_only: [:content]
}

config = double(:config, :ignore => [])
normalizer = PgSearch::Normalizer.new(config)

feature = described_class.new(query, options, columns, Model, normalizer)

highlight_sql = ActiveSupport::Deprecation.silence { feature.highlight.to_sql }
expected_sql = %{(ts_headline('simple', (coalesce(#{Model.quoted_table_name}."content"::text, '')), (to_tsquery('simple', ''' ' || 'query' || ' ''')), 'StartSel = "<start class=""search"">", StopSel = "<stop>", MaxFragments = 3, MaxWords = 123, MinWords = 456, ShortWord = 4, FragmentDelimiter = "&hellip;", HighlightAll = FALSE'))}

expect(highlight_sql).to eq(expected_sql)
end
end
end
end
end