Class: EagleSearch::Interpreter

Inherits:
Object
  • Object
show all
Defined in:
lib/eagle_search/interpreter.rb

Defined Under Namespace

Classes: Aggregation, Filter, Query

Instance Method Summary collapse

Constructor Details

#initialize(index, query, options) ⇒ Interpreter

Returns a new instance of Interpreter.



3
4
5
6
7
8
9
# 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

Instance Method Details

#payloadObject



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
48
49
50
51
52
53
# 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