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
@@ -9,63 +9,63 @@ module Esse
9
9
  # Convert ruby object to json. Arguments will be same of passed through the
10
10
  # collection. It's allowed a block or a class with the `to_h` instance method.
11
11
  # Example with block
12
- # serializer do |model, **context|
12
+ # document do |model, **context|
13
13
  # {
14
14
  # id: model.id,
15
15
  # admin: context[:is_admin],
16
16
  # }
17
17
  # end
18
- # Example with serializer class
19
- # serializer UserSerializer
20
- def serializer(klass = nil, &block)
21
- if @serializer_proc
22
- raise ArgumentError, format('Serializer for %p already defined', repo_name)
18
+ # Example with document class
19
+ # document UserDocument
20
+ def document(klass = nil, &block)
21
+ if @document_proc
22
+ raise ArgumentError, format('Document for %p already defined', repo_name)
23
23
  end
24
24
 
25
25
  if block
26
- @serializer_proc = ->(model, **kwargs) { coerce_to_document(block.call(model, **kwargs)) }
27
- elsif klass.is_a?(Class) && klass <= Esse::Serializer
28
- @serializer_proc = ->(model, **kwargs) { klass.new(model, **kwargs) }
26
+ @document_proc = ->(model, **kwargs) { coerce_to_document(block.call(model, **kwargs)) }
27
+ elsif klass.is_a?(Class) && klass <= Esse::Document
28
+ @document_proc = ->(model, **kwargs) { klass.new(model, **kwargs) }
29
29
  elsif klass.is_a?(Class) && klass.instance_methods.include?(:to_h)
30
- @serializer_proc = ->(model, **kwargs) { coerce_to_document(klass.new(model, **kwargs).to_h) }
30
+ @document_proc = ->(model, **kwargs) { coerce_to_document(klass.new(model, **kwargs).to_h) }
31
31
  elsif klass.is_a?(Class) && klass.instance_methods.include?(:as_json) # backward compatibility
32
- @serializer_proc = ->(model, **kwargs) { coerce_to_document(klass.new(model, **kwargs).as_json) }
32
+ @document_proc = ->(model, **kwargs) { coerce_to_document(klass.new(model, **kwargs).as_json) }
33
33
  elsif klass.is_a?(Class) && klass.instance_methods.include?(:call)
34
- @serializer_proc = ->(model, **kwargs) { coerce_to_document(klass.new(model, **kwargs).call) }
34
+ @document_proc = ->(model, **kwargs) { coerce_to_document(klass.new(model, **kwargs).call) }
35
35
  else
36
- msg = format("%<arg>p is not a valid serializer. The serializer should inherit from Esse::Serializer or respond to `to_h'", arg: klass)
36
+ msg = format("%<arg>p is not a valid document. The document should inherit from Esse::Document or respond to `to_h'", arg: klass)
37
37
  raise ArgumentError, msg
38
38
  end
39
39
  end
40
40
 
41
41
  def coerce_to_document(value)
42
42
  case value
43
- when Esse::Serializer
43
+ when Esse::Document
44
44
  value
45
45
  when Hash
46
46
  Esse::HashDocument.new(value)
47
47
  when NilClass, FalseClass
48
48
  Esse::NullDocument.new
49
49
  else
50
- raise ArgumentError, format('%<arg>p is not a valid document. The document should be a hash or an instance of Esse::Serializer', arg: value)
50
+ raise ArgumentError, format('%<arg>p is not a valid document. The document should be a hash or an instance of Esse::Document', arg: value)
51
51
  end
52
52
  end
53
53
 
54
- # Convert ruby object to json by using the serializer of the given document type.
54
+ # Convert ruby object to json by using the document of the given document type.
55
55
  # @param [Object] model The ruby object
56
56
  # @param [Hash] kwargs The context
57
57
  # @return [Esse::Document] The serialized document
58
58
  def serialize(model, **kwargs)
59
- if @serializer_proc.nil?
60
- raise NotImplementedError, format('there is no %<t>p serializer defined for the %<k>p index', t: repo_name, k: index.to_s)
59
+ if @document_proc.nil?
60
+ raise NotImplementedError, format('there is no %<t>p document defined for the %<k>p index', t: repo_name, k: index.to_s)
61
61
  end
62
62
 
63
- @serializer_proc.call(model, **kwargs)
63
+ @document_proc.call(model, **kwargs)
64
64
  end
