diff --git a/lib/pg_search/features/tsearch.rb b/lib/pg_search/features/tsearch.rb index ddac82e7..23ae8d80 100644 --- a/lib/pg_search/features/tsearch.rb +++ b/lib/pg_search/features/tsearch.rb @@ -24,8 +24,8 @@ def rank arel_wrap(tsearch_rank) end - def highlight - arel_wrap(ts_headline) + def highlight highlight_options_overrides + arel_wrap(ts_headline(highlight_options_overrides)) end private @@ -46,22 +46,33 @@ def checks_for_highlight end end - def ts_headline - "ts_headline((#{document}), (#{tsquery}), '#{ts_headline_options}')" + def ts_headline highlight_options_overrides + "ts_headline((#{document}), (#{tsquery}), '#{ts_headline_options(highlight_options_overrides)}')" end - def ts_headline_options - return nil unless options[:highlight].is_a?(Hash) - - headline_options = map_headline_options - headline_options.map{|key, value| "#{key} = #{value}" if value }.compact.join(", ") + def ts_headline_options highlight_options_overrides + highlight_options = if options[:highlight].is_a?(Hash) + options[:highlight] + else + {} + end + highlight_options = highlight_options.merge(highlight_options_overrides) + map_headline_options(highlight_options) + .map{|key, value| "#{key} = #{value}" if value } + .compact + .join(", ") end - def map_headline_options + def map_headline_options highlight_options { - "StartSel" => options[:highlight][:start_sel], - "StopSel" => options[:highlight][:stop_sel], - "MaxFragments" => options[:highlight][:max_fragments] + "StartSel" => highlight_options[:start_sel], + "StopSel" => highlight_options[:stop_sel], + "MaxWords" => highlight_options[:max_words], + "MinWords" => highlight_options[:min_words], + "ShortWord" => highlight_options[:short_word], + "HighlightAll" => highlight_options[:highlight_all], + "MaxFragments" => highlight_options[:max_fragments], + "FragmentDelimiter" => highlight_options[:fragment_delimiter] } end diff --git a/lib/pg_search/scope_options.rb b/lib/pg_search/scope_options.rb index 5ce948c7..8d6b82ca 100644 --- a/lib/pg_search/scope_options.rb +++ b/lib/pg_search/scope_options.rb @@ -43,17 +43,17 @@ def tsearch raise TypeError.new("You need to instantiate this module with []") end - def with_pg_search_highlight + def with_pg_search_highlight highlight_options_overrides = {} scope = self - scope.select(pg_search_highlight_field) + scope.select(pg_search_highlight_field(highlight_options_overrides)) end - def pg_search_highlight_field - "(#{highlight}) AS pg_search_highlight, #{table_name}.*" + def pg_search_highlight_field highlight_options_overrides + "(#{highlight(highlight_options_overrides)}) AS pg_search_highlight, #{table_name}.*" end - def highlight - tsearch.highlight.to_sql + def highlight highlight_options_overrides + tsearch.highlight(highlight_options_overrides).to_sql end end