esse 0.4.0.rc1 → 0.4.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f6023e2f4fe6b5d8e45dd09e7c98bb250d8862ea3adc769f119011efc07191c5
4
- data.tar.gz: 852a46c60435d7c0848254d6b79a4a29ee89efe9e0700975d7d651660ad874d8
3
+ metadata.gz: 378f933ddf41a9d403121e9272b87814a01ee9e462eea5b9ad556d1ba593e46a
4
+ data.tar.gz: b3551d3361c380b750ca4e52c065d3372e6ec0c5762526f0f65894c097c79163
5
5
  SHA512:
6
- metadata.gz: 40b5c6d0ed7ff8ce793cd8be088c01f9377c62cf58366b201c8ee988f04e9f2f3e53118a48c165857394dc100c26fd06b300d653f2afdead076430d19210c554
7
- data.tar.gz: f3b4f4acadea569ef1108c1d3d3a2745c965177c29f307e5076357d89aec1f050f778933a350ccb75c0128a295d0f1349899452ea041551fae8ae574fb75793d
6
+ metadata.gz: f70d6495c34e176226a16e22da5e04b3ac74cce0d3469a3fa3ddf61c133a402d5577113839b0f4a52935cc71564be08930b529e25abb610c6bee1fcdadad77bb
7
+ data.tar.gz: 1cd02580736bb222d0a6e3e7a26fbdc17e925dafbc8cda97eb87fb911c85f1dac4c32543ffdde3418fade6a632722492147278f6c293184d42cae6ccc2a3ee28
data/lib/esse/document.rb CHANGED
@@ -109,10 +109,10 @@ module Esse
109
109
  end
110
110
 
111
111
  def inspect
112
- attributes = %i[id routing source].map do |attr|
113
- value = send(attr)
112
+ attributes = {id: :id, routing: :routing, source: :memoized_source}.map do |attr_name, attr_src|
113
+ value = send(attr_src)
114
114
  next unless value
115
- "#{attr}: #{value.inspect}"
115
+ "#{attr_name}: #{value.inspect}"
116
116
  rescue
117
117
  nil
118
118
  end.compact.join(', ')
@@ -131,9 +131,17 @@ module Esse
131
131
  end
132
132
 
133
133
  def mutated_source
134
- return source unless @__mutations__
134
+ return memoized_source unless @__mutations__
135
135
 
136
- @__mutated_source__ ||= source.merge(@__mutations__)
136
+ @__mutated_source__ ||= memoized_source.merge(@__mutations__)
137
+ end
138
+
139
+ protected
140
+
141
+ def memoized_source
142
+ return @__memoized_source__ if defined?(@__memoized_source__)
143
+
144
+ @__memoized_source__ = source || {}
137
145
  end
138
146
  end
139
147
  end
data/lib/esse/errors.rb CHANGED
@@ -15,6 +15,19 @@ module Esse
15
15
  end
16
16
  end
17
17
 
18
+ class BulkResponseError < ::Esse::Error
19
+ attr_reader :response
20
+
21
+ def initialize(response)
22
+ @response = response
23
+ super(response)
24
+ end
25
+
26
+ def items
27
+ response.fetch('items', []).select { |item| item.values.dig(0, 'error') }
28
+ end
29
+ end
30
+
18
31
  ES_TRANSPORT_ERRORS = {
19
32
  'MultipleChoices' => 'MultipleChoicesError', # 300
20
33
  'MovedPermanently' => 'MovedPermanentlyError', # 301
@@ -49,9 +49,7 @@ module Esse
49
49
  requests.each do |request|
50
50
  next unless request.body?
51
51
  resp = yield request
52
- if resp&.[]('errors')
53
- raise resp&.fetch('items', [])&.select { |item| item.values.first['error'] }&.join("\n")
54
- end
52
+ raise Esse::Transport::BulkResponseError.new(resp) if resp&.[]('errors')
55
53
  end
56
54
  rescue Faraday::TimeoutError, Esse::Transport::RequestTimeoutError => e
57
55
  retry_count += 1
@@ -10,8 +10,31 @@ module Esse
10
10
  def update_documents_attribute(name, ids_or_doc_headers = [], kwargs = {})
11
11
  batch = documents_for_lazy_attribute(name, ids_or_doc_headers)
12
12
  return if batch.empty?
13
+ kwargs = kwargs.transform_keys(&:to_sym)
13
14
 
14
- index.bulk(**kwargs.transform_keys(&:to_sym), update: batch)
15
+ if kwargs.delete(:index_on_missing) { true }
16
+ begin
17
+ index.bulk(**kwargs, update: batch)
18
+ rescue Esse::Transport::BulkResponseError => ex
19
+ ids = ex.items.map { |item| item.dig('update', '_id') }.compact
20
+ raise ex if ids.empty?
21
+
22
+ each_serialized_batch(eager_load_lazy_attributes: false, preload_lazy_attributes: false, id: ids) do |entries|
23
+ entries.each do |entry|
24
+ partial_doc = batch.find { |doc| doc.eql?(entry, match_lazy_doc_header: true) }
25
+ next unless partial_doc
26
+
27
+ partial_doc.source.each do |attr_name, attr_value|
28
+ entry.mutate(attr_name) { attr_value }
29
+ end
30
+ end
31
+
32
+ index.bulk(**kwargs, index: entries)
33
+ end
34
+ end
35
+ else
36
+ index.bulk(**kwargs, update: batch)
37
+ end
15
38
  end
16
39
 
17
40
  def documents_for_lazy_attribute(name, ids_or_doc_headers)
data/lib/esse/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Esse
4
- VERSION = '0.4.0.rc1'
4
+ VERSION = '0.4.0.rc2'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: esse
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0.rc1
4
+ version: 0.4.0.rc2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcos G. Zimmermann
8
8
  autorequire:
9
9
  bindir: exec
10
10
  cert_chain: []
11
- date: 2024-08-26 00:00:00.000000000 Z
11
+ date: 2024-09-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json