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

Returns a new instance of SearchRequest.

Parameters:

  • klass (Class)

    The class of the model

  • query_or_payload (String, Hash, Object)

    The search request definition (string, JSON, Hash, or object responding to ‘to_hash`)

  • options (Hash) (defaults to: {})

    Optional parameters to be passed to the Elasticsearch client



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

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

  __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

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#execute!Hash

Performs the request and returns the response from client

Returns:

  • (Hash)

    The response from Elasticsearch



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

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