esse 0.0.2 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. checksums.yaml +4 -4
  2. data/exec/esse +3 -1
  3. data/lib/esse/backend/index/aliases.rb +8 -4
  4. data/lib/esse/backend/index/close.rb +54 -0
  5. data/lib/esse/backend/index/create.rb +21 -10
  6. data/lib/esse/backend/index/delete.rb +15 -14
  7. data/lib/esse/backend/index/documents.rb +2 -2
  8. data/lib/esse/backend/index/existance.rb +2 -3
  9. data/lib/esse/backend/index/open.rb +54 -0
  10. data/lib/esse/backend/index/refresh.rb +43 -0
  11. data/lib/esse/backend/index/reset.rb +33 -0
  12. data/lib/esse/backend/index/update.rb +128 -4
  13. data/lib/esse/backend/index.rb +20 -4
  14. data/lib/esse/backend/index_type/documents.rb +53 -42
  15. data/lib/esse/backend/index_type.rb +7 -2
  16. data/lib/esse/cli/event_listener.rb +87 -0
  17. data/lib/esse/cli/generate.rb +9 -4
  18. data/lib/esse/cli/index/base_operation.rb +76 -0
  19. data/lib/esse/cli/index/close.rb +26 -0
  20. data/lib/esse/cli/index/create.rb +26 -0
  21. data/lib/esse/cli/index/delete.rb +26 -0
  22. data/lib/esse/cli/index/open.rb +26 -0
  23. data/lib/esse/cli/index/reset.rb +26 -0
  24. data/lib/esse/cli/index/update_aliases.rb +32 -0
  25. data/lib/esse/cli/index/update_mapping.rb +33 -0
  26. data/lib/esse/cli/index/update_settings.rb +26 -0
  27. data/lib/esse/cli/index.rb +70 -2
  28. data/lib/esse/cli/templates/config.rb.erb +20 -0
  29. data/lib/esse/cli/templates/index.rb.erb +76 -11
  30. data/lib/esse/cli/templates/type_collection.rb.erb +41 -0
  31. data/lib/esse/cli/templates/{mappings.json → type_mappings.json} +0 -0
  32. data/lib/esse/cli/templates/type_serializer.rb.erb +23 -0
  33. data/lib/esse/cli.rb +75 -3
  34. data/lib/esse/cluster.rb +22 -6
  35. data/lib/esse/config.rb +39 -5
  36. data/lib/esse/core.rb +18 -36
  37. data/lib/esse/errors.rb +47 -0
  38. data/lib/esse/events/bus.rb +103 -0
  39. data/lib/esse/events/event.rb +64 -0
  40. data/lib/esse/events/publisher.rb +119 -0
  41. data/lib/esse/events.rb +49 -0
  42. data/lib/esse/index/backend.rb +2 -1
  43. data/lib/esse/index/base.rb +4 -6
  44. data/lib/esse/index/mappings.rb +4 -6
  45. data/lib/esse/index/settings.rb +6 -8
  46. data/lib/esse/index.rb +2 -1
  47. data/lib/esse/index_mapping.rb +2 -2
  48. data/lib/esse/index_setting.rb +8 -4
  49. data/lib/esse/index_type/actions.rb +2 -1
  50. data/lib/esse/index_type/backend.rb +2 -1
  51. data/lib/esse/index_type/mappings.rb +3 -3
  52. data/lib/esse/index_type.rb +6 -1
  53. data/lib/esse/logging.rb +19 -0
  54. data/lib/esse/object_document_mapper.rb +96 -0
  55. data/lib/esse/primitives/hash_utils.rb +29 -0
  56. data/lib/esse/primitives/hstring.rb +4 -3
  57. data/lib/esse/primitives/output.rb +64 -0
  58. data/lib/esse/primitives.rb +1 -0
  59. data/lib/esse/template_loader.rb +1 -1
  60. data/lib/esse/version.rb +1 -1
  61. data/lib/esse.rb +14 -2
  62. metadata +127 -24
  63. data/.gitignore +0 -12
  64. data/.rubocop.yml +0 -128
  65. data/CHANGELOG.md +0 -0
  66. data/Gemfile +0 -7
  67. data/Gemfile.lock +0 -60
  68. data/LICENSE.txt +0 -21
  69. data/README.md +0 -50
  70. data/Rakefile +0 -4
  71. data/bin/console +0 -22
  72. data/bin/setup +0 -8
  73. data/esse.gemspec +0 -39
  74. data/lib/esse/cli/templates/serializer.rb.erb +0 -14
  75. data/lib/esse/index_type/serializer.rb +0 -87
  76. data/lib/esse/types/mapping.rb +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7f309243fb21b878b0d14b85c16a2d344dd595659d7972cadc7cd40c246ffd9a
