Class: AtlasEngine::Elasticsearch::Client

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Includes:
ClientInterface
Defined in:
app/models/atlas_engine/elasticsearch/client.rb

Constant Summary collapse

DEFAULT_OPTIONS =
T.let(
  {
    read_timeout: 1,
    open_timeout: 1,
    keep_alive_timeout: 60,
    retry_on_failure: false,
    headers: {},
  },
  ConfigType,
)

Constants included from ClientInterface

AtlasEngine::Elasticsearch::ClientInterface::BodyType, AtlasEngine::Elasticsearch::ClientInterface::ConfigType

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ClientInterface

#delete, #get, #head, #post, #put

Constructor Details

#initialize(config = {}) ⇒ Client

Returns a new instance of Client.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/models/atlas_engine/elasticsearch/client.rb', line 25

def initialize(config = {})
  @config = T.let(DEFAULT_OPTIONS.merge(config).freeze, ConfigType)

  options = {
    url: @config[:url] || ENV["ELASTICSEARCH_URL"],
    retry_on_failure: false,
  }.compact

  @client = T.let(
    Elastic::Transport::Client.new(options) do |faraday_connection|
      faraday_connection.options.timeout = read_timeout
      faraday_connection.options.open_timeout = open_timeout
      @config[:headers][:"Content-Type"] = "application/json"

      if ENV["ELASTICSEARCH_API_KEY"].present?
        @config[:headers][:Authorization] = "ApiKey #{ENV["ELASTICSEARCH_API_KEY"]}"
      end
      faraday_connection.headers = @config[:headers] if @config[:headers].present?

      if ENV["ELASTICSEARCH_CLIENT_CERT"] && ENV["ELASTICSEARCH_CLIENT_KEY"]
        faraday_connection.ssl.client_cert = ENV["ELASTICSEARCH_CLIENT_CERT"]
        faraday_connection.ssl.client_key = ENV["ELASTICSEARCH_CLIENT_KEY"]
        faraday_connection.ssl.verify = true
      end

      if ENV["ELASTICSEARCH_CLIENT_CA_CERT"]
        faraday_connection.ssl.ca_file = ENV["ELASTICSEARCH_CLIENT_CA_CERT"]
        faraday_connection.ssl.verify = true
      end

      if ENV["ELASTICSEARCH_INSECURE_NO_VERIFY_SERVER"]
        faraday_connection.ssl.verify = false
      end
    end,
    Elastic::Transport::Client,
  )
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



22
23
24
# File 'app/models/atlas_engine/elasticsearch/client.rb', line 22

def config
  @config
end

Instance Method Details

#find_index_by(alias_name:) ⇒ Object



76
77
78
79
80
# File 'app/models/atlas_engine/elasticsearch/client.rb', line 76

def find_index_by(alias_name:)
  get("_alias/#{alias_name}").body.keys.first
rescue Elastic::Transport::Transport::Errors::NotFound
  nil
end

#index_or_alias_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
86
87
# File 'app/models/atlas_engine/elasticsearch/client.rb', line 83

def index_or_alias_exists?(name)
  head(name).status == 200
rescue Elastic::Transport::Transport::Errors::NotFound
  false
end

#request(method, path, body = nil, options = config.dup) ⇒ Object



71
72
73
# File 'app/models/atlas_engine/elasticsearch/client.rb', line 71

def request(method, path, body = nil, options = config.dup)
  raw_request(method, path, body, options)
end