From 991f411bc525001f28fa336eb968adb5159d510a Mon Sep 17 00:00:00 2001 From: GitHub Copilot Date: Tue, 14 Apr 2026 17:00:00 +0200 Subject: [PATCH 1/3] [JAY-724] Add the Elasticsearch::Cluster class The class gives the user access to cluster-related information and statistics. For the moment the class has a single method: #health, which returns the health statistics of the cluster. --- CHANGELOG.md | 4 ++ lib/jay_api/elasticsearch.rb | 1 + lib/jay_api/elasticsearch/cluster.rb | 33 ++++++++++++ spec/jay_api/elasticsearch/cluster_spec.rb | 60 ++++++++++++++++++++++ 4 files changed, 98 insertions(+) create mode 100644 lib/jay_api/elasticsearch/cluster.rb create mode 100644 spec/jay_api/elasticsearch/cluster_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index f32a3ef..0045ae2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ Please mark backwards incompatible changes with an exclamation mark at the start ## [Unreleased] +### Added +- The `Elasticsearch::Cluster` class. The class gives the user access to + cluster-level endpoints, currently including cluster health. + ## [29.6.0] - 2026-03-16 ### Deprecated diff --git a/lib/jay_api/elasticsearch.rb b/lib/jay_api/elasticsearch.rb index ed65435..f00a274 100644 --- a/lib/jay_api/elasticsearch.rb +++ b/lib/jay_api/elasticsearch.rb @@ -4,6 +4,7 @@ require_relative 'elasticsearch/batch_counter' require_relative 'elasticsearch/client' require_relative 'elasticsearch/client_factory' +require_relative 'elasticsearch/cluster' require_relative 'elasticsearch/errors' require_relative 'elasticsearch/index' require_relative 'elasticsearch/indexes' diff --git a/lib/jay_api/elasticsearch/cluster.rb b/lib/jay_api/elasticsearch/cluster.rb new file mode 100644 index 0000000..c75062f --- /dev/null +++ b/lib/jay_api/elasticsearch/cluster.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +require 'forwardable' + +require_relative 'mixins/retriable_requests' + +module JayAPI + module Elasticsearch + # Represents the Elasticsearch cluster and provides access to + # cluster-level APIs. + class Cluster + extend Forwardable + + def_delegator :cluster_client, :health + + attr_reader :transport_client + + # @param [Elasticsearch::Transport::Client] transport_client The transport + # client to use to make requests to the cluster. + def initialize(transport_client) + @transport_client = transport_client + end + + private + + # @return [Elasticsearch::API::Cluster::ClusterClient] The client used to + # access cluster-related information. + def cluster_client + @cluster_client ||= transport_client.cluster + end + end + end +end diff --git a/spec/jay_api/elasticsearch/cluster_spec.rb b/spec/jay_api/elasticsearch/cluster_spec.rb new file mode 100644 index 0000000..99cf957 --- /dev/null +++ b/spec/jay_api/elasticsearch/cluster_spec.rb @@ -0,0 +1,60 @@ +# frozen_string_literal: true + +require 'jay_api/elasticsearch/cluster' + +RSpec.describe JayAPI::Elasticsearch::Cluster do + subject(:cluster) { described_class.new(transport_client) } + + let(:cluster_client) do + instance_double( + Elasticsearch::API::Cluster::ClusterClient, + health: transport_response + ) + end + + let(:transport_client) do + instance_double( + Elasticsearch::Transport::Client, + cluster: cluster_client + ) + end + + describe '#health' do + subject(:method_call) { cluster.health } + + let(:transport_response) do + { + 'cluster_name' => 'xyz01_cluster', + 'status' => 'green', + 'timed_out' => false, + 'number_of_nodes' => 7, + 'number_of_data_nodes' => 4, + 'discovered_master' => true, + 'active_primary_shards' => 646, + 'active_shards' => 2182, + 'relocating_shards' => 0, + 'initializing_shards' => 0, + 'unassigned_shards' => 0, + 'delayed_unassigned_shards' => 0, + 'number_of_pending_tasks' => 0, + 'number_of_in_flight_fetch' => 0, + 'task_max_waiting_in_queue_millis' => 0, + 'active_shards_percent_as_number' => 100.0 + } + end + + it 'gets the ClusterClient from the given TransportClient' do + expect(transport_client).to receive(:cluster) + method_call + end + + it 'forwards the call to the Elasticsearch cluster client' do + expect(cluster_client).to receive(:health) + method_call + end + + it 'directly returns the response' do + expect(method_call).to be(transport_response) + end + end +end From af59797af410d3b21bdd1002de04395618fbc1c2 Mon Sep 17 00:00:00 2001 From: GitHub Copilot Date: Tue, 14 Apr 2026 17:19:17 +0200 Subject: [PATCH 2/3] [JAY-724] Add the #cluster method to Elasticsearch::Client The method returns an instance of the Elasticsearch::Cluster class, which provides the caller with an interface to retrieve cluster-related information and statistics. --- CHANGELOG.md | 2 ++ lib/jay_api/elasticsearch/client.rb | 7 ++++++ .../jay_api/elasticsearch/client_spec.rb | 23 +++++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0045ae2..6b161d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ Please mark backwards incompatible changes with an exclamation mark at the start ## [Unreleased] ### Added +- The `#cluster` method to `JayAPI::Elasticsearch::Client`. The method returns + an instance of `JayAPI::Elasticsearch::Cluster`. - The `Elasticsearch::Cluster` class. The class gives the user access to cluster-level endpoints, currently including cluster health. diff --git a/lib/jay_api/elasticsearch/client.rb b/lib/jay_api/elasticsearch/client.rb index 799427e..0e1ce9f 100644 --- a/lib/jay_api/elasticsearch/client.rb +++ b/lib/jay_api/elasticsearch/client.rb @@ -5,6 +5,7 @@ require 'forwardable' require_relative 'mixins/retriable_requests' +require_relative 'cluster' require_relative 'stats' require_relative 'tasks' @@ -89,6 +90,12 @@ def stats def tasks @tasks ||= ::JayAPI::Elasticsearch::Tasks.new(client: self) end + + # @return [JayAPI::Elasticsearch::Cluster] An instance of the +Cluster+ + # class, which gives the caller access to cluster-related endpoints. + def cluster + @cluster ||= ::JayAPI::Elasticsearch::Cluster.new(transport_client) + end end end end diff --git a/spec/integration/jay_api/elasticsearch/client_spec.rb b/spec/integration/jay_api/elasticsearch/client_spec.rb index 0158ac1..37c0a3b 100644 --- a/spec/integration/jay_api/elasticsearch/client_spec.rb +++ b/spec/integration/jay_api/elasticsearch/client_spec.rb @@ -341,4 +341,27 @@ expect(method_call).to be(tasks) end end + + describe '#cluster' do + subject(:method_call) { client.cluster } + + let(:cluster) do + instance_double( + JayAPI::Elasticsearch::Cluster + ) + end + + before do + allow(JayAPI::Elasticsearch::Cluster).to receive(:new).and_return(cluster) + end + + it 'initializes an instance of JayAPI::Elasticsearch::Cluster and passes the transport client to it' do + expect(JayAPI::Elasticsearch::Cluster).to receive(:new).with(transport_client) + method_call + end + + it 'returns the JayAPI::Elasticsearch::Cluster instance' do + expect(method_call).to be(cluster) + end + end end From a7547646ca2c581316195da05627c5e5b66c7157 Mon Sep 17 00:00:00 2001 From: GitHub Copilot Date: Mon, 13 Apr 2026 17:57:47 +0200 Subject: [PATCH 3/3] [JAY-724] Document Elasticsearch::Client#cluster Documents the the method and its return type, including the methods offered by the returned object. --- .../user_guidelines/elasticsearch/cluster.rst | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 documentation/source/user_guidelines/elasticsearch/cluster.rst diff --git a/documentation/source/user_guidelines/elasticsearch/cluster.rst b/documentation/source/user_guidelines/elasticsearch/cluster.rst new file mode 100644 index 0000000..ad92302 --- /dev/null +++ b/documentation/source/user_guidelines/elasticsearch/cluster.rst @@ -0,0 +1,23 @@ +Cluster +======= + +The ``Cluster`` class gives you access to cluster-level endpoints in +Elasticsearch. The class can be accessed by calling the ``#cluster`` method on +an instance of the ``Elasticsearch::Client`` class. For example: + +.. code-block:: ruby + + require 'jay_api/elasticsearch/client_factory' + + client_factory = JayAPI::Elasticsearch::ClientFactory.new(...) + client = client_factory.create + + cluster = client.cluster + +The ``Cluster`` class has the following method: + +#health +------- + +This method retrieves the cluster health data from the ``/_cluster/health`` +endpoint and returns the response hash from Elasticsearch.