4
- data.tar.gz: 670f66a022b7fe568fe9f995c0d8cd9832d65da5a9bfb21490a4a7e222540023
3
+ metadata.gz: 74a999f5083a3e258834ca27ac444c63e8f10e20940106f1415331d8a5e772b7
4
+ data.tar.gz: e7e2853b5fe1296795e6a5d306d12e2ac5c91e88cba671e2cd980ee1b1edfb2e
5
5
  SHA512:
6
- metadata.gz: 99b6fa0017361c185cdf71c7505f6a37dcfc28127f04504496dc1fbdddf8b7ca4a2c06e6c2e56dd30464c90f3b2012fc982bcdb3ed5159a1b331bfbe3c2e30d1
7
- data.tar.gz: db75895be7643f9bbedb9c81c071b4438e1f277637b2447dd7d8444c719820c41a97bdf69f0230e422a60412b2de93b674d2e459d0a324cb21bd49cbf2763f91
6
+ metadata.gz: 04a5efa492606d9385b07d8f076966b85bf825ffb49d37c9e35edd8fa5be5c4d283ac193097733f16583f920472aa83e2f1c05a00188649932c2c63ce6f14e53
7
+ data.tar.gz: 34b4426c3014edfcfe2c1320da019ba6b5c2566842baeb78e14e73df1df173f7a3a7298505b94128180a094aa953ee83dbf09a0e48791d65aa70f04f9e2c5fd9
data/exec/esse CHANGED
@@ -6,4 +6,6 @@ $LOAD_PATH.unshift File.expand_path('../lib', __dir__)
6
6
  require 'esse'
7
7
  require 'esse/cli'
8
8
 
9
- Esse::CLI.start(ARGV)
9
+ Esse::CLI.with_friendly_errors do
10
+ Esse::CLI.start(ARGV)
11
+ end
@@ -44,10 +44,14 @@ 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
- client.indices.update_aliases(options)
50
+
51
+ Esse::Events.instrument('elasticsearch.update_aliases') do |payload|
52
+ payload[:request] = options
53
+ payload[:response] = client.indices.update_aliases(options)
54
+ end
51
55
  end
52
56
 
53
57
  # Replaces all existing aliases by the respective suffixed index from argument.
@@ -55,11 +59,11 @@ module Esse
55
59
  # @param options [Hash] Hash of paramenters that will be passed along to elasticsearch request
56
60
  # @option [String] :suffix The suffix of the index used for versioning.
57
61
  # @raise [Elasticsearch::Transport::Transport::Errors::NotFound] in case of failure
58
- # @return [Hash, false] the elasticsearch response, or false in case of failure
62
+ # @return [Hash] the elasticsearch response, or an hash with 'errors' as true in case of failure
59
63
  def update_aliases(suffix:, **options)
60
64
  update_aliases!(suffix: suffix, **options)
61
65
  rescue Elasticsearch::Transport::Transport::Errors::NotFound
62
- false
66
+ { 'errors' => true }
63
67
  end
64
68
  end
