esse 0.0.3 → 0.0.4

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: 6d7f91a4ecb008fe3d8849175cb6f1bb08605d56432b7bc7f52ca9df0c55e77e
4
- data.tar.gz: a9b4143597ca2ca571360814b381e8398bd3ac8b45134fc5b3086dfc8a33f0a1
3
+ metadata.gz: a79fdcfdbdc2c217432f128fc93fa67f543c450d9e2cfa1d7f1d57b5242f1be4
4
+ data.tar.gz: ae620c10d8930fcf86e0323d375ed96effa4c77beeb8da13cd0be2738133bc3b
5
5
  SHA512:
6
- metadata.gz: ef25dc5238beea72d67eeaf146dad16184cd8d78c76fc84516154e8ddd5cb93f2c5f1fd84574359147d6428dbdb1d25f037d0dce004f79e4e22cbbf1dcc2a30b
7
- data.tar.gz: 6c5237fc9f5b59376ef6e948132c5a78200f4f849acab591ba603b3c5173d95c6179dfec3844331a6ba30f41ae519515b12c3f3396d80e4bff5dd3445525faad
6
+ metadata.gz: 406f2d91072eb6c8e7dca67072ff5954c3fad9d94efbb20582ecec81bbe4d2080bb6c0921d2b41702745214932d3d15879469061ddca8189913f333b4ee4bb6e
7
+ data.tar.gz: e5f4aecf985af27f3a80f47188a5fa9524a935c9a382da77673d7b3ebb9c71fab2543a19f10725372ca87d2088d25e17919e73f4152000d2dcfc9a23e729a2c3
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- esse (0.0.2)
4
+ esse (0.0.4)
5
5
  elasticsearch
6
6
  multi_json
7
7
  thor (>= 0.19)
@@ -44,7 +44,7 @@ module Esse
44
44
  *indices.map do |index|
45
45
  { remove: { index: index, alias: index_name } }
46
46
  end,
47
- { add: {index: real_index_name(suffix), alias: index_name } }
47
+ { add: {index: build_real_index_name(suffix), alias: index_name } }
48
48
  ],
49
49
  }
50
50
  client.indices.update_aliases(options)
@@ -21,9 +21,7 @@ module Esse
21
21
  #
22
22
  # @see https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-open-close.html
23
23
  def close!(suffix: index_version, **options)
24
- name = suffix ? real_index_name(suffix) : index_name
25
-
26
- client.indices.close(options.merge(index: name))
24
+ client.indices.close(options.merge(index: index_name(suffix: suffix)))
27
25
  end
28
26
 
29
27
  # Close an index (keep the data on disk, but deny operations with the index).
@@ -39,7 +39,7 @@ module Esse
39
39
  # @see http://www.elasticsearch.org/blog/changing-mapping-with-zero-downtime/
40
40
  def create_index!(suffix: nil, **options)
41
41
  options = DEFAULT_OPTIONS.merge(options)
42
- name = real_index_name(suffix)
42
+ name = build_real_index_name(suffix)
43
43
  definition = [settings_hash, mappings_hash].reduce(&:merge)
44
44
 
45
45
  if options[:alias] && name != index_name
@@ -13,9 +13,7 @@ module Esse
13
13
  # @raise [Elasticsearch::Transport::Transport::Errors::NotFound] when index does not exists
14
14
  # @return [Hash] elasticsearch response
15
15
  def delete_index!(suffix:)
16
- name = suffix ? real_index_name(suffix) : index_name
17
-
18
- client.indices.delete(index: name)
16
+ client.indices.delete(index: index_name(suffix: suffix))
19
17
  end
20
18
 
21
19
  # Deletes ES index
@@ -12,8 +12,7 @@ module Esse
12
12
  # @option options [String, nil] :suffix The index suffix. Defaults to the index_version.
13
13
  # Use nil if you want to check existence of the `index_name` index or alias.
14
14
  def exist?(suffix: index_version)
15
- name = suffix ? real_index_name(suffix) : index_name
16
- client.indices.exists(index: name)
15
+ client.indices.exists(index: index_name(suffix: suffix))
17
16
  end
18
17
  end
19
18
 
@@ -21,9 +21,7 @@ module Esse
21
21
  #
22
22
  # @see https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-open-open.html
23
23
  def open!(suffix: index_version, **options)
24
- name = suffix ? real_index_name(suffix) : index_name
25
-
26
- client.indices.open(options.merge(index: name))
24
+ client.indices.open(options.merge(index: index_name(suffix: suffix)))
27
25
  end
28
26
 
29
27
  # Open a previously closed index
