Sha256: 1ec5af2023ee611b7593758b8f9a6215adfc04c51040bccbcd53e91a834b16e6

Contents?: true

Size: 1.73 KB

Versions: 44

Compression:

Stored size: 1.73 KB

Contents

module Sunspot
  module Query
    #
    # A query component that builds parameters for requesting highlights
    #
    class Highlighting #:nodoc:
      def initialize(fields=[], options={})
        @fields  = fields
        @options = options
      end

      #
      # Return Solr highlighting params
      #
      def to_params
        params = {
          :hl => 'on',
          :"hl.simple.pre" => '@@@hl@@@',
          :"hl.simple.post" => '@@@endhl@@@'
        }
        unless @fields.empty?
          params[:"hl.fl"] = @fields.map { |field| field.indexed_name }
        end
        if max_snippets = @options[:max_snippets]
          params.merge!(make_params('snippets', max_snippets))
        end
        if fragment_size = @options[:fragment_size]
          params.merge!(make_params('fragsize', fragment_size))
        end
        if @options[:merge_contiguous_fragments]
          params.merge!(make_params('mergeContiguous', 'true'))
        end
        if @options[:phrase_highlighter]
          params.merge!(make_params('usePhraseHighlighter', 'true'))
          if @options[:require_field_match]
            params.merge!(make_params('requireFieldMatch', 'true'))
          end
        end
        if formatter = @options[:formatter]
          params.merge!(make_params('formatter', formatter))
        end
        if fragmenter = @options[:fragmenter]
          params.merge!(make_params('fragmenter', fragmenter))
        end

        params
      end

      private

      def make_params(name, value)
        if @fields.empty?
          { :"hl.#{name}" => value }
        else
          @fields.inject({}) do |hash, field|
            hash.merge!(:"f.#{field.indexed_name}.hl.#{name}" => value)
          end
        end
      end
    end
  end
end

Version data entries

44 entries across 44 versions & 7 rubygems

Version Path
sunspot-2.7.1 lib/sunspot/query/highlighting.rb
sunspot-2.7.0 lib/sunspot/query/highlighting.rb
sunspot-2.6.0 lib/sunspot/query/highlighting.rb
sunspot-2.5.0 lib/sunspot/query/highlighting.rb
sunspot-2.4.0 lib/sunspot/query/highlighting.rb
sunspot-2.3.0 lib/sunspot/query/highlighting.rb
sunspot-2.2.8 lib/sunspot/query/highlighting.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/sunspot-2.2.7/lib/sunspot/query/highlighting.rb
sunspot-2.2.7 lib/sunspot/query/highlighting.rb
sunspot-2.2.6 lib/sunspot/query/highlighting.rb
sunspot-2.2.5 lib/sunspot/query/highlighting.rb
sunspot-2.2.4 lib/sunspot/query/highlighting.rb
sunspot-2.2.3 lib/sunspot/query/highlighting.rb
sunspot-2.2.2 lib/sunspot/query/highlighting.rb
sunspot-2.2.1 lib/sunspot/query/highlighting.rb
sunspot-2.2.0 lib/sunspot/query/highlighting.rb
sunspot-2.1.1 lib/sunspot/query/highlighting.rb
sunspot-2.1.0 lib/sunspot/query/highlighting.rb
sunspot-2.0.0 lib/sunspot/query/highlighting.rb
sunspot-2.0.0.pre.130115 lib/sunspot/query/highlighting.rb