Class: ElasticSearch::AbstractClient

Inherits:
Object
  • Object
show all
Defined in:
lib/elasticsearch/client/abstract_client.rb

Direct Known Subclasses

Client

Constant Summary collapse

DEFAULTS =
{
  :transport => ElasticSearch::Transport::HTTP
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(servers_or_url, options = {}, &block) ⇒ AbstractClient

Returns a new instance of AbstractClient.



12
13
14
15
16
17
# File 'lib/elasticsearch/client/abstract_client.rb', line 12

def initialize(servers_or_url, options={}, &block)
  @options = DEFAULTS.merge(options)
  @servers, @default_index, @default_type = extract_servers_and_defaults(servers_or_url)
  @current_server = @servers.first
  @connect_block = block
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



10
11
12
# File 'lib/elasticsearch/client/abstract_client.rb', line 10

def connection
  @connection
end

#current_serverObject

Returns the value of attribute current_server.



10
11
12
# File 'lib/elasticsearch/client/abstract_client.rb', line 10

def current_server
  @current_server
end

#serversObject

Returns the value of attribute servers.



10
11
12
# File 'lib/elasticsearch/client/abstract_client.rb', line 10

def servers
  @servers
end

Instance Method Details

#connect!Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/elasticsearch/client/abstract_client.rb', line 35

def connect!
  if @options[:transport].is_a?(Class)
    if @connect_block
      @connection = @options[:transport].new(@current_server, @options, &@connect_block)
    else
      @connection = @options[:transport].new(@current_server, @options)
    end
  else
    @connection = @options[:transport]
  end
  @connection.connect!
end

#disconnect!Object



48
49
50
51
52
# File 'lib/elasticsearch/client/abstract_client.rb', line 48

def disconnect!
  @connection.close rescue nil
  @connection = nil
  @current_server = nil
end

#execute(method_name, *args) ⇒ Object



54
55
56
57
# File 'lib/elasticsearch/client/abstract_client.rb', line 54

def execute(method_name, *args)
  connect! unless @connection
  @connection.send(method_name, *args)
end

#extract_servers_and_defaults(servers_or_url) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/elasticsearch/client/abstract_client.rb', line 19

def extract_servers_and_defaults(servers_or_url)
  default_index = default_type = nil
  given_servers = Array(servers_or_url).collect do |server|
    begin
      uri = URI.parse(server)
      raise URI::InvalidURIError, server if uri.path.nil?
      _, default_index, default_type = uri.path.split("/")
      uri.path = "" # is this expected behavior of URI? may be dangerous to rely on
      uri.to_s
    rescue URI::InvalidURIError
      server
    end
  end
  [given_servers, default_index, default_type]
end