jay_api 29.7.0 → 29.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 469981ee390e1c409ff7e4e3faef1d9fb3579e87c9f1e2262f1664ba8d4731c3
4
- data.tar.gz: 0f9b84be25dfc1a2f318edb8ae462714553e7898c278e1da0b9e38d211049719
3
+ metadata.gz: e0a1faee742a3503bae215225c1a111f9cb21b21e94ffb83b10fcd16d14f71e1
4
+ data.tar.gz: b39ed836b7bef2233a3833d9b9e56f5234f4b542fd10f1a4fb7fc35b85e85e37
5
5
  SHA512:
6
- metadata.gz: 23f0fd77caa519085f05b6efeff92c128895a912ffa3e75d83703f3b4a7b27cf76e39dfcb42652461fc730c3ff790df67913f0d7f52926ab8bebec86cf02196b
7
- data.tar.gz: 48c2c4d5c97ef87fe7ce9ca0809d4b650549db40b654bb7b856438462b6b1c482bdd83f5c11840428fac5f51fc51cbc08cf43bec996f22abdf2e08a4f87050f8
6
+ metadata.gz: 4963d63c87ed8d756e94984ec635d53cb4db539e6fba42632be2ecd8f5cf79443485ededadaddf9f15988414db7e37b07b515f2d37529fcc074de8d57bf6f2f4
7
+ data.tar.gz: 9db8b8d9600477ded9c07058d26af30de56153717adc5cdd9dc179bd0b2893225936fbdecb2281a0d5e06859ad6797eee3554d89e3531d88d449042040f266d2
data/CHANGELOG.md CHANGED
@@ -8,6 +8,27 @@ Please mark backwards incompatible changes with an exclamation mark at the start
8
8
 
9
9
  ## [Unreleased]
10
10
 
