esse 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/lib/esse/cli/event_listener.rb +4 -5
  3. data/lib/esse/cli/generate.rb +14 -16
  4. data/lib/esse/cli/index/close.rb +1 -1
  5. data/lib/esse/cli/index/create.rb +1 -1
  6. data/lib/esse/cli/index/delete.rb +1 -1
  7. data/lib/esse/cli/index/import.rb +2 -2
  8. data/lib/esse/cli/index/open.rb +1 -1
  9. data/lib/esse/cli/index/reset.rb +1 -1
  10. data/lib/esse/cli/index/update_aliases.rb +2 -2
  11. data/lib/esse/cli/index/update_mapping.rb +8 -3
  12. data/lib/esse/cli/index/update_settings.rb +1 -1
  13. data/lib/esse/cli/index.rb +9 -4
  14. data/lib/esse/cli/templates/collection.rb.erb +6 -6
  15. data/lib/esse/cli/templates/{serializer.rb.erb → document.rb.erb} +6 -6
  16. data/lib/esse/cli/templates/index.rb.erb +39 -34
  17. data/lib/esse/cli.rb +5 -0
  18. data/lib/esse/cluster.rb +38 -12
  19. data/lib/esse/core.rb +7 -3
  20. data/lib/esse/deprecations/cluster.rb +5 -5
  21. data/lib/esse/deprecations/deprecate.rb +29 -0
  22. data/lib/esse/deprecations/index.rb +21 -3
  23. data/lib/esse/deprecations/index_backend_delegator.rb +217 -0
  24. data/lib/esse/deprecations/repository.rb +19 -4
  25. data/lib/esse/deprecations/repository_backend_delegator.rb +110 -0
  26. data/lib/esse/deprecations/serializer.rb +14 -0
  27. data/lib/esse/deprecations.rb +4 -0
  28. data/lib/esse/{serializer.rb → document.rb} +17 -2
  29. data/lib/esse/dynamic_template.rb +4 -0
  30. data/lib/esse/errors.rb +8 -1
  31. data/lib/esse/events.rb +13 -5
  32. data/lib/esse/hash_document.rb +1 -1
  33. data/lib/esse/import/bulk.rb +21 -11
  34. data/lib/esse/index/aliases.rb +50 -0
  35. data/lib/esse/index/attributes.rb +14 -5
  36. data/lib/esse/index/base.rb +17 -53
  37. data/lib/esse/index/documents.rb +236 -0
  38. data/lib/esse/index/indices.rb +171 -0
  39. data/lib/esse/index/object_document_mapper.rb +0 -59
  40. data/lib/esse/index/type.rb +2 -3
  41. data/lib/esse/index.rb +4 -3
  42. data/lib/esse/null_document.rb +1 -1
  43. data/lib/esse/repository/{backend.rb → documents.rb} +2 -3
  44. data/lib/esse/repository/object_document_mapper.rb +20 -20
  45. data/lib/esse/repository.rb +1 -2
  46. data/lib/esse/search/query.rb +8 -8
  47. data/lib/esse/template_loader.rb +1 -1
  48. data/lib/esse/transport/aliases.rb +36 -0
  49. data/lib/esse/transport/documents.rb +199 -0
  50. data/lib/esse/transport/health.rb +30 -0
  51. data/lib/esse/transport/indices.rb +192 -0
  52. data/lib/esse/{client_proxy → transport}/search.rb +9 -5
  53. data/lib/esse/transport.rb +44 -0
  54. data/lib/esse/version.rb +1 -1
  55. metadata +28 -28
  56. data/lib/esse/backend/index/aliases.rb +0 -73
  57. data/lib/esse/backend/index/close.rb +0 -54
  58. data/lib/esse/backend/index/create.rb +0 -67
  59. data/lib/esse/backend/index/delete.rb +0 -39
  60. data/lib/esse/backend/index/documents.rb +0 -270
  61. data/lib/esse/backend/index/existance.rb +0 -22
  62. data/lib/esse/backend/index/open.rb +0 -54
  63. data/lib/esse/backend/index/refresh.rb +0 -45
  64. data/lib/esse/backend/index/reset.rb +0 -33
  65. data/lib/esse/backend/index/update.rb +0 -143
  66. data/lib/esse/backend/index.rb +0 -56
  67. data/lib/esse/backend/repository_backend.rb +0 -105
  68. data/lib/esse/client_proxy.rb +0 -32
  69. data/lib/esse/index/backend.rb +0 -14
