Module: SearchFlip::Highlightable

Included in:
Aggregation, Criteria
Defined in:
lib/search_flip/highlightable.rb

Overview

The SearchFlip::Sortable mixin provides the chainable #highlight method to use elasticsearch highlighting

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
9
10
# File 'lib/search_flip/highlightable.rb', line 6

def self.included(base)
  base.class_eval do
    attr_accessor :highlight_values
  end
end

Instance Method Details

#highlight(fields, options = {}) ⇒ SearchFlip::Criteria

Adds highlighting of the given fields to the request.

Examples:

CommentIndex.highlight([:title, :message])
CommentIndex.highlight(:title).highlight(:description)
CommentIndex.highlight(:title, require_field_match: false)
CommentIndex.highlight(title: { type: "fvh" })
query = CommentIndex.highlight(:title).search("hello")
query.results[0].highlight.title # => "<em>hello</em> world"

Parameters:

  • fields (Hash, Array, String, Symbol)

    The fields to highligt. Supports raw Elasticsearch values by passing a Hash.

  • options (Hash) (defaults to: {})

    Extra highlighting options. Check out the Elasticsearch docs for further details.

Returns:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/search_flip/highlightable.rb', line 32

def highlight(fields, options = {})
  fresh.tap do |criteria|
    criteria.highlight_values = (criteria.highlight_values || {}).merge(options)

    hash =
      case fields
      when Hash
        fields
      when Array
        fields.each_with_object({}) { |field, h| h[field] = {} }
      else
        { fields => {} }
      end

    criteria.highlight_values[:fields] = (criteria.highlight_values[:fields] || {}).merge(hash)
  end
end