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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/elasticsearch/model/searching.rb', line 35

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

  __index_name    = options[:index] || klass.index_name

  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, body: body }.update options
  else
    @definition = { index: __index_name, q: q }.update options
  end
end

Instance Attribute Details

#definitionObject (readonly)

Returns the value of attribute definition.



28
29
30
# File 'lib/elasticsearch/model/searching.rb', line 28

def definition
  @definition
end

#klassObject (readonly)

Returns the value of attribute klass.



28
29
30
# File 'lib/elasticsearch/model/searching.rb', line 28

def klass
  @klass
end

#optionsObject (readonly)

Returns the value of attribute options.



28
29
30
# File 'lib/elasticsearch/model/searching.rb', line 28

def options
  @options
end

Instance Method Details

#execute!Hash

Performs the request and returns the response from client

Returns:

  • (Hash)

    The response from Elasticsearch



66
67
68
# File 'lib/elasticsearch/model/searching.rb', line 66

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