diff --git a/lib/pg_search/features/feature.rb b/lib/pg_search/features/feature.rb index 4d738df8..44ec24e9 100644 --- a/lib/pg_search/features/feature.rb +++ b/lib/pg_search/features/feature.rb @@ -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' @@ -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 diff --git a/lib/pg_search/features/tsearch.rb b/lib/pg_search/features/tsearch.rb index fa0b3078..30f7f029 100644 --- a/lib/pg_search/features/tsearch.rb +++ b/lib/pg_search/features/tsearch.rb @@ -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 diff --git a/spec/lib/pg_search/features/tsearch_spec.rb b/spec/lib/pg_search/features/tsearch_spec.rb index c69f8ec9..d9ecadef 100644 --- a/spec/lib/pg_search/features/tsearch_spec.rb +++ b/spec/lib/pg_search/features/tsearch_spec.rb @@ -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: '', + StopSel: '', + MaxWords: 123, + MinWords: 456, + ShortWord: 4, + HighlightAll: true, + MaxFragments: 3, + FragmentDelimiter: '…' + }, + 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 = "", StopSel = "", MaxFragments = 3, MaxWords = 123, MinWords = 456, ShortWord = 4, FragmentDelimiter = "…", 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: '', + stop_sel: '', + max_words: 123, + min_words: 456, + short_word: 4, + highlight_all: false, + max_fragments: 3, + fragment_delimiter: '…' + }, + 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 = "", StopSel = "", MaxFragments = 3, MaxWords = 123, MinWords = 456, ShortWord = 4, FragmentDelimiter = "…", HighlightAll = FALSE'))} + + expect(highlight_sql).to eq(expected_sql) + end + end end end end