65
65
 
66
66
  # Used to define the source of data. A block is required. And its
67
67
  # content should yield an array of each object that should be serialized.
68
- # The list of arguments will be passed throught the serializer method.
68
+ # The list of arguments will be passed throught the document method.
69
69
  #
70
70
  # Example:
71
71
  # collection AdminStore
@@ -9,10 +9,9 @@ module Esse
9
9
  # This methods will be defined using meta programming in the index respository definition
10
10
  # @see Esse::Index::Type.repository
11
11
  attr_reader :index
12
- attr_accessor :document_type
13
12
  end
14
13
  require_relative 'repository/actions'
15
- require_relative 'repository/backend'
14
+ require_relative 'repository/documents'
16
15
  require_relative 'repository/object_document_mapper'
17
16
  end
18
17
  end
@@ -3,13 +3,13 @@
3
3
  module Esse
4
4
  module Search
5
5
  class Query
6
- attr_reader :client_proxy, :definition
6
+ attr_reader :transport, :definition
7
7
 
8
- # @param client_proxy [Esse::ClientProxy] The client proxy to use for the query
8
+ # @param transport [Esse::Transport] The client proxy to use for the query
9
9
  # @param indices [<Array<Esse::Index, String>] The class of the index to search or the index name
10
10
  # @param definition [Hash] The options to pass to the search.
11
- def initialize(client_proxy, *indices, suffix: nil, **definition, &_block)
12
- @client_proxy = client_proxy
11
+ def initialize(transport, *indices, suffix: nil, **definition, &_block)
12
+ @transport = transport
13
13
  @definition = definition
14
14
  @definition[:index] = indices.map do |index|
15
15
  if index.is_a?(Class) && index < Esse::Index
@@ -48,8 +48,8 @@ module Esse
48
48
  end
49
49
  ensure
50
50
  begin
51
- client_proxy.clear_scroll(body: {scroll_id: scroll_id}) if scroll_id
52
- rescue Esse::Backend::NotFoundError
51
+ transport.clear_scroll(body: {scroll_id: scroll_id}) if scroll_id
52
+ rescue Esse::Transport::NotFoundError
53
53
  end
54
54
  end
55
55
 
@@ -60,7 +60,7 @@ module Esse
60
60
  Esse::Events.instrument('elasticsearch.execute_search_query') do |payload|
61
61
  payload[:query] = self
62
62
  begin
63
- resp = Response.new(self, client_proxy.search(**definition, **execution_options))
63
+ resp = Response.new(self, transport.search(**definition, **execution_options))
64
64
  rescue => e
65
65
  err = e
66
66
  end
@@ -77,7 +77,7 @@ module Esse
77
77
  Esse::Events.instrument('elasticsearch.execute_search_query') do |payload|
78
78
  payload[:query] = self
79
79
  begin
80
- resp = Response.new(self, client_proxy.scroll(scroll: scroll, body: { scroll_id: scroll_id }))
80
+ resp = Response.new(self, transport.scroll(scroll: scroll, body: { scroll_id: scroll_id }))
81
81
  rescue => e
82
82
  err = e
83
83
  end
@@ -24,7 +24,7 @@ module Esse
24
24
  path = nil
25
25
  @directories.each do |dir|
26
26
  patterns.find do |pattern|
27
- path = Dir[dir.join("#{pattern}.{#{@extensions.join(",")}}")].first
27
+ path = Dir[dir.join("#{pattern}.{#{@extensions.join(',')}}")].first
28
28
  break if path
29
29
  end