65
69
 
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Esse
4
+ module Backend
5
+ class Index
6
+ module InstanceMethods
7
+ # Close an index (keep the data on disk, but deny operations with the 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 [Elasticsearch::Transport::Transport::Errors::BadRequest, Elasticsearch::Transport::Transport::Errors::NotFound]
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-close.html
23
+ def close!(suffix: index_version, **options)
24
+ Esse::Events.instrument('elasticsearch.close') do |payload|
25
+ payload[:request] = attributes = options.merge(index: index_name(suffix: suffix))
26
+ payload[:response] = client.indices.close(**attributes)
27
+ end
28
+ end
29
+
30
+ # Close an index (keep the data on disk, but deny operations with the 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-close.html
44
+ def close(suffix: index_version, **options)
45
+ close!(suffix: suffix, **options)
46
+ rescue Elasticsearch::Transport::Transport::ServerError
47
+ { 'errors' => true }
48
+ end
49
+ end
50
+
51
+ include InstanceMethods
52
+ end
53
+ end
54
+ end
@@ -10,43 +10,54 @@ module Esse
10
10
 
11
11
  # Creates index and applies mappings and settings.
12
12
  #
13
- # UsersIndex.backend.create # creates index named `<prefix_>users_<suffix|index_version|timestamp>`
13
+ # UsersIndex.elasticsearch.create_index # creates index named `<prefix_>users_<suffix|index_version|timestamp>`
14
14
  #
15
15
  # @param options [Hash] Options hash
16
16
  # @option options [Boolean] :alias Update `index_name` alias along with the new index
17
17
  # @option options [String] :suffix The index suffix. Defaults to the `IndexClass#index_version` or
18
18
  # `Esse.timestamp`. Suffixed index names might be used for zero-downtime mapping change.
19
- # @return [Hash, false] the elasticsearch response or false in case of unsuccessful creation.
19
+ # @return [Hash] the elasticsearch response, or an hash with 'errors' as true in case of failure
20
20
  #
21
21
  # @see http://www.elasticsearch.org/blog/changing-mapping-with-zero-downtime/
22
- def create(suffix: nil, **options)
23
- create!(suffix: suffix, **options)
22
+ def create_index(suffix: index_version, **options)
23
+ create_index!(suffix: suffix, **options)
24
24
  rescue Elasticsearch::Transport::Transport::Errors::BadRequest
25
- false
25
+ { 'errors' => true }
26
26
  end
27
27
 
28
28
  # Creates index and applies mappings and settings.
29
29
  #
30
- # UsersIndex.backend.create! # creates index named `<prefix_>users_<suffix|index_version|timestamp>`
30
+ # UsersIndex.elasticsearch.create_index! # creates index named `<prefix_>users_<suffix|index_version|timestamp>`
31
31
  #
32
32
  # @param options [Hash] Options hash
33
33
  # @option options [Boolean] :alias Update `index_name` alias along with the new index
34
34
  # @option options [String] :suffix The index suffix. Defaults to the `IndexClass#index_version` or
35
35
  # `Esse.timestamp`. Suffixed index names might be used for zero-downtime mapping change.
36
+ # @option arguments [String] :wait_for_active_shards Set the number of active shards
37
+ # to wait for before the operation returns.
38
+ # @option arguments [Time] :timeout Explicit operation timeout
39
+ # @option arguments [Time] :master_timeout Specify timeout for connection to master
40
+ # @option arguments [Hash] :headers Custom HTTP headers
41
+ # @option arguments [Hash] :body The configuration for the index (`settings` and `mappings`)
36
42
  # @raise [Elasticsearch::Transport::Transport::Errors::NotFound] when index already exists
37
43
  # @return [Hash] the elasticsearch response
38
44
  #
39
45
  # @see http://www.elasticsearch.org/blog/changing-mapping-with-zero-downtime/
40
- def create!(suffix: nil, **options)
46
+ def create_index!(suffix: index_version, **options)
41
47
  options = DEFAULT_OPTIONS.merge(options)
42
- name = real_index_name(suffix)
48
+ name = build_real_index_name(suffix)
43
49
  definition = [settings_hash, mappings_hash].reduce(&:merge)
44
50
 
45
- if options[:alias] && name != index_name
51
+ if options.delete(:alias) && name != index_name
46
52
  definition[:aliases] = { index_name => {} }
47
53
  end
48
54
 
