Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ Please mark backwards incompatible changes with an exclamation mark at the start

## [Unreleased]

### Added
- The `timeout` parameter to `Elasticsearch::ClientFactory#create`. The parameter
allows the user to specify the timeout in seconds for Elasticsearch requests.

## [29.2.0] - 2025-12-09

### Added
Expand Down
12 changes: 8 additions & 4 deletions lib/jay_api/elasticsearch/client_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,17 @@ def initialize(cluster_url:, port: nil, logger: nil, **credentials)
# each connection will be:
# * wait_interval with :constant wait strategy
# * wait_interval**i with :geometric wait strategy (where i is the i'th re-try)
# @param [Integer] timeout The number of seconds to wait for Elasticsearch's
# response. For big queries that fetch large amounts of data the default
# timeout may be too low.
# @return [JayAPI::Elasticsearch::Client] The Elasticsearch client.
def create(max_attempts: MAX_ATTEMPTS, wait_strategy: :geometric, wait_interval: WAIT_INTERVAL)
def create(max_attempts: MAX_ATTEMPTS, wait_strategy: :geometric, wait_interval: WAIT_INTERVAL, timeout: nil)
JayAPI::Elasticsearch::Client.new(
::Elasticsearch::Client.new(
::Elasticsearch::Client.new({
hosts: [host],
log: false
),
log: false,
request_timeout: timeout
}.compact),
logger,
max_attempts: max_attempts,
wait_strategy: WAIT_STRATEGIES[wait_strategy].new(wait_interval: wait_interval, logger: logger)
Expand Down
66 changes: 42 additions & 24 deletions spec/integration/jay_api/elasticsearch/client_factory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,15 @@

it 'defaults to using 9200 as a port number' do
expect(Elasticsearch::Client).to receive(:new).with(
hosts: [host],
log: false
{ hosts: [host], log: false }
)

client
end
end

context 'when the port is specified' do
subject(:client_factory) { described_class.new(cluster_url: 'https://www.es.com', port: 1234) }
let(:constructor_params) { super().merge(port: 1234) }

let(:host) do
{
Expand All @@ -43,18 +42,15 @@

it 'uses the specified port number' do
expect(Elasticsearch::Client).to receive(:new).with(
hosts: [host],
log: false
{ hosts: [host], log: false }
)

client
end
end

context 'when the credentials are specified' do
subject(:client_factory) do
described_class.new(cluster_url: 'https://www.es.com', username: 'Koala', password: 'Bear')
end
let(:constructor_params) { super().merge(username: 'Koala', password: 'Bear') }

let(:host) do
{
Expand All @@ -68,8 +64,29 @@

it 'initializes the Elasticsearch Client with the correct credentials' do
expect(Elasticsearch::Client).to receive(:new).with(
hosts: [host],
log: false
{ hosts: [host], log: false }
)

client
end
end

context 'when no timeout is given' do
it "initializes the Elasticsearch Client without the 'request_timeout' parameter" do
expect(Elasticsearch::Client).to receive(:new).with(
{ hosts: [host], log: false }
)

client
end
end

context 'when a timeout is given' do
let(:create_params) { super().merge(timeout: 300) }

it "initializes the Elasticsearch Client with the expected 'request_timeout' parameter" do
expect(Elasticsearch::Client).to receive(:new).with(
{ hosts: [host], log: false, request_timeout: 300 }
)

client
Expand All @@ -78,7 +95,11 @@
end

RSpec.describe JayAPI::Elasticsearch::ClientFactory do
subject(:client_factory) { described_class.new(cluster_url: 'https://www.es.com') }
subject(:client_factory) { described_class.new(**constructor_params) }

let(:constructor_params) do
{ cluster_url: 'https://www.es.com' }
end

shared_examples_for '#create' do
it_behaves_like 'ClientFactory initializing the Transport Client'
Expand All @@ -97,6 +118,10 @@
end

describe '#create' do
subject(:client) { client_factory.create(**create_params) }

let(:create_params) { {} }

let(:host) do
{
host: 'www.es.com',
Expand All @@ -114,47 +139,40 @@
let(:expected_max_attempts) { 4 }

before do
allow(Elasticsearch::Client).to receive(:new).with(
hosts: [host],
log: false
).and_return(transport_client)
allow(Elasticsearch::Client).to receive(:new).and_return(transport_client)
end

context 'without any specified parameters' do
subject(:client) { client_factory.create }
let(:create_params) { {} }

it_behaves_like '#create'
end

context "when specifying the 'wait_strategy' param" do
context "with 'geometric'" do
subject(:client) { client_factory.create(wait_strategy: :geometric) }

let(:create_params) { super().merge(wait_strategy: :geometric) }
let(:expected_wait_strategy) { JayAPI::Abstract::GeometricWait }

it_behaves_like '#create'
end

context "with 'constant'" do
subject(:client) { client_factory.create(wait_strategy: :constant) }

let(:create_params) { super().merge(wait_strategy: :constant) }
let(:expected_wait_strategy) { JayAPI::Abstract::ConstantWait }

it_behaves_like '#create'
end
end

context "when specifying the 'max_attempts' param" do
subject(:client) { client_factory.create(max_attempts: expected_max_attempts) }

let(:create_params) { super().merge(max_attempts: expected_max_attempts) }
let(:expected_max_attempts) { 100 }

it_behaves_like '#create'
end

context "when specifying the 'wait_interval' param" do
subject(:client) { client_factory.create(wait_interval: expected_wait_interval) }

let(:create_params) { super().merge(wait_interval: expected_wait_interval) }
let(:expected_wait_interval) { 399 }

it_behaves_like '#create'
Expand Down
3 changes: 1 addition & 2 deletions spec/integration/jay_api/elasticsearch/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@

before do
allow(Elasticsearch::Client).to receive(:new).with(
hosts: [host],
log: false
{ hosts: [host], log: false }
).and_return(transport_client)
end

Expand Down