From e40e9b533b96ad5286c200bb5256ccc701dcbf9c Mon Sep 17 00:00:00 2001 From: Artem Baguinski Date: Fri, 13 May 2016 10:22:06 +0200 Subject: [PATCH 1/4] add support for other headline options --- lib/pg_search/features/tsearch.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/pg_search/features/tsearch.rb b/lib/pg_search/features/tsearch.rb index ddac82e7..6be9f19e 100644 --- a/lib/pg_search/features/tsearch.rb +++ b/lib/pg_search/features/tsearch.rb @@ -61,7 +61,12 @@ def map_headline_options { "StartSel" => options[:highlight][:start_sel], "StopSel" => options[:highlight][:stop_sel], - "MaxFragments" => options[:highlight][:max_fragments] + "MaxWords" => options[:highlight][:max_words], + "MinWords" => options[:highlight][:min_words], + "ShortWord" => options[:highlight][:short_word], + "HighlightAll" => options[:highlight][:highlight_all], + "MaxFragments" => options[:highlight][:max_fragments], + "FragmentDelimiter" => options[:highlight][:fragment_delimiter] } end From 6cb423a732d1127f143dfc4f2f0e5261c0f80246 Mon Sep 17 00:00:00 2001 From: Artem Baguinski Date: Fri, 13 May 2016 10:42:53 +0200 Subject: [PATCH 2/4] map_headline_options() expects options argument --- lib/pg_search/features/tsearch.rb | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/pg_search/features/tsearch.rb b/lib/pg_search/features/tsearch.rb index 6be9f19e..9e6728ac 100644 --- a/lib/pg_search/features/tsearch.rb +++ b/lib/pg_search/features/tsearch.rb @@ -53,20 +53,20 @@ def ts_headline def ts_headline_options return nil unless options[:highlight].is_a?(Hash) - headline_options = map_headline_options + headline_options = map_headline_options(options[:highlight]) headline_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], - "MaxWords" => options[:highlight][:max_words], - "MinWords" => options[:highlight][:min_words], - "ShortWord" => options[:highlight][:short_word], - "HighlightAll" => options[:highlight][:highlight_all], - "MaxFragments" => options[:highlight][:max_fragments], - "FragmentDelimiter" => options[:highlight][:fragment_delimiter] + "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 From d7fc84920258f945d0faf91a24a9f3738cf827d1 Mon Sep 17 00:00:00 2001 From: Artem Baguinski Date: Fri, 13 May 2016 10:49:01 +0200 Subject: [PATCH 3/4] ts_headline_options() expects option overrides --- lib/pg_search/features/tsearch.rb | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/pg_search/features/tsearch.rb b/lib/pg_search/features/tsearch.rb index 9e6728ac..d4b8c3a4 100644 --- a/lib/pg_search/features/tsearch.rb +++ b/lib/pg_search/features/tsearch.rb @@ -50,11 +50,17 @@ def ts_headline "ts_headline((#{document}), (#{tsquery}), '#{ts_headline_options}')" end - def ts_headline_options - return nil unless options[:highlight].is_a?(Hash) - - headline_options = map_headline_options(options[:highlight]) - 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 highlight_options From bdf44fe025d3c134335ff1cc496ef0e12d5d1c63 Mon Sep 17 00:00:00 2001 From: Artem Baguinski Date: Fri, 13 May 2016 10:52:52 +0200 Subject: [PATCH 4/4] have highlight_options_overrides propagate from with_pg_search_highlight --- lib/pg_search/features/tsearch.rb | 10 +++++----- lib/pg_search/scope_options.rb | 12 ++++++------ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/pg_search/features/tsearch.rb b/lib/pg_search/features/tsearch.rb index d4b8c3a4..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,11 +46,11 @@ 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 highlight_options_overrides = {} + def ts_headline_options highlight_options_overrides highlight_options = if options[:highlight].is_a?(Hash) options[:highlight] else 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