elasticsearch_record 1.2.1 → 1.2.4

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: 32688db9e3220d919f454f7c4c9445b006d43299d66322f4586af9d426fdc907
4
- data.tar.gz: 9169c9110e662d647f7eddeddae7fa3497a03ad36486d85c001e762af5c756df
3
+ metadata.gz: 41575ee0aefe81ed3069dc81c1c0d9062ea841998a7bb4affaab885dad0a4291
4
+ data.tar.gz: 73e98d4b6aad0fda63df0fff562537245f2d4c95fb5f53d67ad891550d8e6b1b
5
5
  SHA512:
6
- metadata.gz: 9221a04be02abcb7d8f36da24f3b7e2d061b778233b8190bb14b906bf54b2a8d881a870f9c0b1b65d90b3ff2418fe8ee21d828b35dceac85b2f8bc766677c73c
7
- data.tar.gz: a1b4e64cf4a8c33d8ce3aaeb89fedd5b7847a648a1dcfccc2b15ecfba0f99d0fbf9a0c94237edcd6ef0feb8d4ef40889ab00c2b8a6bc50773b4982cd5e326149
6
+ metadata.gz: ed60e5fe591f92169d51f333c600338cd4fcee46ca6ca988625399ee522068e3b5c67738fe5b9f3fcc1cede6491cfb1b969c525d5b1af2f2ed03e93b7303237f
7
+ data.tar.gz: eb51b9dc16d0457905fb15589ff2542dfe0a8f59678459be910726305a3b0527e3ddcee6d7e5271e2db0d658dabd5f476eea71b4dca27c1f99375d987de50f0b
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- elasticsearch_record (1.2.1)
4
+ elasticsearch_record (1.2.4)
5
5
  activerecord (~> 7.0.0)
6
6
  elasticsearch (~> 8.4)
7
7
 
@@ -28,7 +28,7 @@ GEM
28
28
  elasticsearch-api (= 8.5.2)
29
29
  elasticsearch-api (8.5.2)
30
30
  multi_json
31
- faraday (2.7.1)
31
+ faraday (2.7.2)
32
32
  faraday-net_http (>= 2.0, < 3.1)
33
33
  ruby2_keywords (>= 0.0.4)
34
34
  faraday-net_http (3.0.2)
data/docs/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # ElasticsearchRecord - CHANGELOG
2
2
 
3
+ ## [1.2.4] - 2022-12-15
4
+ * [fix] missing `#visit_Arel_Nodes_In` method in `Arel::Visitors::ElasticsearchQuery` to build array conditions
5
+ * [fix] resolving buckets from relation `ElasticsearchRecord::Result#buckets` not recognizing sub-buckets
6
+
7
+ ## [1.2.3] - 2022-12-12
8
+ * [fix] `change_table` 'if_exists: true' returns at the wrong state
9
+
10
+ ## [1.2.2] - 2022-12-12
11
+ * [add] `:if_exists` option for `change_table`
12
+ * [fix] executing `_compute_table_name` irregular on some schema methods and some not - not only executes on `create_table` and `change_table`
13
+ * [ref] private `_compute_table_name` method to public `compute_table_name`
14
+ * [ref] drop `_compute_table_name` on methods: open_table, close_table, truncate_table, drop_table, block_table, unblock_table & clone_table
15
+
3
16
  ## [1.2.1] - 2022-12-12
4
17
  * [add] `ActiveRecord::ConnectionAdapters::Elasticsearch::SchemaStatements#access_id_fielddata?` which checks the clusters setting 'indices.id_field_data.enabled' to determinate if a general sorting on the +_id+-field is possible or not.
5
18
  * [add] `ElasticsearchRecord::Relation#ordered_relation` which overwrites the original method to check against the `#access_id_fielddata?` method
@@ -38,9 +38,6 @@ module ActiveRecord
38
38
  # @param [String] table_name
39
39
  # @return [Boolean] acknowledged status
40
40
  def open_table(table_name)
41
- # IMPORTANT: compute will add possible configured prefix & suffix
42
- table_name = _compute_table_name(table_name)
43
-
44
41
  schema_cache.clear_data_source_cache!(table_name)
45
42
  api(:indices, :open, { index: table_name }, 'OPEN TABLE').dig('acknowledged')
46
43
  end
@@ -59,9 +56,6 @@ module ActiveRecord
59
56
  # @param [String] table_name
60
57
  # @return [Boolean] acknowledged status
61
58
  def close_table(table_name)
62
- # IMPORTANT: compute will add possible configured prefix & suffix
63
- table_name = _compute_table_name(table_name)
64
-
65
59
  schema_cache.clear_data_source_cache!(table_name)
