Class: Elasticsearch::Client

Inherits:
Object
  • Object
show all
Includes:
API
Defined in:
lib/elasticsearch.rb

Overview

This is the stateful Elasticsearch::Client, using an instance of elastic-transport.

Constant Summary collapse

DEFAULT_CLOUD_PORT =

The default port to use if connecting using a Cloud ID. Updated from 9243 to 443 in client version 7.10.1

Since:

  • 7.2.0

443

Instance Method Summary collapse

Constructor Details

#initialize(arguments = {}, &block) ⇒ Client

Create a client connected to an Elasticsearch cluster.

Parameters:

  • arguments (Hash) (defaults to: {})
    • initializer arguments

Options Hash (arguments):

  • :cloud_id (String)
    • The Cloud ID to connect to Elastic Cloud

  • :api_key (String, Hash)

    Use API Key Authentication, either the base64 encoding of ‘id` and `api_key` joined by a colon as a String, or a hash with the `id` and `api_key` values.

  • :opaque_id_prefix (String)

    set a prefix for X-Opaque-Id when initializing the client. This will be prepended to the id you set before each request if you’re using X-Opaque-Id

  • :headers (Hash)

    Custom HTTP Request Headers



47
48
49
50
51
52
53
54
55
# File 'lib/elasticsearch.rb', line 47

def initialize(arguments = {}, &block)
  @verified = false
  @warned = false
  @opaque_id_prefix = arguments[:opaque_id_prefix] || nil
  api_key(arguments) if arguments[:api_key]
  setup_cloud(arguments) if arguments[:cloud_id]
  set_user_agent!(arguments) unless sent_user_agent?(arguments)
  @transport = Elastic::Transport::Client.new(arguments, &block)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/elasticsearch.rb', line 57

def method_missing(name, *args, &block)
  if methods.include?(name)
    super
  elsif name == :perform_request
    # The signature for perform_request is:
    # method, path, params, body, headers, opts
    if (opaque_id = args[2]&.delete(:opaque_id))
      headers = args[4] || {}
      opaque_id = @opaque_id_prefix ? "#{@opaque_id_prefix}#{opaque_id}" : opaque_id
      args[4] = headers.merge('X-Opaque-Id' => opaque_id)
    end
    unless @verified
      verify_elasticsearch(*args, &block)
    else
      @transport.perform_request(*args, &block)
    end
  else
    @transport.send(name, *args, &block)
  end
end

Instance Method Details

#respond_to_missing?(method_name, *args) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/elasticsearch.rb', line 78

def respond_to_missing?(method_name, *args)
  @transport.respond_to?(method_name) || super
end