Class: JayAPI::Elasticsearch::Client

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

Overview

The JayAPI wrapper class over the Elastisearch::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 collapse

ERRORS =

The errors that, if raised, must cause a retry of the connection.

[
  ::Elasticsearch::Transport::Transport::ServerError,
  Faraday::TimeoutError
].freeze
NON_RETRIABLE_ERRORS =

Subclasses of the Elasticsearch::Transport::Transport::ServerError for which a retry doesn’t make sense.

[
  ::Elasticsearch::Transport::Transport::Errors::BadRequest,
  ::Elasticsearch::Transport::Transport::Errors::Unauthorized,
  ::Elasticsearch::Transport::Transport::Errors::Forbidden,
  ::Elasticsearch::Transport::Transport::Errors::NotFound,
  ::Elasticsearch::Transport::Transport::Errors::MethodNotAllowed,
  ::Elasticsearch::Transport::Transport::Errors::RequestEntityTooLarge,
  ::Elasticsearch::Transport::Transport::Errors::NotImplemented
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

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.



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

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.



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

def logger
  @logger
end

#max_attemptsObject (readonly)

Returns the value of attribute max_attempts.



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

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.



43
44
45
# File 'lib/jay_api/elasticsearch/client.rb', line 43

def transport_client
  @transport_client
end

#wait_strategyObject (readonly)

Returns the value of attribute wait_strategy.



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

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.



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

def bulk(**args)
  retry_request { transport_client.bulk(**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.



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

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.



60
61
62
# File 'lib/jay_api/elasticsearch/client.rb', line 60

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.



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

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:



97
98
99
# File 'lib/jay_api/elasticsearch/client.rb', line 97

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

#task_by_id(**args) ⇒ Object

Calls Elasticsearch::Client‘s #tasks.get 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.


91
92
93
# File 'lib/jay_api/elasticsearch/client.rb', line 91

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