49
- client.indices.create(index: name, body: definition)
55
+ Esse::Events.instrument('elasticsearch.create_index') do |payload|
56
+ payload[:request] = opts = options.merge(index: name, body: definition)
57
+ payload[:response] = response = client.indices.create(**opts)
58
+ cluster.wait_for_status! if response
59
+ response
60
+ end
50
61
  end
51
62
  end
52
63
 
@@ -6,29 +6,30 @@ module Esse
6
6
  module InstanceMethods
7
7
  # Deletes ES index
8
8
  #
9
- # UsersIndex.backend.delete! # deletes `<prefix_>users<_suffix|_index_version|_timestamp>` index
9
+ # UsersIndex.elasticsearch.delete_index! # deletes `<prefix_>users<_suffix|_index_version|_timestamp>` index
10
10
  #
11
- # @param options [Hash] Options hash
12
- # @option [String, nil] :suffix The index suffix Use nil if you want to delete the current index.
11
+ # @param suffix [String, nil] The index suffix Use nil if you want to delete the current index.
13
12
  # @raise [Elasticsearch::Transport::Transport::Errors::NotFound] when index does not exists
14
13
  # @return [Hash] elasticsearch response
15
- def delete!(suffix:)
16
- name = suffix ? real_index_name(suffix) : index_name
17
-
18
- client.indices.delete(index: name)
14
+ def delete_index!(suffix: index_version, **options)
15
+ Esse::Events.instrument('elasticsearch.delete_index') do |payload|
16
+ payload[:request] = opts = options.merge(index: index_name(suffix: suffix))
17
+ payload[:response] = response = client.indices.delete(**opts)
18
+ cluster.wait_for_status! if response
19
+ response
20
+ end
19
21
  end
20
22
 
21
23
  # Deletes ES index
22
24
  #
23
- # UsersIndex.backend.delete # deletes `<prefix_>users<_suffix|_index_version|_timestamp>` index
25
+ # UsersIndex.elasticsearch.delete_index # deletes `<prefix_>users<_suffix|_index_version|_timestamp>` index
24
26
  #
25
- # @param options [Hash] Options hash
26
- # @option [String] :suffix The index suffix. Use nil if you want to delete the current index.
27
- # @return [Hash, false] elasticsearch response, of false in case of error.
28
- def delete(suffix: index_version)
29
- delete!(suffix: suffix)
27
+ # @param suffix [String, nil] The index suffix Use nil if you want to delete the current index.
28
+ # @return [Hash] the elasticsearch response, or an hash with 'errors' as true in case of failure
29
+ def delete_index(suffix: index_version, **options)
30
+ delete_index!(suffix: suffix, **options)
30
31
  rescue Elasticsearch::Transport::Transport::Errors::NotFound
31
- false
32
+ { 'errors' => true }
32
33
  end
33
34
  end
34
35
 
@@ -6,13 +6,13 @@ module Esse
6
6
  module InstanceMethods
7
7
  def import!(**options)
8
8
  type_hash.each_value do |type|
9
- type.backend.import!(**options)
9
+ type.elasticsearch.import!(**options)
10
10
  end
11
11
  end
12
12
 
13
13
  def import(**options)
14
14
  type_hash.each_value do |type|
15
- type.backend.import(**options)
15
+ type.elasticsearch.import(**options)
16
16
  end
17
17
  end
18
18
  end
@@ -6,14 +6,13 @@ module Esse
6
6
  module InstanceMethods
7
7
  # Checks the index existance. Returns true or false
8
8
  #
9
- # UsersIndex.backend.exist? #=> true
9
+ # UsersIndex.elasticsearch.exist? #=> true
10
10
  #
11
11
  # @param options [Hash] Options hash
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
 