30
30
  break if path
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Esse
4
+ class Transport
5
+ module InstanceMethods
6
+ # Return a list of index aliases.
7
+ #
8
+ # @param options [Hash] Hash of paramenters that will be passed along to elasticsearch request
9
+ # @option [String] :index A comma-separated list of index names to filter aliases
10
+ # @option [String] :name A comma-separated list of alias names to return
11
+ # @raise [Esse::Transport::ServerError] in case of failure
12
+ #
13
+ # @see https://www.elastic.co/guide/en/elasticsearch/reference/7.5/indices-get-alias.html
14
+ def aliases(**options)
15
+ coerce_exception { client.indices.get_alias(**options) }
16
+ end
17
+
18
+ # Updates index aliases.
19
+ #
20
+ # @param options [Hash] Hash of paramenters that will be passed along to elasticsearch request
21
+ # @option [Hash] :body The definition of `actions` to perform
22
+ #
23
+ # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html
24
+ def update_aliases(body:, **options)
25
+ throw_error_when_readonly!
26
+
27
+ Esse::Events.instrument('elasticsearch.update_aliases') do |payload|
28
+ payload[:request] = options
29
+ payload[:response] = coerce_exception { client.indices.update_aliases(**options, body: body) }
30
+ end
31
+ end
32
+ end
33
+
34
+ include InstanceMethods
35
+ end
36
+ end
@@ -0,0 +1,199 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Esse
4
+ class Transport
5
+ module InstanceMethods
6
+ # Returns a document.
7
+ #
8
+ # @option [String] :id The document ID
9
+ # @option [String] :index The name of the index
10
+ # @option [Boolean] :force_synthetic_source Should this request force synthetic _source? Use this to test if the mapping supports synthetic _source and to get a sense of the worst case performance. Fetches with this enabled will be slower the enabling synthetic source natively in the index.
11
+ # @option [List] :stored_fields A comma-separated list of stored fields to return in the response
12
+ # @option [String] :preference Specify the node or shard the operation should be performed on (default: random)
13
+ # @option [Boolean] :realtime Specify whether to perform the operation in realtime or search mode
14
+ # @option [Boolean] :refresh Refresh the shard containing the document before performing the operation
15
+ # @option [String] :routing Specific routing value
16
+ # @option [List] :_source True or false to return the _source field or not, or a list of fields to return
17
+ # @option [List] :_source_excludes A list of fields to exclude from the returned _source field
18
+ # @option [List] :_source_includes A list of fields to extract and return from the _source field
19
+ # @option [Number] :version Explicit version number for concurrency control
20
+ # @option [String] :version_type Specific version type (options: internal, external, external_gte)
21
+ # @option [Hash] :headers Custom HTTP headers
22
+ #
23
+ # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-get.html
24
+ #
25
+ def get(id:, index:, **options)
26
+ Esse::Events.instrument('elasticsearch.get') do |payload|
27
+ payload[:request] = opts = options.merge(id: id, index: index)
28
+ payload[:response] = coerce_exception { client.get(**opts) }
29
+ end
30
+ end
31
+
32
+ # Returns information about whether a document exists in an index.
33
+ #
34
+ # @option [String] :id The document ID
35
+ # @option [String] :index The name of the index
36
+ # @option [List] :stored_fields A comma-separated list of stored fields to return in the response
37
+ # @option [String] :preference Specify the node or shard the operation should be performed on (default: random)
38
+ # @option [Boolean] :realtime Specify whether to perform the operation in realtime or search mode
39
+ # @option [Boolean] :refresh Refresh the shard containing the document before performing the operation
40
+ # @option [String] :routing Specific routing value
41
+ # @option [List] :_source True or false to return the _source field or not, or a list of fields to return
42
+ # @option [List] :_source_excludes A list of fields to exclude from the returned _source field
43
+ # @option [List] :_source_includes A list of fields to extract and return from the _source field
44
+ # @option [Number] :version Explicit version number for concurrency control
45
+ # @option [String] :version_type Specific version type (options: internal, external, external_gte)
46
+ # @option [Hash] :headers Custom HTTP headers
47
+ #
48
+ # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-get.html
49
+ #
50
+ def exist?(id:, index:, **options)
51
+ Esse::Events.instrument('elasticsearch.exist') do |payload|
52
+ payload[:request] = opts = options.merge(id: id, index: index)
53
+ payload[:response] = coerce_exception { client.exists(**opts) }
54
+ end
55
+ end
56
+
57
+ # Returns number of documents matching a query.
58
+ #
59
+ # @option [List] :index A comma-separated list of indices to restrict the results
60
+ # @option [Boolean] :ignore_unavailable Whether specified concrete indices should be ignored when unavailable (missing or closed)
61
+ # @option [Boolean] :ignore_throttled Whether specified concrete, expanded or aliased indices should be ignored when throttled
62
+ # @option [Boolean] :allow_no_indices Whether to ignore if a wildcard indices expression resolves into no concrete indices. (This includes `_all` string or when no indices have been specified)
63
+ # @option [String] :expand_wildcards Whether to expand wildcard expression to concrete indices that are open, closed or both. (options: open, closed, hidden, none, all)
64
+ # @option [Number] :min_score Include only documents with a specific `_score` value in the result
65
+ # @option [String] :preference Specify the node or shard the operation should be performed on (default: random)
66
+ # @option [List] :routing A comma-separated list of specific routing values
67
+ # @option [String] :q Query in the Lucene query string syntax
68
+ # @option [String] :analyzer The analyzer to use for the query string
69
+ # @option [Boolean] :analyze_wildcard Specify whether wildcard and prefix queries should be analyzed (default: false)
70
+ # @option [String] :default_operator The default operator for query string query (AND or OR) (options: AND, OR)
71
+ # @option [String] :df The field to use as default where no field prefix is given in the query string
72
+ # @option [Boolean] :lenient Specify whether format-based query failures (such as providing text to a numeric field) should be ignored
73
+ # @option [Number] :terminate_after The maximum count for each shard, upon reaching which the query execution will terminate early
74
+ # @option [Hash] :headers Custom HTTP headers
75
+ # @option [Hash] :body A query to restrict the results specified with the Query DSL (optional)
76
+ #
77
+ # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-count.html
78
+ def count(index:, **options)
79
+ Esse::Events.instrument('elasticsearch.count') do |payload|
80
+ payload[:request] = opts = options.merge(index: index)
81
+ payload[:response] = coerce_exception { client.count(**opts) }
82
+ end
83
+ end
84
+
85
+ # Removes a document from the index.
86
+ #
87
+ # @option arguments [String] :id The document ID
88
+ # @option arguments [String] :index The name of the index
89
+ # @option arguments [String] :wait_for_active_shards Sets the number of shard copies that must be active before proceeding with the delete operation. Defaults to 1, meaning the primary shard only. Set to `all` for all shard copies, otherwise set to any non-negative value less than or equal to the total number of copies for the shard (number of replicas + 1)
90
+ # @option arguments [String] :refresh If `true` then refresh the affected shards to make this operation visible to search, if `wait_for` then wait for a refresh to make this operation visible to search, if `false` (the default) then do nothing with refreshes. (options: true, false, wait_for)
91
+ # @option arguments [String] :routing Specific routing value
92
+ # @option arguments [Time] :timeout Explicit operation timeout
93
+ # @option arguments [Number] :if_seq_no only perform the delete operation if the last operation that has changed the document has the specified sequence number
94
+ # @option arguments [Number] :if_primary_term only perform the delete operation if the last operation that has changed the document has the specified primary term
95
+ # @option arguments [Number] :version Explicit version number for concurrency control
96
+ # @option arguments [String] :version_type Specific version type (options: internal, external, external_gte)
97
+ # @option arguments [Hash] :headers Custom HTTP headers
98
+ #
99
+ # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete.html
100
+ def delete(id:, index:, **options)
101
+ throw_error_when_readonly!
102
+
103
+ Esse::Events.instrument('elasticsearch.delete') do |payload|
104
+ payload[:request] = opts = options.merge(id: id, index: index)
105
+ payload[:response] = coerce_exception { client.delete(**opts) }
106
+ end
107
+ end
108
+
109
+ # Updates a document with a script or partial document.
110
+ #
111
+ # @option [String] :id Document ID
112
+ # @option [String] :index The name of the index
113
+ # @option [String] :wait_for_active_shards Sets the number of shard copies that must be active before proceeding with the update operation. Defaults to 1, meaning the primary shard only. Set to `all` for all shard copies, otherwise set to any non-negative value less than or equal to the total number of copies for the shard (number of replicas + 1)
114
+ # @option [List] :_source True or false to return the _source field or not, or a list of fields to return
115
+ # @option [List] :_source_excludes A list of fields to exclude from the returned _source field
116
+ # @option [List] :_source_includes A list of fields to extract and return from the _source field
117
+ # @option [String] :lang The script language (default: painless)
118
+ # @option [String] :refresh If `true` then refresh the affected shards to make this operation visible to search, if `wait_for` then wait for a refresh to make this operation visible to search, if `false` (the default) then do nothing with refreshes. (options: true, false, wait_for)
119
+ # @option [Number] :retry_on_conflict Specify how many times should the operation be retried when a conflict occurs (default: 0)
120
+ # @option [String] :routing Specific routing value
121
+ # @option [Time] :timeout Explicit operation timeout
122
+ # @option [Number] :if_seq_no only perform the update operation if the last operation that has changed the document has the specified sequence number
123
+ # @option [Number] :if_primary_term only perform the update operation if the last operation that has changed the document has the specified primary term
124
+ # @option [Boolean] :require_alias When true, requires destination is an alias. Default is false
125
+ # @option [Hash] :headers Custom HTTP headers
126
+ # @option [Hash] :body The request definition requires either `script` or partial `doc` (*Required*)
127
+ #
128
+ # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html
129
+ #
130
+ def update(id:, index:, body:, **options)
131
+ throw_error_when_readonly!
132
+
133
+ Esse::Events.instrument('elasticsearch.update') do |payload|
134
+ payload[:request] = opts = options.merge(id: id, index: index, body: body)
135
+ payload[:response] = coerce_exception { client.update(**opts) }
136
+ end
137
+ end
138
+
139
+ # Creates or updates a document in an index.
140
+ #
141
+ # @option [String] :id Document ID
142
+ # @option [String] :index The name of the index
143
+ # @option [String] :wait_for_active_shards Sets the number of shard copies that must be active before proceeding with the index operation. Defaults to 1, meaning the primary shard only. Set to `all` for all shard copies, otherwise set to any non-negative value less than or equal to the total number of copies for the shard (number of replicas + 1)
144
+ # @option [String] :op_type Explicit operation type. Defaults to `index` for requests with an explicit document ID, and to `create`for requests without an explicit document ID (options: index, create)
145
+ # @option [String] :refresh If `true` then refresh the affected shards to make this operation visible to search, if `wait_for` then wait for a refresh to make this operation visible to search, if `false` (the default) then do nothing with refreshes. (options: true, false, wait_for)
146
+ # @option [String] :routing Specific routing value
147
+ # @option [Time] :timeout Explicit operation timeout
148
+ # @option [Number] :version Explicit version number for concurrency control
149
+ # @option [String] :version_type Specific version type (options: internal, external, external_gte)
150
+ # @option [Number] :if_seq_no only perform the index operation if the last operation that has changed the document has the specified sequence number
151
+ # @option [Number] :if_primary_term only perform the index operation if the last operation that has changed the document has the specified primary term
152
+ # @option [String] :pipeline The pipeline id to preprocess incoming documents with
153
+ # @option [Boolean] :require_alias When true, requires destination to be an alias. Default is false
154
+ # @option [Hash] :headers Custom HTTP headers
155
+ # @option [Hash] :body The document (*Required*)
156
+ #
157
+ # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html
158
+ def index(id:, index:, body:, **options)
159
+ throw_error_when_readonly!
160
+
161
+ Esse::Events.instrument('elasticsearch.index') do |payload|
162
+ payload[:request] = opts = options.merge(id: id, index: index, body: body)
163
+ payload[:response] = coerce_exception { client.index(**opts) }
164
+ end
165
+ end
166
+
167
+ # Allows to perform multiple index/update/delete operations in a single request.
168
+ #
169
+ # @option arguments [String] :index Default index for items which don't provide one
170
+ # @option arguments [String] :wait_for_active_shards Sets the number of shard copies that must be active before proceeding with the bulk operation. Defaults to 1, meaning the primary shard only. Set to `all` for all shard copies, otherwise set to any non-negative value less than or equal to the total number of copies for the shard (number of replicas + 1)
171
+ # @option arguments [String] :refresh If `true` then refresh the affected shards to make this operation visible to search, if `wait_for` then wait for a refresh to make this operation visible to search, if `false` (the default) then do nothing with refreshes. (options: true, false, wait_for)
172
+ # @option arguments [String] :routing Specific routing value
173
+ # @option arguments [Time] :timeout Explicit operation timeout
174
+ # @option arguments [String] :type Default document type for items which don't provide one
175
+ # @option arguments [List] :_source True or false to return the _source field or not, or default list of fields to return, can be overridden on each sub-request
176
+ # @option arguments [List] :_source_excludes Default list of fields to exclude from the returned _source field, can be overridden on each sub-request
177
+ # @option arguments [List] :_source_includes Default list of fields to extract and return from the _source field, can be overridden on each sub-request
178
+ # @option arguments [String] :pipeline The pipeline id to preprocess incoming documents with
179
+ # @option arguments [Boolean] :require_alias Sets require_alias for all incoming documents. Defaults to unset (false)
180
+ # @option arguments [Hash] :headers Custom HTTP headers
181
+ # @option arguments [String|Array] :body The operation definition and data (action-data pairs), separated by newlines. Array of Strings, Header/Data pairs,
182
+ # or the conveniency "combined" format can be passed, refer to Elasticsearch::API::Utils.__bulkify documentation.
183
+ #
184
+ # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html
185
+ def bulk(body:, **options)
186
+ throw_error_when_readonly!
187
+
188
+ Esse::Events.instrument('elasticsearch.bulk') do |payload|
189
+ payload[:request] = opts = options.merge(body: body)
190
+ payload[:response] = response = coerce_exception { client.bulk(**opts) }
191
+ yield(payload) if block_given? # Allow caller to add data to the payload of event
192
+ response
193
+ end
194
+ end
195
+ end
196
+
197
+ include InstanceMethods
198
+ end
199
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Esse
4
+ class Transport
5
+ module InstanceMethods
6
+ # Returns basic information about the health of the cluster.
7
+ #
8
+ # @option [List] :index Limit the information returned to a specific index
9
+ # @option [String] :expand_wildcards Whether to expand wildcard expression to concrete indices that are open, closed or both. (options: open, closed, hidden, none, all)
10
+ # @option [String] :level Specify the level of detail for returned information (options: cluster, indices, shards)
11
+ # @option [Boolean] :local Return local information, do not retrieve the state from master node (default: false)
12
+ # @option [Time] :master_timeout Explicit operation timeout for connection to master node
13
+ # @option [Time] :timeout Explicit operation timeout
14
+ # @option [String] :wait_for_active_shards Wait until the specified number of shards is active
15
+ # @option [String] :wait_for_nodes Wait until the specified number of nodes is available
16
+ # @option [String] :wait_for_events Wait until all currently queued events with the given priority are processed (options: immediate, urgent, high, normal, low, languid)
17
+ # @option [Boolean] :wait_for_no_relocating_shards Whether to wait until there are no relocating shards in the cluster
18
+ # @option [Boolean] :wait_for_no_initializing_shards Whether to wait until there are no initializing shards in the cluster
19
+ # @option [String] :wait_for_status Wait until cluster is in a specific state (options: green, yellow, red)
20
+ # @option [Hash] :headers Custom HTTP headers
21
+ #
22
+ # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-health.html
23
+ def health(**options)
24
+ coerce_exception { client.cluster.health(**options) }
25
+ end
26
+ end
27
+
28
+ include InstanceMethods
29
+ end
30
+ end
@@ -0,0 +1,192 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Esse
4
+ class Transport
5
+ module InstanceMethods
6
+ # Creates an index with optional settings and mappings.
7
+ #
8
+ # @param options [Hash] Options hash
9
+ # @option [String] :index The name of the index
10
+ # @option [String] :wait_for_active_shards Set the number of active shards to wait for before the operation returns.
11
+ # @option [Time] :timeout Explicit operation timeout
12
+ # @option [Time] :master_timeout Specify timeout for connection to master
13
+ # @option [Hash] :headers Custom HTTP headers
14
+ # @option [Hash] :body The configuration for the index (`settings` and `mappings`)
15
+ # @option [String] :wait_for_status Wait until cluster is in a specific state (options: green, yellow, red)
16
+ #
17
+ # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html
18
+ def create_index(index:, wait_for_status: nil, **options)
19
+ throw_error_when_readonly!
20
+
21
+ Esse::Events.instrument('elasticsearch.create_index') do |payload|
22
+ payload[:request] = opts = options.merge(index: index)
23
+ payload[:response] = response = coerce_exception { client.indices.create(**opts) }
24
+ if response && response['acknowledged']
25
+ coerce_exception do
26
+ cluster.wait_for_status!(status: (wait_for_status || cluster.wait_for_status), index: index)
27
+ end
28
+ end
29
+ response
30
+ end
31
+ end
32
+
33
+ # Returns information about whether a particular index exists.
34
+ #
35
+ # @option [List] :index A comma-separated list of index names
36
+ # @option [Boolean] :local Return local information, do not retrieve the state from master node (default: false)
37
+ # @option [Boolean] :ignore_unavailable Ignore unavailable indexes (default: false)
38
+ # @option [Boolean] :allow_no_indices Ignore if a wildcard expression resolves to no concrete indices (default: false)
39
+ # @option [String] :expand_wildcards Whether wildcard expressions should get expanded to open or closed indices (default: open) (options: open, closed, hidden, none, all)
40
+ # @option [Boolean] :flat_settings Return settings in flat format (default: false)
41
+ # @option [Boolean] :include_defaults Whether to return all default setting for each of the indices.
42
+ # @option [Hash] :headers Custom HTTP headers
43
+ #
44
+ # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-exists.html
45
+ def index_exist?(index:, **options)
46
+ Esse::Events.instrument('elasticsearch.index_exist') do |payload|
47
+ payload[:request] = opts = options.merge(index: index)
48
+ payload[:response] = coerce_exception { client.indices.exists(**opts) }
49
+ end
50
+ end
51
+
52
+ # Deletes an index.
53
+ #
54
+ # @option [List] :index A comma-separated list of indices to delete; use `_all` or `*` string to delete all indices
55
+ # @option [Time] :timeout Explicit operation timeout
56
+ # @option [Time] :master_timeout Specify timeout for connection to master
57
+ # @option [Boolean] :ignore_unavailable Ignore unavailable indexes (default: false)
58
+ # @option [Boolean] :allow_no_indices Ignore if a wildcard expression resolves to no concrete indices (default: false)
59
+ # @option [String] :expand_wildcards Whether wildcard expressions should get expanded to open, closed, or hidden indices (options: open, closed, hidden, none, all)
60
+ # @option [Hash] :headers Custom HTTP headers
61
+ # @option [String] :wait_for_status Wait until cluster is in a specific state (options: green, yellow, red)
62
+ #
63
+ # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-delete-index.html
64
+ def delete_index(index:, wait_for_status: nil, **options)
65
+ throw_error_when_readonly!
66
+
67
+ Esse::Events.instrument('elasticsearch.delete_index') do |payload|
68
+ payload[:request] = opts = options.merge(index: index)
69
+ payload[:response] = response = coerce_exception { client.indices.delete(**opts) }
70
+ if response && response['acknowledged']
71
+ coerce_exception do
72
+ cluster.wait_for_status!(status: (wait_for_status || cluster.wait_for_status), index: index)
73
+ end
74
+ end
75
+ response
76
+ end
77
+ end
78
+
79
+ # Open a previously closed index
80
+ #
81
+ # @option options [List] :index A comma separated list of indices to open
82
+ # @option options [String] :expand_wildcards Whether to expand wildcard expression to concrete indices that
83
+ # are open, closed or both. (options: open, closed)
84
+ # @option options [String] :ignore_indices When performed on multiple indices, allows to ignore
85
+ # `missing` ones (options: none, missing) @until 1.0
86
+ # @option options [Boolean] :ignore_unavailable Whether specified concrete indices should be ignored when
87
+ # unavailable (missing, closed, etc)
88
+ # @option options [Time] :timeout Explicit operation timeout
89
+ # @raise [Esse::Transport::ServerError]
90
+ # in case of failure
91
+ # @return [Hash] the elasticsearch response
92
+ #
93
+ # @see https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-open-open.html
94
+ def open(index:, **options)
95
+ throw_error_when_readonly!
96
+
97
+ Esse::Events.instrument('elasticsearch.open') do |payload|
98
+ payload[:request] = attributes = options.merge(index: index)
99
+ payload[:response] = coerce_exception { client.indices.open(**attributes) }
100
+ end
101
+ end
102
+
103
+ # Close an index (keep the data on disk, but deny operations with the index).
104
+ #
105
+ # @option options [List] :index A comma separated list of indices to open
106
+ # @option options [String] :expand_wildcards Whether to expand wildcard expression to concrete indices that
107
+ # are open, closed or both. (options: open, closed)
108
+ # @option options [String] :ignore_indices When performed on multiple indices, allows to ignore
109
+ # `missing` ones (options: none, missing) @until 1.0
110
+ # @option options [Boolean] :ignore_unavailable Whether specified concrete indices should be ignored when
111
+ # unavailable (missing, closed, etc)
112
+ # @option options [Time] :timeout Explicit operation timeout
113
+ # @raise [Esse::Transport::ServerError] in case of failure
114
+ # @return [Hash] the elasticsearch response
115
+ #
116
+ # @see https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-open-close.html
117
+ def close(index:, **options)
118
+ throw_error_when_readonly!
119
+
120
+ Esse::Events.instrument('elasticsearch.close') do |payload|
121
+ payload[:request] = attributes = options.merge(index: index)
122
+ payload[:response] = coerce_exception { client.indices.close(**attributes) }
123
+ end
124
+ end
125
+
126
+ # Performs the refresh operation in one or more indices.
127
+ #
128
+ # @option options [List] :index A comma-separated list of index names; use `_all` or empty string to perform the operation on all indices
129
+ # @option options [Boolean] :ignore_unavailable Whether specified concrete indices should be ignored when unavailable (missing or closed)
130
+ # @option options [Boolean] :allow_no_indices Whether to ignore if a wildcard indices expression resolves into no concrete indices. (This includes `_all` string or when no indices have been specified)
131
+ # @option options [String] :expand_wildcards Whether to expand wildcard expression to concrete indices that are open, closed or both. (options: open, closed, hidden, none, all)
132
+ # @option options [Hash] :headers Custom HTTP headers
133
+ #
134
+ # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-refresh.html
135
+ def refresh(index:, **options)
136
+ throw_error_when_readonly!
137
+
138
+ Esse::Events.instrument('elasticsearch.refresh') do |payload|
139
+ payload[:request] = attributes = options.merge(index: index)
140
+ payload[:response] = coerce_exception { client.indices.refresh(**attributes) }
141
+ end
142
+ end
143
+
144
+ # Updates the index mappings.
145
+ #
146
+ # @option options [List] :index A comma-separated list of index names the mapping should be added to (supports wildcards); use `_all` or omit to add the mapping on all indices.
147
+ # @option options [Time] :timeout Explicit operation timeout
148
+ # @option options [Time] :master_timeout Specify timeout for connection to master
149
+ # @option options [Boolean] :ignore_unavailable Whether specified concrete indices should be ignored when unavailable (missing or closed)
150
+ # @option options [Boolean] :allow_no_indices Whether to ignore if a wildcard indices expression resolves into no concrete indices. (This includes `_all` string or when no indices have been specified)
151
+ # @option options [String] :expand_wildcards Whether to expand wildcard expression to concrete indices that are open, closed or both. (options: open, closed, hidden, none, all)
152
+ # @option options [Boolean] :write_index_only When true, applies mappings only to the write index of an alias or data stream
153
+ # @option options [Hash] :headers Custom HTTP headers
154
+ # @option options [Hash] :body The mapping definition (*Required*)
155
+ #
156
+ # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html
157
+ def update_mapping(index:, body:, **options)
158
+ throw_error_when_readonly!
159
+
160
+ Esse::Events.instrument('elasticsearch.update_mapping') do |payload|
161
+ payload[:request] = opts = options.merge(index: index, body: body)
162
+ payload[:response] = coerce_exception { client.indices.put_mapping(**opts) }
163
+ end
164
+ end
165
+
166
+ # Updates the index settings.
167
+ #
168
+ # @option options [List] :index A comma-separated list of index names; use `_all` or empty string to perform the operation on all indices
169
+ # @option options [Time] :master_timeout Specify timeout for connection to master
170
+ # @option options [Time] :timeout Explicit operation timeout
171
+ # @option options [Boolean] :preserve_existing Whether to update existing settings. If set to `true` existing settings on an index remain unchanged, the default is `false`
172
+ # @option options [Boolean] :ignore_unavailable Whether specified concrete indices should be ignored when unavailable (missing or closed)
173
+ # @option options [Boolean] :allow_no_indices Whether to ignore if a wildcard indices expression resolves into no concrete indices. (This includes `_all` string or when no indices have been specified)
174
+ # @option options [String] :expand_wildcards Whether to expand wildcard expression to concrete indices that are open, closed or both. (options: open, closed, hidden, none, all)
175
+ # @option options [Boolean] :flat_settings Return settings in flat format (default: false)
176
+ # @option options [Hash] :headers Custom HTTP headers
177
+ # @option options [Hash] :body The index settings to be updated (*Required*)
178
+ #
179
+ # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-update-settings.html
180
+ def update_settings(index:, body:, **options)
181
+ throw_error_when_readonly!
182
+
183
+ Esse::Events.instrument('elasticsearch.update_settings') do |payload|
184
+ payload[:request] = opts = options.merge(index: index, body: body)
185
+ payload[:response] = coerce_exception { client.indices.put_settings(**opts) }
186
+ end
187
+ end
188
+ end
189
+
190
+ include InstanceMethods
191
+ end
192
+ end