class Elasticsearch::Model::Searching::SearchRequest

Wraps a search request definition

Attributes

definition[R]
klass[R]
options[R]

Public Class Methods

new(klass, query_or_payload, options={}) click to toggle source

@param klass [Class] The class of the model @param query_or_payload [String,Hash,Object] The search request definition

(string, JSON, Hash, or object responding to `to_hash`)

@param options [Hash] Optional parameters to be passed to the Elasticsearch client

# 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
  __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

Public Instance Methods

execute!() click to toggle source

Performs the request and returns the response from client

@return [Hash] The response from Elasticsearch

# File lib/elasticsearch/model/searching.rb, line 67
def execute!
  klass.client.search(@definition)
end