66
60
  api(:indices, :close, { index: table_name }, 'CLOSE TABLE').dig('acknowledged')
67
61
  end
@@ -84,9 +78,6 @@ module ActiveRecord
84
78
  # @param [String] table_name
85
79
  # @return [Boolean] acknowledged status
86
80
  def truncate_table(table_name)
87
- # IMPORTANT: compute will add possible configured prefix & suffix
88
- table_name = _compute_table_name(table_name)
89
-
90
81
  # force: automatically drops an existing index
91
82
  create_table(table_name, force: true, **table_schema(table_name))
92
83
  end
@@ -111,9 +102,6 @@ module ActiveRecord
111
102
  # @param [Boolean] if_exists
112
103
  # @return [Array] acknowledged status
113
104
  def drop_table(table_name, if_exists: false, **)
114
- # IMPORTANT: compute will add possible configured prefix & suffix
115
- table_name = _compute_table_name(table_name)
116
-
117
105
  schema_cache.clear_data_source_cache!(table_name)
118
106
  api(:indices, :delete, { index: table_name, ignore: (if_exists ? 404 : nil) }, 'DROP TABLE').dig('acknowledged')
119
107
  end
@@ -123,9 +111,6 @@ module ActiveRecord
123
111
  # @param [Symbol] block_name The block to add (one of :read, :write, :read_only or :metadata)
124
112
  # @return [Boolean] acknowledged status
125
113
  def block_table(table_name, block_name = :write)
126
- # IMPORTANT: compute will add possible configured prefix & suffix
127
- table_name = _compute_table_name(table_name)
128
-
129
114
  api(:indices, :add_block, { index: table_name, block: block_name }, "BLOCK #{block_name.to_s.upcase} TABLE").dig('acknowledged')
130
115
  end
131
116
 
@@ -135,9 +120,6 @@ module ActiveRecord
135
120
  # @param [Symbol] block_name The block to add (one of :read, :write, :read_only or :metadata)
136
121
  # @return [Boolean] acknowledged status
137
122
  def unblock_table(table_name, block_name = nil)
138
- # IMPORTANT: compute will add possible configured prefix & suffix
139
- table_name = _compute_table_name(table_name)
140
-
141
123
  if block_name.nil?
142
124
  change_table(table_name) do |t|
143
125
  t.change_setting('index.blocks.read', false)
@@ -157,10 +139,6 @@ module ActiveRecord
157
139
  # @param [Hash] options
158
140
  # @param [Proc] block
159
141
  def clone_table(table_name, target_name, **options, &block)
160
- # IMPORTANT: compute will add possible configured prefix & suffix
161
- table_name = _compute_table_name(table_name)
162
- target_name = _compute_table_name(target_name)
163
-
164
142
  # create new definition
165
143
  definition = clone_table_definition(table_name, target_name, **extract_table_options!(options), &block)
166
144
 
@@ -191,7 +169,7 @@ module ActiveRecord
191
169
  # @return [Boolean] acknowledged status
192
170
  def create_table(table_name, force: false, copy_from: nil, if_not_exists: false, **options)
193
171
  # IMPORTANT: compute will add possible configured prefix & suffix
194
- table_name = _compute_table_name(table_name)
172
+ table_name = compute_table_name(table_name)
195
173
 
196
174
  return if if_not_exists && table_exists?(table_name)
197
175
 
@@ -226,9 +204,11 @@ module ActiveRecord
226
204
  # t.mapping :name, :string
227
205
  # # Other column alterations here
228
206
  # end
229
- def change_table(table_name, **options)
207
+ def change_table(table_name, if_exists: false, **options)
230
208
  # IMPORTANT: compute will add possible configured prefix & suffix
231
- table_name = _compute_table_name(table_name)
209
+ table_name = compute_table_name(table_name)
210
+
211
+ return if if_exists && !table_exists?(table_name)
232
212
 
233
213
  definition = update_table_definition(table_name, self, **options)
234
214
 
@@ -303,17 +283,22 @@ module ActiveRecord
303
283
  _exec_change_table_with(:delete_alias, table_name, name, **options, &block)
304
284
  end
305
285
 
306
- private
307
-
308
- def _compute_table_name(table_name)
286
+ # computes a provided +table_name+ with optionally configured +table_name_prefix+ & +table_name_suffix+.
287
+ # @param [String] table_name
288
+ # @return [String]
289
+ def compute_table_name(table_name)
290
+ table_name = table_name.to_s
291
+
309
292
  # HINT: +"" creates a new +unfrozen+ string!
