Class: JayAPI::Elasticsearch::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Mixins::RetriableRequests
Defined in:
lib/jay_api/elasticsearch/client.rb

Overview

The JayAPI wrapper class over the Elasticsearch::Client object. It mirrors the object's API, but if one of the ERRORS is raised, this Wrapper class will rescue the error up to a few times and re-try the connection. This way the connection to Elasticsearch will be more robust.

Constant Summary

Constants included from Mixins::RetriableRequests

Mixins::RetriableRequests::NON_RETRIABLE_ERRORS, Mixins::RetriableRequests::RETRIABLE_ERRORS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mixins::RetriableRequests

#non_retriable_errors, #retriable_errors

Constructor Details

#initialize(transport_client, logger = nil, max_attempts:, wait_strategy:) ⇒ Client

Returns a new instance of Client.

Parameters:

  • transport_client (Elasticsearch::Transport::Client)

    The Client object that will be wrapped.

  • logger (Logging::Logger) (defaults to: nil)
  • max_attempts (Integer)

    The maximum number of attempts that the connection shall be retried.

  • wait_strategy (JayAPI::Elasticsearch::WaitStrategy)

    The waiting strategy for reconnections.



35
36
37
38
39
40
# File 'lib/jay_api/elasticsearch/client.rb', line 35

def initialize(transport_client, logger = nil, max_attempts:, wait_strategy:)
  @transport_client = transport_client
  @logger = logger || Logging.logger($stdout)
  @max_attempts = max_attempts
  @wait_strategy = wait_strategy
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



23
24
25
# File 'lib/jay_api/elasticsearch/client.rb', line 23

def logger
  @logger
end

#max_attemptsObject (readonly)

Returns the value of attribute max_attempts.



23
24
25
# File 'lib/jay_api/elasticsearch/client.rb', line 23

def max_attempts
  @max_attempts
end

#transport_clientBoolean (readonly)

Returns True if there is connectivity to the cluster, false otherwise.

Returns:

  • (Boolean)

    True if there is connectivity to the cluster, false otherwise.

Raises:

  • (Transport::Errors::Forbidden)

    If the user has no permissions to ping the cluster.



28
29
30
# File 'lib/jay_api/elasticsearch/client.rb', line 28

def transport_client
  @transport_client
end

#wait_strategyObject (readonly)

Returns the value of attribute wait_strategy.



23
24
25
# File 'lib/jay_api/elasticsearch/client.rb', line 23

def wait_strategy
  @wait_strategy
end

Instance Method Details

#bulk(**args) ⇒ Object

Calls the Elasticsearch::Client's #bulk method and retries the connection a few times if a ServerError occurs.



75
76
77
# File 'lib/jay_api/elasticsearch/client.rb', line 75

def bulk(**args)
  retry_request { transport_client.bulk(**args) }
end

#clusterJayAPI::Elasticsearch::Cluster

Returns An instance of the Cluster class, which gives the caller access to cluster-related endpoints.

Returns:



112
113
114
# File 'lib/jay_api/elasticsearch/client.rb', line 112

def cluster
  @cluster ||= ::JayAPI::Elasticsearch::Cluster.new(transport_client)
end

#count(**args) ⇒ Hash

Calls Elasticsearch::Client's #count method and retries the connection a few times when certain Elasticsearch::Error::ServerError are raised.

Returns:

  • (Hash)

    The response from Elasticsearch, which contains the count of documents in the index matching the given query (if any). An example of this response is shown below:

    { "count" => 24097688, "_shards" => { "total"=>5, "successful"=>5, "skipped"=>0, "failed"=>0 } }

See Also:

  • for information about the required arguments.


68
69
70
# File 'lib/jay_api/elasticsearch/client.rb', line 68

def count(**args)
  retry_request { transport_client.count(**args) }
end

#delete_by_query(**args) ⇒ Object

Calls the +Elasticsearch::Client+'s #delete_by_query method forwarding the given parameters. If the request fails additional retries will be performed.



84
85
86
# File 'lib/jay_api/elasticsearch/client.rb', line 84

def delete_by_query(**args)
  retry_request { transport_client.delete_by_query(**args) }
end

#index(**args) ⇒ Object

Calls the Elasticsearch::Client's #index method and retries the connection a few times if a ServerError occurs.



45
46
47
# File 'lib/jay_api/elasticsearch/client.rb', line 45

def index(**args)
  retry_request { transport_client.index(**args) }
end

#search(**args) ⇒ Object

Calls the Elasticsearch::Client's #search method and retries the connection a few times if a ServerError occurs.



52
53
54
# File 'lib/jay_api/elasticsearch/client.rb', line 52

def search(**args)
  retry_request { transport_client.search(**args) }
end

#statsJayAPI::Elasticsearch::Stats

Returns An instance of the Stats class, which gives the caller access to Elasticsearch's Statistics API.

Returns:



99
100
101
# File 'lib/jay_api/elasticsearch/client.rb', line 99

def stats
  @stats ||= ::JayAPI::Elasticsearch::Stats.new(transport_client)
end

#task_by_id(**args) ⇒ Object

Deprecated.

Use Tasks#by_id instead.

Calls +Elasticsearch::Client+'s #tasks.get method forwarding the given parameters. If the request fails, additional retries will be performed.



93
94
95
# File 'lib/jay_api/elasticsearch/client.rb', line 93

def task_by_id(**args)
  retry_request { transport_client.tasks.get(**args) }
end

#tasksJayAPI::Elasticsearch::Tasks

Returns An instance of the Tasks class, which can be used to retrieve the status of the tasks running on the Elasticsearch cluster.

Returns:

  • (JayAPI::Elasticsearch::Tasks)

    An instance of the Tasks class, which can be used to retrieve the status of the tasks running on the Elasticsearch cluster.



106
107
108
# File 'lib/jay_api/elasticsearch/client.rb', line 106

def tasks
  @tasks ||= ::JayAPI::Elasticsearch::Tasks.new(client: self)
end

#update(**args) ⇒ Object

Calls the +Elasticsearch::Client+'s #update method forwarding the given parameters. If the request fails, additional retries will be performed.

See Also:

  • for more info about the arguments and the return value.


120
121
122
# File 'lib/jay_api/elasticsearch/client.rb', line 120

def update(**args)
  retry_request { transport_client.update(**args) }
end