@@ -27,9 +27,7 @@ module Esse
27
27
  #
28
28
  # @see http://www.elasticsearch.org/guide/reference/api/admin-indices-put-mapping/
29
29
  def update_mapping!(suffix: index_version, **options)
30
- name = suffix ? real_index_name(suffix) : index_name
31
-
32
- client.indices.put_mapping(options.merge(index: name, body: mappings_hash.fetch(Esse::MAPPING_ROOT_KEY)))
30
+ client.indices.put_mapping(options.merge(index: index_name(suffix: suffix), body: mappings_hash.fetch(Esse::MAPPING_ROOT_KEY)))
33
31
  end
34
32
 
35
33
  # Create or update a mapping
@@ -78,13 +76,12 @@ module Esse
78
76
  #
79
77
  # @see http://www.elasticsearch.org/guide/reference/api/admin-indices-update-settings/
80
78
  def update_settings!(suffix: index_version, **options)
81
- name = suffix ? real_index_name(suffix) : index_name
82
79
  response = nil
83
80
 
84
81
  close!(suffix: suffix)
85
82
  begin
86
83
  body = settings_hash(cluster_settings: false).fetch(Esse::SETTING_ROOT_KEY)
87
- response = client.indices.put_settings(options.merge(index: name, body: body))
84
+ response = client.indices.put_settings(options.merge(index: index_name(suffix: suffix), body: body))
88
85
  ensure
89
86
  open!(suffix: suffix)
90
87
  end
@@ -16,7 +16,7 @@ module Esse
16
16
 
17
17
  extend Forwardable
18
18
 
19
- NAMING = %i[index_name index_version].freeze
19
+ NAMING = %i[index_version].freeze
20
20
  DEFINITION = %i[settings_hash mappings_hash].freeze
21
21
 
22
22
  def_delegators :@index, :type_hash, *(NAMING + DEFINITION)
@@ -27,9 +27,17 @@ module Esse
27
27
 
28
28
  protected
29
29
 
30
- def real_index_name(suffix = nil)
30
+ def index_name(suffix: nil)
31
+ suffix = Hstring.new(suffix).underscore.presence
32
+ return @index.index_name unless suffix
33
+
34
+ [@index.index_name, suffix].join('_')
35
+ end
36
+
37
+ def build_real_index_name(suffix = nil)
31
38
  suffix = Hstring.new(suffix).underscore.presence || index_version || Esse.timestamp
32
- [index_name, suffix].compact.join('_')
39
+
40
+ index_name(suffix: suffix)
33
41
  end
34
42
 
35
43
  def client
@@ -7,11 +7,12 @@ module Esse
7
7
  # Resolve collection and index data
8
8
  #
9
9
  # @param options [Hash] Hash of paramenters that will be passed along to elasticsearch request
10
+ # @option [String, nil] :suffix The index suffix. Defaults to the nil.
10
11
  # @option [Hash] :context The collection context. This value will be passed as argument to the collection
11
12
  # May be SQL condition or any other filter you have defined on the collection.
12
- def import(context: {}, **options)
13
+ def import(context: {}, suffix: nil, **options)
13
14
  each_serialized_batch(context || {}) do |batch|
14
- bulk(index: batch, **options)
15
+ bulk(index: batch, suffix: suffix, **options)
15
16
  end
16
17
  end
17
18
  alias import! import
@@ -20,13 +21,14 @@ module Esse
20
21
  # This reduces overhead and can greatly increase indexing speed.
21
22
  #
22
23
  # @param options [Hash] Hash of paramenters that will be passed along to elasticsearch request
24
+ # @param options [String, nil] :suffix The index suffix. Defaults to the nil.
23
25
  # @param options [Array] :index list of serialized documents to be indexed(Optional)
24
26
  # @param options [Array] :delete list of serialized documents to be deleted(Optional)
25
27
  # @param options [Array] :create list of serialized documents to be created(Optional)
26
28
  # @return [Hash, nil] the elasticsearch response or nil if there is no data.
27
29
  #
28
30
  # @see https://www.elastic.co/guide/en/elasticsearch/reference/7.5/docs-bulk.html
29
- def bulk(index: nil, delete: nil, create: nil, **options)
31
+ def bulk(index: nil, delete: nil, create: nil, suffix: nil, **options)
30
32
  body = []
31
33
  Array(index).each do |entry|
32
34
  id, data = Esse.doc_id!(entry)
@@ -44,7 +46,7 @@ module Esse
44
46
  return if body.empty?
45
47
 
46
48
  definition = {
47
- index: index_name,
49
+ index: index_name(suffix: suffix),
48
50
  type: type_name,
49
51
  body: body,
50
52
  }.merge(options)
