Class: Chewy::ElasticClient

Inherits:
Object
  • Object
show all
Defined in:
lib/chewy/elastic_client.rb

Overview

Replacement for Chewy.client

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(elastic_client = self.class.build_es_client) ⇒ ElasticClient

Returns a new instance of ElasticClient.



11
12
13
# File 'lib/chewy/elastic_client.rb', line 11

def initialize(elastic_client = self.class.build_es_client)
  @elastic_client = elastic_client
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



32
33
34
35
36
# File 'lib/chewy/elastic_client.rb', line 32

def method_missing(name, *args, **kwargs, &block)
  inspect_payload(name, args, kwargs)

  @elastic_client.__send__(name, *args, **kwargs, &block)
end

Class Method Details

.build_es_client(configuration = Chewy.configuration) ⇒ Object



4
5
6
7
8
9
# File 'lib/chewy/elastic_client.rb', line 4

def self.build_es_client(configuration = Chewy.configuration)
  client_configuration = configuration.deep_dup
  client_configuration.delete(:prefix) # used by Chewy, not relevant to Elasticsearch::Client
  block = client_configuration[:transport_options].try(:delete, :proc)
  ::Elasticsearch::Client.new(client_configuration, &block)
end

Instance Method Details

#closeObject

Closes the underlying connections to Elasticsearch.

Neither elasticsearch-ruby nor elastic-transport expose a public method to close connections, so they are only released when Ruby’s garbage collector reclaims the client instance. This reaches down to the Faraday connection of every transport connection and closes it explicitly, which is useful to avoid file descriptor leaks in long-lived processes that build a client per thread (e.g. Sidekiq workers).



23
24
25
26
27
28
# File 'lib/chewy/elastic_client.rb', line 23

def close
  @elastic_client.transport.connections.each do |connection|
    faraday = connection.connection
    faraday.close if faraday.respond_to?(:close)
  end
end