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: {})

    a customizable set of options

  • api_key (Hash)

    a customizable set of options

  • opaque_id_prefix (Hash)

    a customizable set of options

Options Hash (arguments):

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


44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/elasticsearch.rb', line 44

def initialize(arguments = {}, &block)
  @verified = false
  @opaque_id_prefix = arguments[:opaque_id_prefix] || nil
  api_key(arguments) if arguments[:api_key]
  if arguments[:cloud_id]
    arguments[:hosts] = setup_cloud_host(
      arguments[:cloud_id],
      arguments[:user],
      arguments[:password],
      arguments[:port]
    )
  end
  @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


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

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
    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
    verify_elasticsearch unless @verified
    @transport.perform_request(*args, &block)
  else
    @transport.send(name, *args, &block)
  end
end

Instance Method Details

#respond_to_missing?(method_name, *args) ⇒ Boolean

Returns:

  • (Boolean)

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

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