Class: Elasticsearch::Model::Searching::SearchRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/elasticsearch/model/searching.rb

Overview

Wraps a search request definition

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, query_or_payload, options = {}) ⇒ SearchRequest



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
# File 'lib/elasticsearch/model/searching.rb', line 18

def initialize(klass, query_or_payload, options={})
  @klass   = klass

  __index_name    = options[:index] || klass.index_name
  __document_type = options[:type]  || klass.document_type

  case
    # search query: ...
    when query_or_payload.respond_to?(:to_hash)
      body = query_or_payload.to_hash

    # search '{ "query" : ... }'
    when query_or_payload.is_a?(String) && query_or_payload =~ /^\s*{/
      body = query_or_payload

    # search '...'
    else
      q = query_or_payload
  end

  if body
    @definition = { index: __index_name, type: __document_type, body: body }.update options
  else
    @definition = { index: __index_name, type: __document_type, q: q }.update options
  end
end

Instance Attribute Details

#definitionObject (readonly)

Returns the value of attribute definition.



11
12
13
# File 'lib/elasticsearch/model/searching.rb', line 11

def definition
  @definition
end

#klassObject (readonly)

Returns the value of attribute klass.



11
12
13
# File 'lib/elasticsearch/model/searching.rb', line 11

def klass
  @klass
end

Instance Method Details

#execute!Hash

Performs the request and returns the response from client



49
50
51
# File 'lib/elasticsearch/model/searching.rb', line 49

def execute!
  klass.client.search(@definition)
end