310
293
  str = +""
311
- str << table_name_prefix unless table_name.to_s.start_with?(table_name_prefix)
294
+ str << table_name_prefix unless table_name.start_with?(table_name_prefix)
312
295
  str << table_name
313
- str << table_name_suffix unless table_name.to_s.end_with?(table_name_suffix)
296
+ str << table_name_suffix unless table_name.end_with?(table_name_suffix)
314
297
  str
315
298
  end
316
299
 
300
+ private
301
+
317
302
  def _exec_change_table_with(method, table_name, *args, **kwargs, &block)
318
303
  change_table(table_name) do |t|
319
304
  t.send(method, *args, **kwargs, &block)
@@ -355,6 +355,23 @@ module Arel # :nodoc: all
355
355
  end
356
356
  end
357
357
 
358
+ # DIRECT ASSIGNMENT
359
+ def visit_Arel_Nodes_In(o)
360
+ self.collector.preparable = false
361
+
362
+ attr, values = o.left, o.right
363
+
364
+ if Array === values
365
+ unless values.empty?
366
+ values.delete_if { |value| unboundable?(value) }
367
+ end
368
+
369
+ return failed! if values.empty?
370
+ end
371
+
372
+ assign(:filter, [{ terms: { visit(attr) => visit(values) } }])
373
+ end
374
+
358
375
  def visit_Arel_Nodes_And(o)
359
376
  collect(o.children)
360
377
  end
@@ -393,6 +410,7 @@ module Arel # :nodoc: all
393
410
  claim(:index, o.name)
394
411
  end
395
412
 
413
+ # RAW RETURN
396
414
  def visit_Struct_Raw(o)
397
415
  o
398
416
  end
@@ -9,7 +9,7 @@ module ElasticsearchRecord
9
9
  module VERSION
10
10
  MAJOR = 1
11
11
  MINOR = 2
12
- TINY = 1
12
+ TINY = 4
13
13
  PRE = nil
14
14
 
15
15
  STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".")
@@ -72,21 +72,7 @@ module ElasticsearchRecord
72
72
  # aggregations are already a hash with key => data, but to prevent reference manipulation on the hash
73
73
  # we have to create a new one here...
74
74
  aggregations.reduce({}) { |buckets, (key, agg)|
75
- # check if this agg has a bucket
76
- if agg.key?(:buckets)
77
- buckets[key] = agg[:buckets].reduce({}) { |m, b|
78
- # buckets can be a Hash or Array (of Hashes)
79
- bucket_key, bucket = b.is_a?(Hash) ? [b[:key], b] : b
80
- m[bucket_key] = bucket.except(:key, :doc_count).transform_values { |val| val[:value] }
81
-
82
- m
83
- }
84
- elsif agg.key?(:value)
85
- buckets[key] = agg[:value]
86
- elsif agg.key?(:values)
87
- buckets[key] = agg[:values]
88
- end
89
-
75
+ buckets[key] = _resolve_bucket(agg)
90
76
  buckets
91
77
  }.with_indifferent_access
92
78
  end
@@ -247,6 +233,33 @@ module ElasticsearchRecord
247
233
  end
248
234
  end
249
235
 
236
+ # resolves bucket nodes recursively
237
+ # @param [Object] node
238
+ # @return [Object]
239
+ def _resolve_bucket(node)
240
+ # check, if node is not a hash - in this case we just return it's value
241
+ return node unless node.is_a?(Hash)
242
+
243
+ # check if the node has a bucket
244
+ if node.key?(:buckets)
245
+ node[:buckets].reduce({}) { |m, b|
246
+ # buckets can be a Hash or Array (of Hashes)
247
+ bucket_key, bucket = b.is_a?(Hash) ? [b[:key], b] : b
248
+
249
+ m[bucket_key] = _resolve_bucket(bucket)
250
+ m
251
+ }
252
+ elsif node.key?(:value)
253
+ node[:value]
254
+ elsif node.key?(:values)
255
+ node[:values]
256
+ else
257
+ # resolve sub-aggregations / nodes without 'meta' keys.
258
+ # if this results in an empty hash, the return will be nil
259
+ node.except(:key, :doc_count, :doc_count_error_upper_bound, :sum_other_doc_count).transform_values { |val| _resolve_bucket(val) }.presence
260
+ end
261
+ end
262
+
250
263
  # builds computed results (used to build ActiveRecord models)
251
264
  # @return [Array]
252
265
  def computed_results
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elasticsearch_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tobias Gonsior
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-12-12 00:00:00.000000000 Z
11
+ date: 2022-12-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord