esse 0.1.1 → 0.2.0

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: 74a999f5083a3e258834ca27ac444c63e8f10e20940106f1415331d8a5e772b7
4
- data.tar.gz: e7e2853b5fe1296795e6a5d306d12e2ac5c91e88cba671e2cd980ee1b1edfb2e
3
+ metadata.gz: a31d2a1c181dccd9c2acd7256d958010e7c636016b1d5a267597f7aec9ed3334
4
+ data.tar.gz: cdb390a10b0492833a9360d4fd39df0c18df114a44b37554cba1dd39d229f75b
5
5
  SHA512:
6
- metadata.gz: 04a5efa492606d9385b07d8f076966b85bf825ffb49d37c9e35edd8fa5be5c4d283ac193097733f16583f920472aa83e2f1c05a00188649932c2c63ce6f14e53
7
- data.tar.gz: 34b4426c3014edfcfe2c1320da019ba6b5c2566842baeb78e14e73df1df173f7a3a7298505b94128180a094aa953ee83dbf09a0e48791d65aa70f04f9e2c5fd9
6
+ metadata.gz: 59ae5dcafd8998c69e5705910b119f5f9d1333899a4320b8f7afa4c5782aa435d820dde222726de111cee00c6d3b4bbed9d6fdd18dac6a6f740cf64138b70d53
7
+ data.tar.gz: 84b876eaec5b090718e2d265dd5d9456c5dfaeedafdae6e9361ac790de91eab0ba46b9ceabee3c5e8a891c5027ff3ac8443217554219f8dce5f52c853912989c
@@ -19,10 +19,10 @@ module Esse
19
19
  suffix ||= Esse.timestamp
20
20
  suffix = Esse.timestamp while exist?(suffix: suffix).tap { |exist| existing << suffix if exist }
21
21
 
22
- create_index!(suffix: suffix, **options)
23
- import!(suffix: suffix, **options)
22
+ create_index!(**options, suffix: suffix, alias: false)
23
+ import!(**options, suffix: suffix)
24
24
  update_aliases!(suffix: suffix)
25
- existing.each { |_s| delete_index!(suffix: suffix, **options) }
25
+ existing.each { |_s| delete_index!(**options, suffix: suffix) }
26
26
  true
27
27
  end
28
28
  end
@@ -11,7 +11,7 @@ module Esse
11
11
  # @option [Hash] :context The collection context. This value will be passed as argument to the collection
12
12
  # May be SQL condition or any other filter you have defined on the collection.
13
13
  def import(context: {}, suffix: nil, **options)
14
- each_serialized_batch(context || {}) do |batch|
14
+ each_serialized_batch(**(context || {})) do |batch|
15
15
  bulk(index: batch, suffix: suffix, **options)
16
16
  end
17
17
  end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base_operation'
4
+
5
+ module Esse
6
+ module CLI
7
+ class Index::Import < Index::BaseOperation
8
+ def run
9
+ validate_options!
10
+ indices.each do |index|
11
+ index.elasticsearch.import!(**options)
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ def options
18
+ @options.slice(*@options.keys - CLI_IGNORE_OPTS)
19
+ end
20
+
21
+ def validate_options!
22
+ validate_indices_option!
23
+ end
24
+ end
25
+ end
26
+ end
@@ -77,6 +77,14 @@ module Esse
77
77
  require_relative 'index/open'
78
78
  Open.new(indices: index_classes, **options.to_h.transform_keys(&:to_sym)).run
79
79
  end
80
+
81
+ desc 'import *INDEX_CLASSES', 'Import documents from the given classes'
82
+ option :suffix, type: :string, default: nil, aliases: '-s', desc: 'Suffix to append to index name'
83
+ option :context, type: :hash, default: {}, required: true, desc: 'List of options to pass to the index class'
84
+ def import(*index_classes)
85
+ require_relative 'index/import'
86
+ Import.new(indices: index_classes, **HashUtils.deep_transform_keys(options.to_h, &:to_sym)).run
87
+ end
80
88
  end
81
89
  end
82
90
  end
@@ -22,8 +22,8 @@ class <%= @index_name %> < <%= @base_class %>
22
22
  # collection Collections::DocumentCollection
23
23
  #
24
24
  # but you can also use block definition style:
25
- # collection do |_context, &block|
26
- # block.call [{ title: 'foo' }, { title: 'bar' }], context
25
+ # collection do |**context, &block|
26
+ # block.call [{ title: 'foo' }, { title: 'bar' }], extra: 'info'
27
27
  # end
28
28
  #
29
29
  # Serializer block or class yielder should be called with an array of objects.
@@ -78,7 +78,7 @@ class <%= @index_name %> < <%= @base_class %>
78
78
  # Useful for eager loading data from database or any other repository. Below is an example of a rails like
79
79
  # application could load using activerecord.
80
80
  #
81
- # collection do |context, &block|
81
+ # collection do |**context, &block|
82
82
  # <%= type.camelize %>.where(context[:conditions]).find_in_batches(batch_size: 5000) do |batch|
83
83
  # block.call batch, context
84
84
  # end
@@ -26,15 +26,15 @@ class <%= @index_name %> < <%= @base_class %>
26
26
 
27
27
  yield(rows, **params)
28
28
  end
29
+ end
29
30
 
30
- protected
31
+ protected
31
32
 
32
- attr_reader :params
33
+ attr_reader :params
33
34
 
34
- # @param offset [Number] Offset to start from
35
- def find_all(offset)
36
- # @TODO load data from persistent store
37
- end
35
+ # @param offset [Number] Offset to start from
36
+ def find_all(offset)
37
+ # @TODO load data from persistent store
38
38
  end
39
39
  end
40
40
  end
@@ -86,10 +86,24 @@ module Esse
86
86
  # @param args [*Object] Any argument is allowed here. The collection will be called with same arguments.
87
87
  # And the serializer will be initialized with those arguments too.
88
88
  # @yield [Array, *Object] serialized collection and method arguments
89
- def each_serialized_batch(*args, **kwargs, &block)
90
- each_batch(*args, **kwargs) do |batch|
91
- entries = batch.map { |entry| serialize(entry, *args, **kwargs) }.compact
92
- block.call(entries, *args, **kwargs)
89
+ def each_serialized_batch(**kwargs, &block)
90
+ each_batch(**kwargs) do |*batch, **collection_kwargs|
91
+ entries = batch.flatten.map { |entry| serialize(entry, **collection_kwargs) }.compact
92
+ block.call(entries, **kwargs)
93
+ end
94
+ end
95
+
96
+ # Wrap collection data into serialized documents
97
+ #
98
+ # Example:
99
+ # GeosIndex.documents(id: 1).first
100
+ #
101
+ # @return [Enumerator] All serialized entries
102
+ def documents(**kwargs)
103
+ Enumerator.new do |yielder|
104
+ each_serialized_batch(**kwargs) do |documents, **_collection_kargs|
105
+ documents.each { |document| yielder.yield(document) }
106
+ end
93
107
  end
94
108
  end
95
109
  end
@@ -6,6 +6,17 @@ module Esse
6
6
  module HashUtils
7
7
  module_function
8
8
 
9
+ def deep_transform_keys(hash, &block)
10
+ hash.each_with_object({}) do |(key, value), result|
11
+ result[yield(key)] = \
12
+ if value.is_a?(Hash)
13
+ deep_transform_keys(value, &block)
14
+ else
15
+ value
16
+ end
17
+ end
18
+ end
19
+
9
20
  def deep_merge(target, source)
10
21
  target.merge(source) do |key, oldval, newval|
11
22
  if oldval.is_a?(Hash) && newval.is_a?(Hash)
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.1.1'
4
+ VERSION = '0.2.0'
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.1.1
4
+ version: 0.2.0
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: 2022-01-27 00:00:00.000000000 Z
11
+ date: 2022-03-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: elasticsearch
@@ -246,6 +246,7 @@ files:
246
246
  - lib/esse/cli/index/close.rb
247
247
  - lib/esse/cli/index/create.rb
248
248
  - lib/esse/cli/index/delete.rb
249
+ - lib/esse/cli/index/import.rb
249
250
  - lib/esse/cli/index/open.rb
250
251
  - lib/esse/cli/index/reset.rb
251
252
  - lib/esse/cli/index/update_aliases.rb