@@ -61,12 +63,13 @@ module Esse
61
63
  # @param options [Hash] Hash of paramenters that will be passed along to elasticsearch request
62
64
  # @param options [String, Integer] :id The `_id` of the elasticsearch document
63
65
  # @param options [Hash] :body The JSON document that will be indexed (Required)
66
+ # @param options [String, nil] :suffix The index suffix. Defaults to the nil.
64
67
  # @return [Hash] the elasticsearch response Hash
65
68
  #
66
69
  # @see https://www.elastic.co/guide/en/elasticsearch/reference/7.5/docs-index_.html
67
- def index(id:, body:, **options)
70
+ def index(id:, body:, suffix: nil, **options)
68
71
  client.index(
69
- options.merge(index: index_name, type: type_name, id: id, body: body),
72
+ options.merge(index: index_name(suffix: suffix), type: type_name, id: id, body: body),
70
73
  )
71
74
  end
72
75
  alias index! index
@@ -78,13 +81,14 @@ module Esse
78
81
  # @param options [Hash] Hash of paramenters that will be passed along to elasticsearch request
79
82
  # @param options [String, Integer] :id The `_id` of the elasticsearch document
80
83
  # @param options [Hash] :body the body of the request
84
+ # @param options [String, nil] :suffix The index suffix. Defaults to the nil.
81
85
  # @raise [Elasticsearch::Transport::Transport::Errors::NotFound] when the doc does not exist
82
86
  # @return [Hash] elasticsearch response hash
83
87
  #
84
88
  # @see https://www.elastic.co/guide/en/elasticsearch/reference/7.5/docs-update.html
85
- def update!(id:, body:, **options)
89
+ def update!(id:, body:, suffix: nil, **options)
86
90
  client.update(
87
- options.merge(index: index_name, type: type_name, id: id, body: body),
91
+ options.merge(index: index_name(suffix: suffix), type: type_name, id: id, body: body),
88
92
  )
89
93
  end
90
94
 
@@ -95,11 +99,12 @@ module Esse
95
99
  # @param options [Hash] Hash of paramenters that will be passed along to elasticsearch request
96
100
  # @param options [String, Integer] :id The `_id` of the elasticsearch document
97
101
  # @param options [Hash] :body the body of the request
102
+ # @param options [String, nil] :suffix The index suffix. Defaults to the nil.
98
103
  # @return [Hash, false] the elasticsearch response hash, or false in case of failure
99
104
  #
100
105
  # @see https://www.elastic.co/guide/en/elasticsearch/reference/7.5/docs-update.html
101
- def update(id:, body:, **options)
102
- update!(id: id, body: body, **options)
106
+ def update(id:, body:, suffix: nil, **options)
107
+ update!(id: id, body: body, suffix: suffix, **options)
103
108
  rescue Elasticsearch::Transport::Transport::Errors::NotFound
104
109
  false
105
110
  end
@@ -111,12 +116,13 @@ module Esse
111
116
  #
112
117
  # @param options [Hash] Hash of paramenters that will be passed along to elasticsearch request
113
118
  # @param options [String, Integer] :id The `_id` of the elasticsearch document
119
+ # @param options [String, nil] :suffix The index suffix. Defaults to the nil.
114
120
  # @raise [Elasticsearch::Transport::Transport::Errors::NotFound] when the doc does not exist
115
121
  # @return [Boolean] true when the operation is successfully completed
116
122
  #
117
123
  # @see https://www.elastic.co/guide/en/elasticsearch/reference/7.5/docs-delete.html
118
- def delete!(id:, **options)
119
- client.delete(options.merge(index: index_name, type: type_name, id: id))
124
+ def delete!(id:, suffix: nil, **options)
125
+ client.delete(options.merge(index: index_name(suffix: suffix), type: type_name, id: id))
120
126
  end
121
127
 
122
128
  # Removes a JSON document from the specified index.
@@ -126,12 +132,13 @@ module Esse
126
132
  #
127
133
  # @param options [Hash] Hash of paramenters that will be passed along to elasticsearch request
128
134
  # @param options [String, Integer] :id The `_id` of the elasticsearch document
135
+ # @param options [String, nil] :suffix The index suffix. Defaults to the nil.
129
136
  # @raise [Elasticsearch::Transport::Transport::Errors::NotFound] when the doc does not exist
130
137
  # @return [Boolean] true when the operation is successfully completed
131
138
  #
132
139
  # @see https://www.elastic.co/guide/en/elasticsearch/reference/7.5/docs-delete.html
