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
77
78
79
80
# 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
    # The last arg is opts, which shouldn't be sent when `perform_request` is called
    # directly. Check if :endpoint is a key, which means it's the extra identifier
    # used for OpenTelemetry.
    args.pop if args[-1].is_a?(Hash) && args[-1][:endpoint]
    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)


82
83
84
# File 'lib/elasticsearch.rb', line 82

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