@@ -1,270 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Esse
4
- module Backend
5
- class Index
6
- module InstanceMethods
7
- # Resolve collection and index data
8
- #
9
- # @param types [Array<String>] List of document types. Defaults to all types.
10
- # @param options [Hash] Hash of paramenters that will be passed along to elasticsearch request
11
- # @option [String, nil] :suffix The index suffix. Defaults to the nil.
12
- # @option [Hash] :context The collection context. This value will be passed as argument to the collection
13
- # May be SQL condition or any other filter you have defined on the collection.
14
- # @return [Numeric] The number of documents imported
15
- def import(*types, context: {}, suffix: nil, **options)
16
- types = repo_hash.keys if types.empty?
17
- count = 0
18
- types.each do |type|
19
- each_serialized_batch(type, **(context || {})) do |batch|
20
- bulk(type: type, index: batch, suffix: suffix, **options)
21
- count += batch.size
22
- end
23
- end
24
- count
25
- end
26
- alias_method :import!, :import
27
-
28
- # Performs multiple indexing or delete operations in a single API call.
29
- # This reduces overhead and can greatly increase indexing speed.
30
- #
31
- # @param options [Hash] Hash of paramenters that will be passed along to elasticsearch request
32
- # @option [String, nil] :suffix The index suffix. Defaults to the nil.
33
- # @option [Array] :index list of serialized documents to be indexed(Optional)
34
- # @option [Array] :delete list of serialized documents to be deleted(Optional)
35
- # @option [Array] :create list of serialized documents to be created(Optional)
36
- # @option [String, NilClass] :type The type of the document (Optional for elasticsearch >= 7)
37
- # @return [Hash, nil] the elasticsearch response or nil if there is no data.
38
- #
39
- # @see https://www.elastic.co/guide/en/elasticsearch/reference/7.5/docs-bulk.html
40
- # @see https://github.com/elastic/elasticsearch-ruby/blob/main/elasticsearch-api/lib/elasticsearch/api/utils.rb
41
- # @see https://github.com/elastic/elasticsearch-ruby/blob/main/elasticsearch-api/lib/elasticsearch/api/actions/bulk.rb
42
- def bulk(index: nil, delete: nil, create: nil, type: nil, suffix: nil, **options)
43
- definition = {
44
- index: index_name(suffix: suffix),
45
- }.merge(options)
46
- definition[:type] = type if document_type?
47
-
48
- Esse::Import::Bulk.new(
49
- index: index,
50
- delete: delete,
51
- create: create,
52
- ).each_request do |request_body|
53
- Esse::Events.instrument('elasticsearch.bulk') do |payload|
54
- payload[:request] = definition.merge(body_stats: request_body.stats)
55
- payload[:response] = resp = coerce_exception { client.bulk(**definition, body: request_body.body) }
56
- # @todo move it to a BulkRequest class
57
- if resp&.[]('errors')
58
- payload[:error] = resp['errors']
59
- raise resp&.fetch('items', [])&.select { |item| item.values.first['error'] }.join("\n")
60
- end
61
- if bulk_wait_interval > 0
62
- payload[:wait_interval] = bulk_wait_interval
63
- sleep(bulk_wait_interval)
64
- else
65
- payload[:wait_interval] = 0.0
66
- end
67
- resp
68
- end
69
- end
70
- end
71
- alias_method :bulk!, :bulk
72
-
73
- # Adds a JSON document to the specified index and makes it searchable. If the document
74
- # already exists, updates the document and increments its version.
75
- #
76
- # UsersIndex::User.index(id: 1, body: { name: 'name' }) # { '_id' => 1, ...}
77
- #
78
- # @param options [Hash] Hash of paramenters that will be passed along to elasticsearch request
79
- # @option [String, Integer] :id The `_id` of the elasticsearch document
80
- # @option [Hash] :body The JSON document that will be indexed (Required)
81
- # @option [String, NilClass] :type The type of the document (Optional for elasticsearch >= 7)
82
- # @option [String, nil] :suffix The index suffix. Defaults to the nil.
83
- # @return [Hash] the elasticsearch response Hash
84
- #
85
- # @see https://www.elastic.co/guide/en/elasticsearch/reference/7.5/docs-index_.html
86
- # @todo update to allow serialized document as parameter
87
- def index(id:, body:, type: nil, suffix: nil, **options)
88
- params = {
89
- index: index_name(suffix: suffix),
90
- id: id,
91
- body: body,
92
- }
93
- params[:type] = type if document_type?
94
- coerce_exception { client.index(**options, **params) }
95
- end
96
- alias_method :index!, :index
97
-
98
- # Updates a document using the specified script.
99
- #
100
- # UsersIndex::User.update!(id: 1, body: { doc: { ... } }) # { '_id' => 1, ...}
101
- #
102
- # @param options [Hash] Hash of paramenters that will be passed along to elasticsearch request
103
- # @option [String, Integer] :id The `_id` of the elasticsearch document
104
- # @option [Hash] :body the body of the request
105
- # @option [String, NilClass] :type The type of the document (Optional for elasticsearch >= 7)
106
- # @option [String, nil] :suffix The index suffix. Defaults to the nil.
107
- # @raise [Esse::Backend::NotFoundError] when the doc does not exist
108
- # @return [Hash] elasticsearch response hash
109
- #
110
- # @see https://www.elastic.co/guide/en/elasticsearch/reference/7.5/docs-update.html
111
- # @todo update to allow serialized document as parameter
112
- def update!(id:, body:, type: nil, suffix: nil, **options)
113
- params = {
114
- index: index_name(suffix: suffix),
115
- id: id,
116
- body: body,
117
- }
118
- params[:type] = type if document_type?
119
- coerce_exception { client.update(**options, **params) }
120
- end
121
-
122
- # Updates a document using the specified script.
123
- #
124
- # UsersIndex::User.update(id: 1, body: { doc: { ... } }) # { '_id' => 1, ...}
125
- #
126
- # @param options [Hash] Hash of paramenters that will be passed along to elasticsearch request
127
- # @option [String, Integer] :id The `_id` of the elasticsearch document
128
- # @option [String, NilClass] :type The type of the document (Optional for elasticsearch >= 7)
129
- # @option [Hash] :body the body of the request
130
- # @option [String, nil] :suffix The index suffix. Defaults to the nil.
131
- # @return [Hash] the elasticsearch response, or an hash with 'errors' as true in case of failure
132
- #
133
- # @see https://www.elastic.co/guide/en/elasticsearch/reference/7.5/docs-update.html
134
- # @todo update to allow serialized document as parameter
135
- def update(id:, body:, suffix: nil, **options)
136
- update!(id: id, body: body, suffix: suffix, **options)
137
- rescue NotFoundError
138
- { 'errors' => true }
139
- end
140
-
141
- # Removes a JSON document from the specified index.
142
- #
143
- # UsersIndex::User.delete!(id: 1) # true
144
- # UsersIndex::User.delete!(id: 'missing') # raise Esse::Backend::NotFoundError
145
- #
146
- # @param options [Hash] Hash of paramenters that will be passed along to elasticsearch request
147
- # @option [String, Integer] :id The `_id` of the elasticsearch document
148
- # @option [String, NilClass] :type The type of the document (Optional for elasticsearch >= 7)
149
- # @option [String, nil] :suffix The index suffix. Defaults to the nil.
150
- # @raise [Esse::Backend::NotFoundError] when the doc does not exist
151
- # @return [Boolean] true when the operation is successfully completed
152
- #
153
- # @see https://www.elastic.co/guide/en/elasticsearch/reference/7.5/docs-delete.html
154
- # @todo update to allow serialized document as parameter
155
- def delete!(id:, type: nil, suffix: nil, **options)
156
- params = {
157
- index: index_name(suffix: suffix),
158
- id: id,
159
- }
160
- params[:type] = type if document_type?
161
- coerce_exception { client.delete(**options, **params) }
162
- end
163
-
164
- # Removes a JSON document from the specified index.
165
- #
166
- # UsersIndex::User.delete(id: 1) # true
167
- # UsersIndex::User.delete(id: 'missing') # false
168
- #
169
- # @param options [Hash] Hash of paramenters that will be passed along to elasticsearch request
170
- # @option [String, Integer] :id The `_id` of the elasticsearch document
171
- # @option [String, NilClass] :type The type of the document (Optional for elasticsearch >= 7)
172
- # @option [String, nil] :suffix The index suffix. Defaults to the nil.
173
- # @raise [Esse::Backend::NotFoundError] when the doc does not exist
174
- # @return [Boolean] true when the operation is successfully completed
175
- #
176
- # @see https://www.elastic.co/guide/en/elasticsearch/reference/7.5/docs-delete.html
177
- # @todo update to allow serialized document as parameter
178
- def delete(id:, type: nil, suffix: nil, **options)
179
- delete!(id: id, type: type, suffix: suffix, **options)
180
- rescue NotFoundError
181
- false
182
- end
183
-
184
- # Gets the number of matches for a search query.
185
- #
186
- # UsersIndex::User.count # 999
187
- # UsersIndex::User.count(body: { ... }) # 32
188
- #
189
- # @param options [Hash] Hash of paramenters that will be passed along to elasticsearch request
190
- # @option [Hash] :body A query to restrict the results specified with the Query DSL (optional)
191
- # @option [String, NilClass] :type The type of the document (Optional for elasticsearch >= 7)
192
- # @option [String, nil] :suffix The index suffix. Defaults to the nil.
193
- # @return [Integer] amount of documents found
194
- #
195
- # @see https://www.elastic.co/guide/en/elasticsearch/reference/7.5/search-count.html
196
- def count(type: nil, suffix: nil, **options)
197
- params = {
198
- index: index_name(suffix: suffix),
199
- }
200
- params[:type] = type if document_type?
201
- response = coerce_exception { client.count(**options, **params) }
202
- response['count']
203
- rescue NotFoundError
204
- 0
205
- end
206
-
207
- # Check if a JSON document exists
208
- #
209
- # UsersIndex::User.elasticsearch.exist?(id: 1) # true
210
- # UsersIndex::User.elasticsearch.exist?(id: 'missing') # false
211
- #
212
- # @param options [Hash] Hash of paramenters that will be passed along to elasticsearch request
213
- # @option [String, Integer] :id The `_id` of the elasticsearch document
214
- # @option [String, NilClass] :type The type of the document (Optional for elasticsearch >= 7)
215
- # @option [String, nil] :suffix The index suffix. Defaults to the nil.
216
- # @return [Boolean] true if the document exists
217
- def exist?(id:, type: nil, suffix: nil, **options)
218
- params = {
219
- index: index_name(suffix: suffix),
220
- id: id,
221
- }
222
- params[:type] = type if document_type?
223
- coerce_exception { client.exists(**options, **params) }
224
- end
225
-
226
- # Retrieves the specified JSON document from an index.
227
- #
228
- # UsersIndex::User.find!(id: 1) # { '_id' => 1, ... }
229
- # UsersIndex::User.find!(id: 'missing') # raise Esse::Backend::NotFoundError
230
- #
231
- # @param options [Hash] Hash of paramenters that will be passed along to elasticsearch request
232
- # @option [String, Integer] :id The `_id` of the elasticsearch document
233
- # @option [String, NilClass] :type The type of the document (Optional for elasticsearch >= 7)
234
- # @option [String, nil] :suffix The index suffix. Defaults to the nil.
235
- # @raise [Esse::Backend::NotFoundError] when the doc does not exist
236
- # @return [Hash] The elasticsearch document.
237
- #
238
- # @see https://www.elastic.co/guide/en/elasticsearch/reference/7.5/docs-get.html
239
- def find!(id:, type: nil, suffix: nil, **options)
240
- params = {
241
- index: index_name(suffix: suffix),
242
- id: id,
243
- }
244
- params[:type] = type if document_type?
245
- coerce_exception { client.get(**options, **params) }
246
- end
247
-
248
- # Retrieves the specified JSON document from an index.
249
- #
250
- # UsersIndex::User.find(id: 1) # { '_id' => 1, ... }
251
- # UsersIndex::User.find(id: 'missing') # nil
252
- #
253
- # @param options [Hash] Hash of paramenters that will be passed along to elasticsearch request
254
- # @option [String, Integer] :id The `_id` of the elasticsearch document
255
- # @option [String, NilClass] :type The type of the document (Optional for elasticsearch >= 7)
256
- # @option [String, nil] :suffix The index suffix. Defaults to the nil.
257
- # @return [Hash, nil] The elasticsearch document
258
- #
259
- # @see https://www.elastic.co/guide/en/elasticsearch/reference/7.5/docs-get.html
260
- def find(id:, suffix: nil, **options)
261
- find!(id: id, suffix: suffix, **options)
262
- rescue NotFoundError
263
- nil
264
- end
265
- end
266
-
267
- include InstanceMethods
268
- end
269
- end
270
- end
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Esse
4
- module Backend
5
- class Index
6
- module InstanceMethods
7
- # Checks the index existance. Returns true or false
8
- #
9
- # UsersIndex.elasticsearch.index_exist? #=> true
10
- #
11
- # @param options [Hash] Options hash
12
- # @option options [String, nil] :suffix The index suffix. Defaults to the index_version.
13
- # Use nil if you want to check existence of the `index_name` index or alias.
14
- def index_exist?(suffix: index_version)
15
- coerce_exception { client.indices.exists(index: index_name(suffix: suffix)) }
16
- end
17
- end
18
-
19
- include InstanceMethods
20
- end
21
- end
22
- end
@@ -1,54 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Esse
4
- module Backend
5
- class Index
6
- module InstanceMethods
7
- # Open a previously closed index
8
- #
9
- # @option options [String, nil] :suffix The index suffix. Defaults to the index_version.
10
- # Use nil if you want to check existence of the `index_name` index or alias.
11
- # @option options [String] :expand_wildcards Whether to expand wildcard expression to concrete indices that
12
- # are open, closed or both. (options: open, closed)
13
- # @option options [String] :ignore_indices When performed on multiple indices, allows to ignore
14
- # `missing` ones (options: none, missing) @until 1.0
15
- # @option options [Boolean] :ignore_unavailable Whether specified concrete indices should be ignored when
16
- # unavailable (missing, closed, etc)
17
- # @option options [Time] :timeout Explicit operation timeout
18
- # @raise [Esse::Backend::ServerError]
19
- # in case of failure
20
- # @return [Hash] the elasticsearch response
21
- #
22
- # @see https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-open-open.html
23
- def open!(suffix: index_version, **options)
24
- Esse::Events.instrument('elasticsearch.open') do |payload|
25
- payload[:request] = attributes = options.merge(index: index_name(suffix: suffix))
26
- payload[:response] = coerce_exception { client.indices.open(**attributes) }
27
- end
28
- end
29
-
30
- # Open a previously closed index
31
- #
32
- # @option options [String, nil] :suffix The index suffix. Defaults to the index_version.
33
- # Use nil if you want to check existence of the `index_name` index or alias.
34
- # @option options [String] :expand_wildcards Whether to expand wildcard expression to concrete indices that
35
- # are open, closed or both. (options: open, closed)
36
- # @option options [String] :ignore_indices When performed on multiple indices, allows to ignore
37
- # `missing` ones (options: none, missing) @until 1.0
38
- # @option options [Boolean] :ignore_unavailable Whether specified concrete indices should be ignored when
39
- # unavailable (missing, closed, etc)
40
- # @option options [Time] :timeout Explicit operation timeout
41
- # @return [Hash] the elasticsearch response, or an hash with 'errors' as true in case of failure
42
- #
43
- # @see https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-open-open.html
44
- def open(suffix: index_version, **options)
45
- open!(suffix: suffix, **options)
46
- rescue ServerError
47
- { 'errors' => true }
48
- end
49
- end
50
-
51
- include InstanceMethods
52
- end
53
- end
54
- end
@@ -1,45 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Esse
4
- module Backend
5
- class Index
6
- module InstanceMethods
7
- # Performs the refresh operation in one or more indices.
8
- #
9
- # @note The refresh operation can adversely affect indexing throughput when used too frequently.
10
- # @param :suffix [String, nil] :suffix The index suffix. Defaults to the index_version.
11
- # A uniq index name will be generated if one index already exist with the given alias.
12
- # @param options [Hash] Options hash
13
- # @raise [Esse::Backend::ServerError]
14
- # in case of failure
15
- # @return [Hash] the elasticsearch response
16
- #
17
- # @see https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-refresh.html
18
- def refresh!(suffix: index_version, **options)
19
- coerce_exception do
20
- client.indices.refresh(
21
- options.merge(index: index_name(suffix: suffix)),
22
- )
23
- end
24
- end
25
-
26
- # Performs the refresh operation in one or more indices.
27
- #
28
- # @note The refresh operation can adversely affect indexing throughput when used too frequently.
29
- # @param :suffix [String, nil] :suffix The index suffix. Defaults to the index_version.
30
- # A uniq index name will be generated if one index already exist with the given alias.
31
- # @param options [Hash] Options hash
32
- # @return [Hash] the elasticsearch response, or an hash with 'errors' as true in case of failure
33
- #
34
- # @see https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-refresh.html
35
- def refresh(suffix: index_version, **options)
36
- refresh!(suffix: suffix, **options)
37
- rescue ServerError
38
- { 'errors' => true }
39
- end
40
- end
41
-
42
- include InstanceMethods
43
- end
44
- end
45
- end
@@ -1,33 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Esse
4
- module Backend
5
- class Index
6
- module InstanceMethods
7
- # Deletes, creates and imports data to the index. Performs zero-downtime index resetting.
8
- #
9
- # @option options [String, nil] :suffix The index suffix. Defaults to the index_version.
10
- # A uniq index name will be generated if one index already exist with the given alias.
11
- # @option options [Time] :timeout Explicit operation timeout
12
- # @raise [Esse::Backend::ServerError]
13
- # in case of failure
14
- # @return [Hash] the elasticsearch response
15
- #
16
- # @see https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-open-close.html
17
- def reset_index!(suffix: index_version, import: true, **options)
18
- existing = []
19
- suffix ||= Esse.timestamp
20
- suffix = Esse.timestamp while index_exist?(suffix: suffix).tap { |exist| existing << suffix if exist }
21
-
22
- create_index!(**options, suffix: suffix, alias: false)
23
- import!(**options, suffix: suffix) if import
24
- update_aliases!(suffix: suffix)
25
- existing.each { |_s| delete_index!(**options, suffix: suffix) }
26
- true
27
- end
28
- end
29
-
30
- include InstanceMethods
31
- end
32
- end
33
- end
@@ -1,143 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Esse
4
- module Backend
5
- class Index
6
- module InstanceMethods
7
- # Create or update a mapping
8
- #
9
- # @option options [String] :type The name of the document type. This field is required for some elasticsearch versions
10
- # @option options [Boolean] :ignore_conflicts Specify whether to ignore conflicts while updating the mapping
11
- # (default: false)
12
- # @option options [Boolean] :allow_no_indices Whether to ignore if a wildcard indices expression resolves into
13
- # no concrete indices. (This includes `_all` string or when no indices have been specified)
14
- # @option options [String] :expand_wildcards Whether to expand wildcard expression to concrete indices that
15
- # are open, closed or both. (options: open, closed)
16
- # @option options [String] :ignore_indices When performed on multiple indices, allows to ignore
17
- # `missing` ones (options: none, missing) @until 1.0
18
- # @option options [Boolean] :ignore_unavailable Whether specified concrete indices should be ignored when
19
- # unavailable (missing, closed, etc)
20
- # @option options [Boolean] :update_all_types Whether to update the mapping for all fields
21
- # with the same name across all types
22
- # @option options [Time] :timeout Explicit operation timeout
23
- # @option options [Boolean] :master_timeout Timeout for connection to master
24
- # @raise [Esse::Backend::ServerError]
25
- # in case of failure
26
- # @return [Hash] the elasticsearch response
27
- #
28
- # @see http://www.elasticsearch.org/guide/reference/api/admin-indices-put-mapping/
29
- def update_mapping!(suffix: index_version, **options)
30
- Esse::Events.instrument('elasticsearch.update_mapping') do |payload|
31
- body = mappings_hash.fetch(Esse::MAPPING_ROOT_KEY)
32
- if (type = options[:type])
33
- body = body[type.to_s] || body[type.to_sym]
34
- end
35
- payload[:request] = opts = options.merge(index: index_name(suffix: suffix), body: body)
36
- payload[:response] = coerce_exception { client.indices.put_mapping(**opts) }
37
- end
38
- end
39
-
40
- # Create or update a mapping
41
- #
42
- # @option options [String] :type The name of the document type. This field is required for some elasticsearch versions
43
- # @option options [Boolean] :ignore_conflicts Specify whether to ignore conflicts while updating the mapping
44
- # (default: false)
45
- # @option options [Boolean] :allow_no_indices Whether to ignore if a wildcard indices expression resolves into
46
- # no concrete indices. (This includes `_all` string or when no indices have been specified)
47
- # @option options [String] :expand_wildcards Whether to expand wildcard expression to concrete indices that
48
- # are open, closed or both. (options: open, closed)
49
- # @option options [String] :ignore_indices When performed on multiple indices, allows to ignore
50
- # `missing` ones (options: none, missing) @until 1.0
51
- # @option options [Boolean] :ignore_unavailable Whether specified concrete indices should be ignored when
52
- # unavailable (missing, closed, etc)
53
- # @option options [Boolean] :update_all_types Whether to update the mapping for all fields
54
- # with the same name across all types
55
- # @option options [Time] :timeout Explicit operation timeout
56
- # @option options [Boolean] :master_timeout Timeout for connection to master
57
- # @return [Hash] the elasticsearch response, or an hash with 'errors' as true in case of failure
58
- #
59
- # @see http://www.elasticsearch.org/guide/reference/api/admin-indices-put-mapping/
60
- def update_mapping(suffix: index_version, **options)
61
- update_mapping!(suffix: suffix, **options)
62
- rescue ServerError
63
- { 'errors' => true }
64
- end
65
-
66
- # Closes the index for read/write operations, updates the index settings, and open it again
67
- #
68
- # @option options [String] :expand_wildcards Whether to expand wildcard expression to concrete indices that
69
- # are open, closed or both. (options: open, closed)
70
- # @option options [String] :ignore_indices When performed on multiple indices, allows to ignore
71
- # `missing` ones (options: none, missing) @until 1.0
72
- # @option options [Boolean] :ignore_unavailable Whether specified concrete indices should be ignored when
73
- # unavailable (missing, closed, etc)
74
- # @option options [Boolean] :include_defaults Whether to return all default clusters setting
75
- # @option options [Boolean] :preserve_existing Whether to update existing settings.
76
- # If set to `true` existing settings on an index remain unchanged, the default is `false`
77
- # @option options [Time] :master_timeout Specify timeout for connection to master
78
- # @option options [Boolean] :flat_settings Return settings in flat format (default: false)
79
- # @raise [Esse::Backend::ServerError]
80
- # in case of failure
81
- # @return [Hash] the elasticsearch response
82
- #
83
- # @see http://www.elasticsearch.org/guide/reference/api/admin-indices-update-settings/
84
- def update_settings!(suffix: index_version, **options)
85
- response = nil
86
-
87
- settings = settings_hash.fetch(Esse::SETTING_ROOT_KEY).transform_keys(&:to_s)
88
- settings.delete('number_of_shards') # Can't change number of shards for an index
89
- analysis = settings.delete('analysis')
90
-
91
- if settings.any?
92
- # When changing the number of replicas the index needs to be open. Changing the number of replicas on a
93
- # closed index might prevent the index to be opened correctly again.
94
- Esse::Events.instrument('elasticsearch.update_settings') do |payload|
95
- payload[:request] = opts = options.merge(index: index_name(suffix: suffix), body: { index: settings })
96
- payload[:response] = response = coerce_exception { client.indices.put_settings(**opts) }
97
- end
98
- end
99
-
100
- if analysis
101
- # It is also possible to define new analyzers for the index. But it is required to close the
102
- # index first and open it after the changes are made.
103
- close!(suffix: suffix)
104
- begin
105
- Esse::Events.instrument('elasticsearch.update_settings') do |payload|
106
- payload[:request] = opts = options.merge(index: index_name(suffix: suffix), body: { analysis: analysis })
107
- payload[:response] = response = coerce_exception { client.indices.put_settings(**opts) }
108
- end
109
- ensure
110
- open!(suffix: suffix)
111
- end
112
- end
113
-
114
- response
115
- end
116
-
117
- # Closes the index for read/write operations, updates the index settings, and open it again
118
- #
119
- # @option options [String] :expand_wildcards Whether to expand wildcard expression to concrete indices that
120
- # are open, closed or both. (options: open, closed)
121
- # @option options [String] :ignore_indices When performed on multiple indices, allows to ignore
122
- # `missing` ones (options: none, missing) @until 1.0
123
- # @option options [Boolean] :ignore_unavailable Whether specified concrete indices should be ignored when
124
- # unavailable (missing, closed, etc)
125
- # @option options [Boolean] :include_defaults Whether to return all default clusters setting
126
- # @option options [Boolean] :preserve_existing Whether to update existing settings.
127
- # If set to `true` existing settings on an index remain unchanged, the default is `false`
128
- # @option options [Time] :master_timeout Specify timeout for connection to master
129
- # @option options [Boolean] :flat_settings Return settings in flat format (default: false)
130
- # @return [Hash] the elasticsearch response, or an hash with 'errors' as true in case of failure
131
- #
132
- # @see http://www.elasticsearch.org/guide/reference/api/admin-indices-update-settings/
133
- def update_settings(suffix: index_version, **options)
134
- update_settings!(suffix: suffix, **options)
135
- rescue ServerError
136
- { 'errors' => true }
137
- end
138
- end
139
-
140
- include InstanceMethods
141
- end
142
- end
143
- end
@@ -1,56 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'forwardable'
4
-
5
- module Esse
6
- module Backend
7
- class Index
8
- require_relative 'index/aliases'
9
- require_relative 'index/create'
10
- require_relative 'index/delete'
11
- require_relative 'index/existance'
12
- require_relative 'index/update'
13
- require_relative 'index/refresh'
14
- require_relative 'index/reset'
15
- require_relative 'index/documents'
16
- require_relative 'index/open'
17
- require_relative 'index/close'
18
-
19
- extend Forwardable
20
-
21
- NAMING = %i[index_version].freeze
22
- DEFINITION = %i[settings_hash mappings_hash].freeze
23
- DOCUMENTS = %i[each_serialized_batch].freeze
24
-
25
- def_delegators :@index, :index_name, :cluster, :repo_hash, :bulk_wait_interval, *(NAMING + DEFINITION + DOCUMENTS)
26
- def_delegators :cluster, :document_type?, :client
27
-
28
- def initialize(index)
29
- @index = index
30
- end
31
-
32
- protected
33
-
34
- def build_real_index_name(suffix = nil)
35
- suffix = Hstring.new(suffix).underscore.presence || index_version || Esse.timestamp
36
-
37
- index_name(suffix: suffix)
38
- end
39
-
40
- # Elasticsearch::Transport was renamed to Elastic::Transport in 8.0
41
- # This lib should support both versions that's why we are wrapping up the transport
42
- # errors to local errors.
43
- def coerce_exception
44
- yield
45
- rescue => exception
46
- name = Hstring.new(exception.class.name)
47
- if /^(Elasticsearch|Elastic|OpenSearch)?::Transport::Transport::Errors/.match?(name.value) && \
48
- (exception_class = ERRORS[name.demodulize.value])
49
- raise exception_class.new(exception.message)
50
- else
51
- raise exception
52
- end
53
- end
54
- end
55
- end
56
- end