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

More headline highlighting options + overrides at query time #301

Open
wants to merge 4 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
37 changes: 24 additions & 13 deletions lib/pg_search/features/tsearch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
12 changes: 6 additions & 6 deletions lib/pg_search/scope_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down