@@ -0,0 +1,54 @@
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 [Elasticsearch::Transport::Transport::Errors::BadRequest, Elasticsearch::Transport::Transport::Errors::NotFound]
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] = 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 Elasticsearch::Transport::Transport::ServerError
47
+ { 'errors' => true }
48
+ end
49
+ end
50
+
51
+ include InstanceMethods
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,43 @@
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 [Elasticsearch::Transport::Transport::Errors::BadRequest, Elasticsearch::Transport::Transport::Errors::NotFound]
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
+ client.indices.refresh(
20
+ options.merge(index: index_name(suffix: suffix)),
21
+ )
22
+ end
23
+
24
+ # Performs the refresh operation in one or more indices.
25
+ #
26
+ # @note The refresh operation can adversely affect indexing throughput when used too frequently.
27
+ # @param :suffix [String, nil] :suffix The index suffix. Defaults to the index_version.
28
+ # A uniq index name will be generated if one index already exist with the given alias.
29
+ # @param options [Hash] Options hash
30
+ # @return [Hash] the elasticsearch response, or an hash with 'errors' as true in case of failure
31
+ #
32
+ # @see https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-refresh.html
33
+ def refresh(suffix: index_version, **options)
34
+ refresh!(suffix: suffix, **options)
35
+ rescue Elasticsearch::Transport::Transport::ServerError
36
+ { 'errors' => true }
37
+ end
38
+ end
39
+
40
+ include InstanceMethods
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,33 @@
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 [Elasticsearch::Transport::Transport::Errors::BadRequest, Elasticsearch::Transport::Transport::Errors::NotFound]
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, **options)
18
+ existing = []
19
+ suffix ||= Esse.timestamp
20
+ suffix = Esse.timestamp while exist?(suffix: suffix).tap { |exist| existing << suffix if exist }
21
+
22
+ create_index!(suffix: suffix, **options)
23
+ import!(suffix: suffix, **options)
24
+ update_aliases!(suffix: suffix)
25
+ existing.each { |_s| delete_index!(suffix: suffix, **options) }
26
+ true
27
+ end
28
+ end
29
+
30
+ include InstanceMethods
31
+ end
32
+ end
33
+ end
@@ -4,12 +4,136 @@ module Esse
4
4
  module Backend
5
5
  class Index
6
6
  module InstanceMethods
7
- def update_mapping!(**options)
8
- # @TODO
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 [Elasticsearch::Transport::Transport::Errors::BadRequest, Elasticsearch::Transport::Transport::Errors::NotFound]
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] = client.indices.put_mapping(**opts)
37
+ end
9
38
  end
10
39
 
11
- def update_settings!(**options)
12
- # @TODO
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 Elasticsearch::Transport::Transport::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 [Elasticsearch::Transport::Transport::Errors::BadRequest, Elasticsearch::Transport::Transport::Errors::NotFound]
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 = 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 = 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 Elasticsearch::Transport::Transport::ServerError
136
+ { 'errors' => true }
13
137
  end
14
138
  end
15
139
 
@@ -10,11 +10,15 @@ module Esse
10
10
  require_relative 'index/delete'
11
11
  require_relative 'index/existance'
12
12
  require_relative 'index/update'
13
+ require_relative 'index/refresh'
14
+ require_relative 'index/reset'
13
15
  require_relative 'index/documents'
16
+ require_relative 'index/open'
17
+ require_relative 'index/close'
14
18
 
15
19
  extend Forwardable
16
20
 
17
- NAMING = %i[index_name index_version].freeze
21
+ NAMING = %i[index_version].freeze
18
22
  DEFINITION = %i[settings_hash mappings_hash].freeze
19
23
 
20
24
  def_delegators :@index, :type_hash, *(NAMING + DEFINITION)
@@ -25,13 +29,25 @@ module Esse
25
29
 
26
30
  protected
27
31
 
28
- def real_index_name(suffix = nil)
32
+ def index_name(suffix: nil)
33
+ suffix = Hstring.new(suffix).underscore.presence
34
+ return @index.index_name unless suffix
35
+
36
+ [@index.index_name, suffix].join('_')
37
+ end
38
+
39
+ def build_real_index_name(suffix = nil)
29
40
  suffix = Hstring.new(suffix).underscore.presence || index_version || Esse.timestamp
30
- [index_name, suffix].compact.join('_')
41
+
42
+ index_name(suffix: suffix)
31
43
  end
32
44
 
33
45
  def client
34
- @index.cluster.client
46
+ cluster.client
47
+ end
48
+
49
+ def cluster
50
+ @index.cluster
35
51
  end
36
52
  end
37
53
  end