11
+ ## [29.9.0] - 2026-08-04
12
+
13
+ ### Added
14
+ - The `JayAPI::Elasticsearch::Count` class.
15
+ - The `#count` method to the `Indexable` module. The method returns the number
16
+ of documents in the `Index` or `Indexes`. A query can be provided to filter
17
+ the documents that are counted.
18
+
19
+ ## [29.8.0] - 2026-07-06
20
+
21
+ ### Deprecated
22
+ - The `Elasticsearch::QueryBuilder::Script` class is now deprecated, please
23
+ use `Elasticsearch::Script` instead.
24
+
25
+ ### Added
26
+ - The `#update` method to `JayAPI::Elasticsearch::Client`.
27
+ - The `QueryClauses::IDs` class and the corresponding `#ids` method to the
28
+ `MatchClauses` module. This allows the use of Elasticsearch's
29
+ [ids](https://www.elastic.co/docs/reference/query-languages/query-dsl/query-dsl-ids-query)
30
+ query with the Query Builder.
31
+
11
32
  ## [29.7.0] - 2026-04-28
12
33
 
13
34
  ### Added
@@ -53,6 +53,22 @@ module JayAPI
53
53
  retry_request { transport_client.search(**args) }
54
54
  end
55
55
 
56
+ # Calls Elasticsearch::Client's #count method and retries the connection
57
+ # a few times when certain Elasticsearch::Error::ServerError are raised.
58
+ # @see Elasticsearch::API::Cat::Actions#count for information about the
59
+ # required arguments.
60
+ # @return [Hash] The response from Elasticsearch, which contains the count
61
+ # of documents in the index matching the given query (if any). An
62
+ # example of this response is shown below:
63
+ #
64
+ # {
65
+ # "count" => 24097688,
66
+ # "_shards" => { "total"=>5, "successful"=>5, "skipped"=>0, "failed"=>0 }
67
+ # }
68
+ def count(**args)
69
+ retry_request { transport_client.count(**args) }
70
+ end
71
+
56
72
  # Calls the Elasticsearch::Client's #bulk method and retries the connection a few times if
57
73
  # a ServerError occurs.
58
74
  # @see Elasticsearch::Client#index for information about the arguments and the returned value.
@@ -96,6 +112,14 @@ module JayAPI
96
112
  def cluster
97
113
  @cluster ||= ::JayAPI::Elasticsearch::Cluster.new(transport_client)
98
114
  end
115
+
116
+ # Calls the +Elasticsearch::Client+'s #update method forwarding the given
117
+ # parameters. If the request fails, additional retries will be performed.
118
+ # @see Elasticsearch::API::Actions#update for more info about the
119
+ # arguments and the return value.
120
+ def update(**args)
121
+ retry_request { transport_client.update(**args) }
122
+ end
99
123
  end
100
124
  end
101
125
  end
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+
5
+ module JayAPI
6
+ module Elasticsearch
7
+ # Lightweight wrapper around an Elasticsearch count response.
8
+ #
9
+ # It behaves like a numeric value for comparisons and arithmetic while
10
+ # preserving access to the original response payload.
11
+ class Count
12
+ extend Forwardable
13
+
14
+ include Comparable
15
+
16
+ def_delegators :count, :to_i, :to_int
17
+
18
+ # @param [Hash] data The raw Elasticsearch response that includes the
19
+ # +count+ value.
20
+ def initialize(data)
21
+ @data = data
22
+ end
23
+
24
+ # The count returned by Elasticsearch.
25
+ # @return [Integer] The raw count value from the response payload.
26
+ def count
27
+ @count ||= data['count']
28
+ end
29
+
30
+ # Coerces another value so arithmetic operations can be performed with
31
+ # the receiver.
32
+ # @param [Numeric, #to_int] other The other operand.
33
+ # @return [Array(Numeric, Integer)] A two-element array in the form
34
+ # expected by Ruby coercion.
35
+ # @raise [TypeError] If +other+ cannot be coerced into an +Integer+.
36
+ # :reek:FeatureEnvy :reek:ManualDispatch -- Not much that can be done here, since this is a coercion method.
37
+ def coerce(other)
38
+ if other.is_a?(Numeric)
39
+ [other, to_int]
40
+ elsif other.respond_to?(:to_int)
41
+ [other.to_int, to_int]
42
+ else
43
+ raise TypeError, "#{other.class} cannot be coerced into Integer"
44
+ end
45
+ end
46
+
47
+ # Compares this count with another numeric value.
48
+ # @param [Numeric, #to_int] other The value to compare with.
49
+ # @return [-1, 0, 1, nil] Comparison result, or +nil+ when the values are
50
+ # not comparable.
51
+ # :reek:ManualDispatch -- Relies on the other object responding to +to_int+.
52
+ def <=>(other)
53
+ if other.is_a?(Numeric)
54
+ to_int <=> other
55
+ elsif other.respond_to?(:to_int)
56
+ to_int <=> other.to_int
57
+ else
58
+ super
59
+ end
60
+ end
61
+
62
+ # Adds another value to the receiver's count.
63
+ # @param [Numeric, #to_int] other The value to add.
64
+ # @return [Numeric] The arithmetic result.
65
+ def +(other)
66
+ other, this = coerce(other)
67
+ this + other
68
+ end
69
+
70
+ # Subtracts another value from the receiver's count.
71
+ # @param [Numeric, #to_int] other The value to subtract.
72
+ # @return [Numeric] The arithmetic result.
73
+ def -(other)
74
+ other, this = coerce(other)
75
+ this - other
76
+ end
77
+
78
+ # Multiplies the receiver's count by another value.
79
+ # @param [Numeric, #to_int] other The value to multiply by.
80
+ # @return [Numeric] The arithmetic result.
81
+ def *(other)
82
+ other, this = coerce(other)
83
+ this * other
84
+ end
85
+
86
+ # Divides the receiver's count by another value.
87
+ # @param [Numeric, #to_int] other The divisor.
88
+ # @return [Numeric] The arithmetic result.
89
+ def /(other)
90
+ other, this = coerce(other)
91
+ this / other
92
+ end
93
+
94
+ # @return [String] The count converted to a string.
95
+ def to_s
96
+ count.to_s
97
+ end
98
+
99
+ private
100
+
101
+ attr_reader :data
102
+ end
103
+ end
104
+ end
@@ -1,5 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'active_support'
4
+ require 'active_support/core_ext/object/blank'
5
+
3
6
  require_relative 'indexable'
4
7
  require_relative 'indices/settings'
5
8
  require_relative 'errors/writable_index_error'
@@ -52,6 +55,46 @@ module JayAPI
52
55
  super.first
53
56
  end
54
57
 
58
+ # Updates a document by ID.
59
+ # @param [String] id The ID of the document to update.
60
+ # @param [Hash, nil] doc The partial document to update the existing
61
+ # document with. If +nil+ is given, the +script+ parameter must be
62
+ # provided. If both +doc+ and +script+ are given, the +doc+ parameter
63
+ # will be ignored by Elasticsearch.
64
+ # @param [JayAPI::Elasticsearch::Script, Hash, nil] script The script to
65
+ # update the existing document with. If +nil+ is given, the +doc+
66
+ # parameter must be provided. It's recommended to use a
67
+ # +JayAPI::Elasticsearch::Script+ object, but a Hash can also be used.
68
+ # @return [Hash] A Hash containing information about the updated document.
69
+ # An example of such Hash is:
70
+ #
71
+ # {
72
+ # "_index" : "xyz01_build_properties",
73
+ # "_type" : "_doc",
74
+ # "_id" : "ns4AAZ8BXEjZhYMmw-8y",
75
+ # "_version" : 7,
76
+ # "result" : "updated",
77
+ # "_shards" : {
78
+ # "total" : 2,
79
+ # "successful" : 2,
80
+ # "failed" : 0
81
+ # },
82
+ # "_seq_no" : 11,
83
+ # "_primary_term" : 1
84
+ # }
85
+ #
86
+ # For information on how to use the script and about the contents of the
87
+ # returned Hash please see:
88
+ # https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-update
89
+ # @raise [ArgumentError] If both +doc+ and +script+ are +nil+.
90
+ # @raise [Elasticsearch::Transport::Transport::ServerError] If the
91
+ # update fails.
92
+ def update(id:, doc: nil, script: nil)
93
+ raise ArgumentError, "Either 'doc' or 'script' must be provided" if doc.blank? && script.blank?
94
+
95
+ client.update(index: index_name, id:, body: { doc: doc, script: script&.to_h }.compact)
96
+ end
97
+
55
98
  # @return [JayAPI::Elasticsearch::Indices::Settings] The settings for the
56
99
  # index.
57
100
  def settings
@@ -8,6 +8,7 @@ require 'logging'
8
8
 
9
9
  require_relative 'async'
10
10
  require_relative 'batch_counter'
11
+ require_relative 'count'
11
12
  require_relative 'errors/elasticsearch_error'
12
13
  require_relative 'query_results'
13
14
  require_relative 'response'
@@ -106,6 +107,18 @@ module JayAPI
106
107
  query_results(query, response, batch_counter, type)
107
108
  end
108
109
 
110
+ # @return [JayAPI::Elasticsearch::Count] The count of documents in the
111
+ # index(es) matching the given query (if any).
112
+ # @raise [Elasticsearch::Transport::Transport::ServerError] If the
113
+ # query or the connection to Elasticsearch fail.
114
+ def count(query: nil)
115
+ body = query ? { query: query } : nil
116
+
117
+ JayAPI::Elasticsearch::Count.new(
118
+ client.count(**{ index: index_names, body: body }.compact)
119
+ )
120
+ end
121
+
109
122
  # Sends whatever is currently in the send queue to the Elasticsearch
110
123
  # instance and clears the queue.
111
124
  def flush
@@ -19,8 +19,8 @@ module JayAPI
19
19
  # The keys are the names of the script variables, the values the
20
20
  # paths to the metrics (relative to the parent aggregation).
21
21
  # The script will receive these variables in its +params+.
22
- # @param [JayAPI::Elasticsearch::QueryBuilder::Script] script
23
- # Script used to decide whether to keep each bucket.
22
+ # @param [JayAPI::Elasticsearch::Script] script Script used to decide
23
+ # whether to keep each bucket.
24
24
  # @param [String, nil] gap_policy Optional gap policy (e.g. "skip",
25
25
  # "insert_zeros").
26
26
  def initialize(name, buckets_path:, script:, gap_policy: nil)
@@ -19,10 +19,9 @@ module JayAPI
19
19
  # @param [String] name The name used by Elasticsearch to identify each
20
20
  # of the aggregations.
21
21
  # @param [String] field The field whose unique values should be counted.
22
- # @param [JayAPI::Elasticsearch::QueryBuilder::Script] script If a
23
- # script is given the aggregation will count the unique values
24
- # returned by the script instead of the unique values in a specific
25
- # field.
22
+ # @param [JayAPI::Elasticsearch::Script] script If a script is given
23
+ # the aggregation will count the unique values returned by the
24
+ # script instead of the unique values in a specific field.
26
25
  # @param [Integer] size By default the aggregation returns the top 10
27
26
  # unique values (the ones with the higher frequency). By specifying
28
27
  # a size this can be changed.
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'query_clause'
4
+
5
+ module JayAPI
6
+ module Elasticsearch
7
+ class QueryBuilder
8
+ class QueryClauses
9
+ # Represents an IDs query in Elasticsearch.
10
+ # Information about this type of query can be found here:
11
+ # https://www.elastic.co/docs/reference/query-languages/query-dsl/query-dsl-ids-query
12
+ class IDs < ::JayAPI::Elasticsearch::QueryBuilder::QueryClauses::QueryClause
13
+ attr_reader :ids
14
+
15
+ # @param [Array<String>] ids The ids of the documents to match.
16
+ def initialize(ids:)
17
+ super()
18
+
19
+ @ids = ids
20
+ end
21
+
22
+ # @return [Hash] The Hash that represents this query (in
23
+ # Elasticsearch's DSL)
24
+ def to_h
25
+ {
26
+ ids: {
27
+ values: ids
28
+ }
29
+ }
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'exists'
4
+ require_relative 'ids'
4
5
  require_relative 'match_all'
5
6
  require_relative 'match_none'
6
7
  require_relative 'match_phrase'
@@ -116,6 +117,18 @@ module JayAPI
116
117
  self << ::JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Regexp.new(**params)
117
118
  end
118
119
 
120
+ # Adds a +JayAPI::Elasticsearch::QueryBuilder::QueryClauses::IDs+
121
+ # clause to the Query Clauses set.
122
+ # @param [Hash] params The parameters for the +IDs+ class'
123
+ # constructor.
124
+ # @see JayAPI::Elasticsearch::QueryBuilder::QueryClauses::IDs#initialize
125
+ # @return [self] Returns itself, so that other methods can be chained.
126
+ # @raise [JayAPI::Elasticsearch::QueryBuilder::Errors::QueryBuilderError]
127
+ # If an error occurs when trying to add the query clause to the set.
128
+ def ids(**params)
129
+ self << ::JayAPI::Elasticsearch::QueryBuilder::QueryClauses::IDs.new(**params)
130
+ end
131
+
119
132
  # Adds a +JayAPI::Elasticsearch::QueryBuilder::QueryClauses::MatchAll+
120
133
  # clause to the Query Clauses set.
121
134
  # @return [self] Returns itself, so that other methods can be chained.
@@ -1,36 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative '../script'
4
+
3
5
  module JayAPI
4
6
  module Elasticsearch
5
7
  class QueryBuilder
6
8
  # Represents a scripted element in a query. This scripted element can be
7
9
  # used in different places. It can be used in a query clause, but can
8
10
  # also be used to create custom aggregations.
9
- class Script
10
- attr_reader :source, :lang, :params
11
-
12
- # @param [String] source The source for the script element.
13
- # @param [String] lang The language the script is written in.
14
- # @param [Hash] params A +Hash+ with key-value pairs for the script's
15
- # parameters.
16
- def initialize(source:, lang: 'painless', params: nil)
17
- @source = source
18
- @lang = lang
19
-
20
- # Keeps the parameters from being modified from the outside after the
21
- # class has been initialized.
22
- @params = params.dup.freeze
23
- end
24
-
25
- # @return [Hash] The hash representation of the scripted element.
26
- def to_h
27
- {
28
- source: source,
29
- lang: lang,
30
- params: params
31
- }.compact
32
- end
33
- end
11
+ # @deprecated Use +JayAPI::Elasticsearch::Script+ instead.
12
+ Script = JayAPI::Elasticsearch::Script
34
13
  end
35
14
  end
36
15
  end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JayAPI
4
+ module Elasticsearch
5
+ # Represents a scripted element in Elasticsearch. This scripted element
6
+ # can be used in different places. It can be used in a query clause, but can
7
+ # also be used to create custom aggregations or when updating documents.
8
+ class Script
9
+ attr_reader :source, :lang, :params
10
+
11
+ # @param [String] source The source for the script element.
12
+ # @param [String] lang The language the script is written in.
13
+ # @param [Hash] params A +Hash+ with key-value pairs for the script's
14
+ # parameters.
15
+ def initialize(source:, lang: 'painless', params: nil)
16
+ @source = source
17
+ @lang = lang
18
+
19
+ # Keeps the parameters from being modified from the outside after the
20
+ # class has been initialized.
21
+ @params = params.dup.freeze
22
+ end
23
+
24
+ # @return [Hash] The hash representation of the scripted element.
25
+ def to_h
26
+ {
27
+ source: source,
28
+ lang: lang,
29
+ params: params
30
+ }.compact
31
+ end
32
+ end
33
+ end
34
+ end
@@ -5,6 +5,7 @@ require_relative 'elasticsearch/batch_counter'
5
5
  require_relative 'elasticsearch/client'
6
6
  require_relative 'elasticsearch/client_factory'
7
7
  require_relative 'elasticsearch/cluster'
8
+ require_relative 'elasticsearch/count'
8
9
  require_relative 'elasticsearch/errors'
9
10
  require_relative 'elasticsearch/index'
10
11
  require_relative 'elasticsearch/indexes'
@@ -13,6 +14,7 @@ require_relative 'elasticsearch/mixins'
13
14
  require_relative 'elasticsearch/query_builder'
14
15
  require_relative 'elasticsearch/query_results'
15
16
  require_relative 'elasticsearch/response'
17
+ require_relative 'elasticsearch/script'
16
18
  require_relative 'elasticsearch/search_after_results'
17
19
  require_relative 'elasticsearch/stats'
18
20
  require_relative 'elasticsearch/tasks'
@@ -2,5 +2,5 @@
2
2
 
3
3
  module JayAPI
4
4
  # JayAPI gem's semantic version
5
- VERSION = '29.7.0'
5
+ VERSION = '29.9.0'
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jay_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 29.7.0
4
+ version: 29.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Accenture-Industry X
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2026-04-28 00:00:00.000000000 Z
12
+ date: 2026-08-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -122,6 +122,7 @@ files:
122
122
  - lib/jay_api/elasticsearch/client.rb
123
123
  - lib/jay_api/elasticsearch/client_factory.rb
124
124
  - lib/jay_api/elasticsearch/cluster.rb
125
+ - lib/jay_api/elasticsearch/count.rb
125
126
  - lib/jay_api/elasticsearch/errors.rb
126
127
  - lib/jay_api/elasticsearch/errors/elasticsearch_error.rb
127
128
  - lib/jay_api/elasticsearch/errors/end_of_query_results_error.rb
@@ -162,6 +163,7 @@ files:
162
163
  - lib/jay_api/elasticsearch/query_builder/query_clauses.rb
163
164
  - lib/jay_api/elasticsearch/query_builder/query_clauses/bool.rb
164
165
  - lib/jay_api/elasticsearch/query_builder/query_clauses/exists.rb
166
+ - lib/jay_api/elasticsearch/query_builder/query_clauses/ids.rb
165
167
  - lib/jay_api/elasticsearch/query_builder/query_clauses/match_all.rb
166
168
  - lib/jay_api/elasticsearch/query_builder/query_clauses/match_clauses.rb
167
169
  - lib/jay_api/elasticsearch/query_builder/query_clauses/match_none.rb
@@ -177,6 +179,7 @@ files:
177
179
  - lib/jay_api/elasticsearch/query_builder/script.rb
178
180
  - lib/jay_api/elasticsearch/query_results.rb
179
181
  - lib/jay_api/elasticsearch/response.rb
182
+ - lib/jay_api/elasticsearch/script.rb
180
183
  - lib/jay_api/elasticsearch/search_after_results.rb
181
184
  - lib/jay_api/elasticsearch/stats.rb
182
185
  - lib/jay_api/elasticsearch/stats/errors/stats_data_not_available.rb