Module: Elastics::ClassProxy::Templates::Search

Included in:
Elastics::ClassProxy::Templates
Defined in:
lib/elastics/class_proxy/templates/search.rb

Instance Method Summary collapse

Instance Method Details

#count_search(template, *vars) ⇒ Object

in the default use of query_then_search setting the size=0 will cause the same



61
62
63
64
# File 'lib/elastics/class_proxy/templates/search.rb', line 61

def count_search(template, *vars)
  template = template.is_a?(Elastics::Template) ? template : templates[template]
  template.render Vars.new({:params => {:size => 0}, :raw_result => true}, *vars)
end

#define_search(name, source, source_vars = nil) ⇒ Object



6
7
8
9
# File 'lib/elastics/class_proxy/templates/search.rb', line 6

def define_search(name, source, source_vars=nil)
  args = Utils.parse_source(source)
  send :define_template, Template::Search, name, args, source_vars
end

#multi_search(requests, *variables) ⇒ Object

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-multi-search.html requests can be an array of arrays: [[:template1, variable_hash1], [template2, variable_hash2]] or a hash => variable_hash1, template2 => variable_hash2 The variables are an hash of variables that will be used to render the msearch template the array of result is at .responses



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/elastics/class_proxy/templates/search.rb', line 16

def multi_search(requests, *variables)
  requests = requests.map { |name, vars| [name, vars] } if requests.is_a?(Hash)
  lines    = requests.map { |name, vars| templates[name].to_msearch(vars) }.join()
  template = Elastics::Template.new('GET', '/<<index>>/<<type>>/_msearch') # no setup elastics so raw_result
  template.send(:do_render, *variables, :data => lines) do |http_response|
    responses   = []
    es_response = MultiJson.decode(http_response.body)
    es_response['responses'].each_with_index do |raw_result, i|
      name, vars = requests[i]
      int = templates[name].interpolate(vars, strict=true)
      result = Result.new(templates[name], int[:vars], http_response, raw_result)
      responses << result.to_elastics_result
    end
    es_response['responses'] = responses
    def es_response.responses
      self['responses']
    end
    es_response
  end
end

#scan_search(template, *vars, &block) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/elastics/class_proxy/templates/search.rb', line 38

def scan_search(template, *vars, &block)
  user_raw_result = vars.any?{|v| v[:raw_result]}
  scroll          = '5m'
  search_vars     = Vars.new({:params     => { :search_type => 'scan',
                                               :scroll      => scroll,
                                               :size        => 50 },
                              :raw_result => true}, *vars)
  search_temp     = template.is_a?(Elastics::Template) ? template : templates[template]

  scroll_vars     = Vars.new({:params     => { :scroll => scroll },
                              :raw_result => true}, variables, *vars)
  scroll_temp     = Elastics::Template.new( :get, '/_search/scroll', nil, scroll_vars )
  search_res      = search_temp.render search_vars
  scroll_id       = search_res['_scroll_id']
  while (result = scroll_temp.render(:data => scroll_id)) do
    break if result['hits']['hits'].empty?
    scroll_id = result['_scroll_id']
    result.variables[:raw_result] = user_raw_result
    block.call result.to_elastics_result(force=true)
  end
end