Module: Virgo::SearchHelper
- Defined in:
- app/helpers/virgo/search_helper.rb
Instance Method Summary collapse
-
#search_snippet(document, query, opts = {}) ⇒ Object
Extracts the portion of the document that matches query text.
Instance Method Details
#search_snippet(document, query, opts = {}) ⇒ Object
Extracts the portion of the document that matches query text. Consider refactoring into a complex activerecord query leveraging “ts_headling”. For present though we will perform this with a regex (on the document, which is constructed at the query level - see app/models/concerns/questions/search.rb)
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'app/helpers/virgo/search_helper.rb', line 10 def search_snippet(document, query, opts={}) length = opts[:length] || 300 start_padding = opts[:start_padding] || 30 words = query.split(" ") text = original_text = document matcher = Regexp.new(Regexp.union(words).source, Regexp::IGNORECASE) if text.length > length first_keyword_occurrence = text =~ matcher if first_keyword_occurrence && first_keyword_occurrence > start_padding start_offset = first_keyword_occurrence - start_padding else start_offset = 0 end truncated = text.slice(start_offset, text.length) text = truncated.slice(0, length) if text.length < length # pad beginning with existing text padding_length = length - text.length padding_text = original_text.slice(0, padding_length) text = "...#{padding_text}...#{text}..." else text = "...#{text}..." end end highlighted = text.gsub(matcher) { |match| "<strong class='match'>#{match}</strong>" } highlighted.html_safe end |