133
- def delete(id:, **options)
134
- delete!(id: id, **options)
140
+ def delete(id:, suffix: nil, **options)
141
+ delete!(id: id, suffix: suffix, **options)
135
142
  rescue Elasticsearch::Transport::Transport::Errors::NotFound
136
143
  false
137
144
  end
@@ -143,11 +150,12 @@ module Esse
143
150
  #
144
151
  # @param options [Hash] Hash of paramenters that will be passed along to elasticsearch request
145
152
  # @param options [Hash] :body A query to restrict the results specified with the Query DSL (optional)
153
+ # @param options [String, nil] :suffix The index suffix. Defaults to the nil.
146
154
  # @return [Integer] amount of documents found
147
155
  #
148
156
  # @see https://www.elastic.co/guide/en/elasticsearch/reference/7.5/search-count.html
149
- def count(**options)
150
- response = client.count(options.merge(index: index_name, type: type_name))
157
+ def count(suffix: nil, **options)
158
+ response = client.count(options.merge(index: index_name(suffix: suffix), type: type_name))
151
159
  response['count']
152
160
  rescue Elasticsearch::Transport::Transport::Errors::NotFound
153
161
  0
@@ -160,9 +168,10 @@ module Esse
160
168
  #
161
169
  # @param options [Hash] Hash of paramenters that will be passed along to elasticsearch request
162
170
  # @param options [String, Integer] :id The `_id` of the elasticsearch document
171
+ # @param options [String, nil] :suffix The index suffix. Defaults to the nil.
163
172
  # @return [Boolean] true if the document exists
164
- def exist?(id:, **options)
165
- client.exists(options.merge(index: index_name, type: type_name, id: id))
173
+ def exist?(id:, suffix: nil, **options)
174
+ client.exists(options.merge(index: index_name(suffix: suffix), type: type_name, id: id))
166
175
  end
167
176
 
168
177
  # Retrieves the specified JSON document from an index.
@@ -172,12 +181,13 @@ module Esse
172
181
  #
173
182
  # @param options [Hash] Hash of paramenters that will be passed along to elasticsearch request
174
183
  # @param options [String, Integer] :id The `_id` of the elasticsearch document
184
+ # @param options [String, nil] :suffix The index suffix. Defaults to the nil.
175
185
  # @raise [Elasticsearch::Transport::Transport::Errors::NotFound] when the doc does not exist
176
186
  # @return [Hash] The elasticsearch document.
177
187
  #
178
188
  # @see https://www.elastic.co/guide/en/elasticsearch/reference/7.5/docs-get.html
179
- def find!(id:, **options)
180
- client.get(options.merge(index: index_name, type: type_name, id: id))
189
+ def find!(id:, suffix: nil, **options)
190
+ client.get(options.merge(index: index_name(suffix: suffix), type: type_name, id: id))
181
191
  end
182
192
 
183
193
  # Retrieves the specified JSON document from an index.
@@ -187,11 +197,12 @@ module Esse
187
197
  #
188
198
  # @param options [Hash] Hash of paramenters that will be passed along to elasticsearch request
189
199
  # @param options [String, Integer] :id The `_id` of the elasticsearch document
200
+ # @param options [String, nil] :suffix The index suffix. Defaults to the nil.
190
201
  # @return [Hash, nil] The elasticsearch document
191
202
  #
192
203
  # @see https://www.elastic.co/guide/en/elasticsearch/reference/7.5/docs-get.html
193
- def find(id:, **options)
194
- find!(id: id, **options)
204
+ def find(id:, suffix: nil, **options)
205
+ find!(id: id, suffix: suffix, **options)
195
206
  rescue Elasticsearch::Transport::Transport::Errors::NotFound
196
207
  nil
197
208
  end
@@ -11,8 +11,6 @@ module Esse
11
11
 
12
12
  # Type delegators
13
13
  def_delegators :@index_type, :type_name, :each_serialized_batch, :serialize
14
- # Index delegators
15
- def_delegators :index_class, :index_name
16
14
 
17
15
  def initialize(type)
18
16
  @index_type = type
@@ -20,6 +18,13 @@ module Esse
20
18
 
21
19
  protected
22
20
 
21
+ def index_name(suffix: nil)
22
+ suffix = Hstring.new(suffix).underscore.presence
23
+ return index_class.index_name unless suffix
24
+
25
+ [index_class.index_name, suffix].join('_')
26
+ end
27
+
23
28
  def index_class
24
29
  @index_type.index
25
30
  end
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.0.3'
4
+ VERSION = '0.0.4'
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.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcos G. Zimmermann
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-05-10 00:00:00.000000000 Z
11
+ date: 2020-05-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: elasticsearch