Sha256: 33753b5fc13fe7dc759b559196beadf7776d0a35234899fd2c78e55546b550e2

Contents?: true

Size: 1.56 KB

Versions: 30

Compression:

Stored size: 1.56 KB

Contents

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

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

    # Adds highlighting of the given fields to the request.
    #
    # @example
    #   CommentIndex.highlight([:title, :message])
    #   CommentIndex.highlight(:title).highlight(:description)
    #   CommentIndex.highlight(:title, require_field_match: false)
    #   CommentIndex.highlight(title: { type: "fvh" })
    #
    # @example
    #   query = CommentIndex.highlight(:title).search("hello")
    #   query.results[0].highlight.title # => "<em>hello</em> world"
    #
    # @param fields [Hash, Array, String, Symbol] The fields to highligt.
    #   Supports raw Elasticsearch values by passing a Hash.
    #
    # @param options [Hash] Extra highlighting options. Check out the Elasticsearch
    #   docs for further details.
    #
    # @return [SearchFlip::Criteria] A new criteria including the highlighting

    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
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
search_flip-3.9.0 lib/search_flip/highlightable.rb
search_flip-4.0.0.beta16 lib/search_flip/highlightable.rb
search_flip-4.0.0.beta15 lib/search_flip/highlightable.rb
search_flip-4.0.0.beta14 lib/search_flip/highlightable.rb
search_flip-3.8.0 lib/search_flip/highlightable.rb
search_flip-4.0.0.beta13 lib/search_flip/highlightable.rb
search_flip-3.7.2 lib/search_flip/highlightable.rb
search_flip-4.0.0.beta12 lib/search_flip/highlightable.rb
search_flip-3.7.1 lib/search_flip/highlightable.rb
search_flip-4.0.0.beta11 lib/search_flip/highlightable.rb
search_flip-3.7.0 lib/search_flip/highlightable.rb
search_flip-4.0.0.beta10 lib/search_flip/highlightable.rb
search_flip-3.6.0 lib/search_flip/highlightable.rb
search_flip-3.5.0 lib/search_flip/highlightable.rb
search_flip-4.0.0.beta9 lib/search_flip/highlightable.rb
search_flip-4.0.0.beta8 lib/search_flip/highlightable.rb
search_flip-3.4.0 lib/search_flip/highlightable.rb
search_flip-3.3.0 lib/search_flip/highlightable.rb
search_flip-4.0.0.beta7 lib/search_flip/highlightable.rb
search_flip-4.0.0.beta6 lib/search_flip/highlightable.rb