class EagleSearch::Interpreter

Public Class Methods

new(index, query, options) click to toggle source
# File lib/eagle_search/interpreter.rb, line 3
def initialize(index, query, options)
  @index   = index
  @query   = query
  @options = options
  @options[:page] = @options[:page].to_i if @options[:page]
  @options[:per_page] = @options[:per_page].to_i if @options[:per_page]
end

Public Instance Methods

payload() click to toggle source
# File lib/eagle_search/interpreter.rb, line 11
def payload
  return @options[:custom_payload] if @options[:custom_payload]

  payload = {
    query: {
      filtered: {
        query: query_payload,
        filter: filter_payload
      }
    },
    aggregations: aggregations_payload
  }

  payload.merge!({ sort: @options[:sort] }) if @options[:sort]

  # from
  if @options[:page] && @options[:page] > 1
    from = (@options[:page] - 1) * (@options[:per_page] || 10)
    payload.merge!({ from: from })
  end

  #size
  payload.merge!({ size: @options[:per_page] }) if @options[:per_page]

  #highlight
  if @options[:highlight] && @options[:highlight][:fields]
    highlight = {
      fields: {}
    }
    @options[:highlight][:fields].each do |field|
      highlight[:fields][field] = {}
    end

    if @options[:highlight][:tags]
      highlight[:pre_tags] = @options[:highlight][:tags]
      highlight[:post_tags] = @options[:highlight][:tags].map { |tag| tag.gsub(/</, "</")}
    end

    payload.merge!(highlight: highlight)
  end

  payload
end

Private Instance Methods

aggregations_payload() click to toggle source
# File lib/eagle_search/interpreter.rb, line 81
def aggregations_payload
  return {} unless @options[:aggregations]
  EagleSearch::Interpreter::Aggregation.new(@options[:aggregations]).payload
end
filter_payload() click to toggle source
# File lib/eagle_search/interpreter.rb, line 71
def filter_payload
  if @options[:filters]
    EagleSearch::Interpreter::Filter.new(@options[:filters]).payload
  elsif @options[:custom_filters]
    @options[:custom_filters]
  else
    {}
  end
end
query_payload() click to toggle source
# File lib/eagle_search/interpreter.rb, line 63
def query_payload
  if @options[:custom_query]
    @options[:custom_query]
  else
    EagleSearch::Interpreter::Query.new(@index, @query, @options).payload
  end
end