Module: Esse::Transport::InstanceMethods

Included in:
Esse::Transport
Defined in:
lib/esse/transport/health.rb,
lib/esse/transport/search.rb,
lib/esse/transport/aliases.rb,
lib/esse/transport/indices.rb,
lib/esse/transport/documents.rb

Instance Method Summary collapse

Instance Method Details

#aliases(**options) ⇒ Object

Return a list of index aliases.

Parameters:

  • options (Hash)

    Hash of paramenters that will be passed along to elasticsearch request

  • [String] (Hash)

    a customizable set of options

Raises:

See Also:



14
15
16
# File 'lib/esse/transport/aliases.rb', line 14

def aliases(**options)
  coerce_exception { client.indices.get_alias(**options) }
end

#bulk(body:, **options) ⇒ Object

Allows to perform multiple index/update/delete operations in a single request.

or the conveniency “combined” format can be passed, refer to Elasticsearch::API::Utils.__bulkify documentation.

Parameters:

  • arguments (Hash)

    a customizable set of options

See Also:



185
186
187
188
189
190
191
192
193
194
# File 'lib/esse/transport/documents.rb', line 185

def bulk(body:, **options)
  throw_error_when_readonly!

  Esse::Events.instrument('elasticsearch.bulk') do |payload|
    payload[:request] = opts = options.merge(body: body)
    payload[:response] = response = coerce_exception { client.bulk(**opts) }
    yield(payload) if block_given? # Allow caller to add data to the payload of event
    response
  end
end

#clear_scroll(body:, **options) ⇒ Object

Explicitly clears the search context for a scroll.

Parameters:

  • options (Hash)
  • [Hash] (Hash)

    a customizable set of options



41
42
43
# File 'lib/esse/transport/search.rb', line 41

def clear_scroll(body:, **options)
  coerce_exception { client.clear_scroll(body: body, **options) }
end

#close(index:, **options) ⇒ Hash

Close an index (keep the data on disk, but deny operations with the index).

Parameters:

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • :index (List)

    A comma separated list of indices to open

  • :expand_wildcards (String)

    Whether to expand wildcard expression to concrete indices that are open, closed or both. (options: open, closed)

  • :ignore_indices (String)

    When performed on multiple indices, allows to ignore ‘missing` ones (options: none, missing) @until 1.0

  • :ignore_unavailable (Boolean)

    Whether specified concrete indices should be ignored when unavailable (missing, closed, etc)

  • :timeout (Time)

    Explicit operation timeout

Returns:

  • (Hash)

    the elasticsearch response

Raises:

See Also:



117
118
119
120
121
122
123
124
# File 'lib/esse/transport/indices.rb', line 117

def close(index:, **options)
  throw_error_when_readonly!

  Esse::Events.instrument('elasticsearch.close') do |payload|
    payload[:request] = attributes = options.merge(index: index)
    payload[:response] = coerce_exception { client.indices.close(**attributes) }
  end
end

#count(index:, **options) ⇒ Object

Returns number of documents matching a query.

Parameters:

  • [List] (Hash)

    a customizable set of options

  • [Boolean] (Hash)

    a customizable set of options

  • [String] (Hash)

    a customizable set of options

  • [Number] (Hash)

    a customizable set of options

  • [Hash] (Hash)

    a customizable set of options

See Also:



78
79
80
81
82
83
# File 'lib/esse/transport/documents.rb', line 78

def count(index:, **options)
  Esse::Events.instrument('elasticsearch.count') do |payload|
    payload[:request] = opts = options.merge(index: index)
    payload[:response] = coerce_exception { client.count(**opts) }
  end
end

#create_index(index:, wait_for_status: nil, **options) ⇒ Object

Creates an index with optional settings and mappings.

Parameters:

  • options (Hash)

    Options hash

  • [String] (Hash)

    a customizable set of options

  • [Time] (Hash)

    a customizable set of options

  • [Hash] (Hash)

    a customizable set of options

See Also:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/esse/transport/indices.rb', line 18

def create_index(index:, wait_for_status: nil, **options)
  throw_error_when_readonly!

  Esse::Events.instrument('elasticsearch.create_index') do |payload|
    payload[:request] = opts = options.merge(index: index)
    payload[:response] = response = coerce_exception { client.indices.create(**opts) }
    if response && response['acknowledged']
      coerce_exception do
        cluster.wait_for_status!(status: (wait_for_status || cluster.wait_for_status), index: index)
      end
    end
    response
  end
end

#delete(id:, index:, **options) ⇒ Object

Removes a document from the index.

Parameters:

  • arguments (Hash)

    a customizable set of options

See Also:



100
101
102
103
104
105
106
107
# File 'lib/esse/transport/documents.rb', line 100

def delete(id:, index:, **options)
  throw_error_when_readonly!

  Esse::Events.instrument('elasticsearch.delete') do |payload|
    payload[:request] = opts = options.merge(id: id, index: index)
    payload[:response] = coerce_exception { client.delete(**opts) }
  end
end

#delete_index(index:, wait_for_status: nil, **options) ⇒ Object

Deletes an index.

Parameters:

  • [List] (Hash)

    a customizable set of options

  • [Time] (Hash)

    a customizable set of options

  • [Boolean] (Hash)

    a customizable set of options

  • [String] (Hash)

    a customizable set of options

  • [Hash] (Hash)

    a customizable set of options

See Also:



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/esse/transport/indices.rb', line 64

def delete_index(index:, wait_for_status: nil, **options)
  throw_error_when_readonly!

  Esse::Events.instrument('elasticsearch.delete_index') do |payload|
    payload[:request] = opts = options.merge(index: index)
    payload[:response] = response = coerce_exception { client.indices.delete(**opts) }
    if response && response['acknowledged']
      coerce_exception do
        cluster.wait_for_status!(status: (wait_for_status || cluster.wait_for_status), index: index)
      end
    end
    response
  end
end

#exist?(id:, index:, **options) ⇒ Boolean

Returns information about whether a document exists in an index.

Parameters:

  • [String] (Hash)

    a customizable set of options

  • [List] (Hash)

    a customizable set of options

  • [Boolean] (Hash)

    a customizable set of options

  • [Number] (Hash)

    a customizable set of options

  • [Hash] (Hash)

    a customizable set of options

Returns:

  • (Boolean)

See Also:



50
51
52
53
54
55
# File 'lib/esse/transport/documents.rb', line 50

def exist?(id:, index:, **options)
  Esse::Events.instrument('elasticsearch.exist') do |payload|
    payload[:request] = opts = options.merge(id: id, index: index)
    payload[:response] = coerce_exception { client.exists(**opts) }
  end
end

#get(id:, index:, **options) ⇒ Object

Returns a document.

Parameters:

  • [String] (Hash)

    a customizable set of options

  • [Boolean] (Hash)

    a customizable set of options

  • [List] (Hash)

    a customizable set of options

  • [Number] (Hash)

    a customizable set of options

  • [Hash] (Hash)

    a customizable set of options

See Also:



25
26
27
28
29
30
# File 'lib/esse/transport/documents.rb', line 25

def get(id:, index:, **options)
  Esse::Events.instrument('elasticsearch.get') do |payload|
    payload[:request] = opts = options.merge(id: id, index: index)
    payload[:response] = coerce_exception { client.get(**opts) }
  end
end

#health(**options) ⇒ Object

Returns basic information about the health of the cluster.

Parameters:

  • [List] (Hash)

    a customizable set of options

  • [String] (Hash)

    a customizable set of options

  • [Boolean] (Hash)

    a customizable set of options

  • [Time] (Hash)

    a customizable set of options

  • [Hash] (Hash)

    a customizable set of options

See Also:



23
24
25
# File 'lib/esse/transport/health.rb', line 23

def health(**options)
  coerce_exception { client.cluster.health(**options) }
end

#index(id:, index:, body:, **options) ⇒ Object

Creates or updates a document in an index.

Parameters:

  • [String] (Hash)

    a customizable set of options

  • [Time] (Hash)

    a customizable set of options

  • [Number] (Hash)

    a customizable set of options

  • [Boolean] (Hash)

    a customizable set of options

  • [Hash] (Hash)

    a customizable set of options

See Also:



158
159
160
161
162
163
164
165
# File 'lib/esse/transport/documents.rb', line 158

def index(id:, index:, body:, **options)
  throw_error_when_readonly!

  Esse::Events.instrument('elasticsearch.index') do |payload|
    payload[:request] = opts = options.merge(id: id, index: index, body: body)
    payload[:response] = coerce_exception { client.index(**opts) }
  end
end

#index_exist?(index:, **options) ⇒ Boolean

Returns information about whether a particular index exists.

Parameters:

  • [List] (Hash)

    a customizable set of options

  • [Boolean] (Hash)

    a customizable set of options

  • [String] (Hash)

    a customizable set of options

  • [Hash] (Hash)

    a customizable set of options

Returns:

  • (Boolean)

See Also:



45
46
47
48
49
50
# File 'lib/esse/transport/indices.rb', line 45

def index_exist?(index:, **options)
  Esse::Events.instrument('elasticsearch.index_exist') do |payload|
    payload[:request] = opts = options.merge(index: index)
    payload[:response] = coerce_exception { client.indices.exists(**opts) }
  end
end

#open(index:, **options) ⇒ Hash

Open a previously closed index

Parameters:

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • :index (List)

    A comma separated list of indices to open

  • :expand_wildcards (String)

    Whether to expand wildcard expression to concrete indices that are open, closed or both. (options: open, closed)

  • :ignore_indices (String)

    When performed on multiple indices, allows to ignore ‘missing` ones (options: none, missing) @until 1.0

  • :ignore_unavailable (Boolean)

    Whether specified concrete indices should be ignored when unavailable (missing, closed, etc)

  • :timeout (Time)

    Explicit operation timeout

Returns:

  • (Hash)

    the elasticsearch response

Raises:

See Also:



94
95
96
97
98
99
100
101
# File 'lib/esse/transport/indices.rb', line 94

def open(index:, **options)
  throw_error_when_readonly!

  Esse::Events.instrument('elasticsearch.open') do |payload|
    payload[:request] = attributes = options.merge(index: index)
    payload[:response] = coerce_exception { client.indices.open(**attributes) }
  end
end

#refresh(index:, **options) ⇒ Object

Performs the refresh operation in one or more indices.

Parameters:

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • :index (List)

    A comma-separated list of index names; use ‘_all` or empty string to perform the operation on all indices

  • :ignore_unavailable (Boolean)

    Whether specified concrete indices should be ignored when unavailable (missing or closed)

  • :allow_no_indices (Boolean)

    Whether to ignore if a wildcard indices expression resolves into no concrete indices. (This includes ‘_all` string or when no indices have been specified)

  • :expand_wildcards (String)

    Whether to expand wildcard expression to concrete indices that are open, closed or both. (options: open, closed, hidden, none, all)

  • :headers (Hash)

    Custom HTTP headers

See Also:



135
136
137
138
139
140
141
142
# File 'lib/esse/transport/indices.rb', line 135

def refresh(index:, **options)
  throw_error_when_readonly!

  Esse::Events.instrument('elasticsearch.refresh') do |payload|
    payload[:request] = attributes = options.merge(index: index)
    payload[:response] = coerce_exception { client.indices.refresh(**attributes) }
  end
end

#reindex(body:, **options) ⇒ Object

Allows to copy documents from one index to another, optionally filtering the source documents by a query, changing the destination index settings, or fetching the documents from a remote cluster.

Parameters:

  • arguments (Hash)

    a customizable set of options

See Also:



205
206
207
208
209
210
211
212
# File 'lib/esse/transport/indices.rb', line 205

def reindex(body:, **options)
  throw_error_when_readonly!

  Esse::Events.instrument('elasticsearch.reindex') do |payload|
    payload[:request] = opts = options.merge(body: body)
    payload[:response] = coerce_exception { client.reindex(**opts) }
  end
end

#scroll(scroll:, **definition) ⇒ Object

Allows to retrieve a large numbers of results from a single search request.

Parameters:

  • options (Hash)
  • [Time] (Hash)

    a customizable set of options

  • [Boolean] (Hash)

    a customizable set of options

  • [Hash] (Hash)

    a customizable set of options



26
27
28
29
30
31
32
33
34
# File 'lib/esse/transport/search.rb', line 26

def scroll(scroll:, **definition)
  unless definition[:body]
    raise ArgumentError, 'scroll search must have a :body with the :scroll_id'
  end
  Esse::Events.instrument('elasticsearch.search') do |payload|
    payload[:request] = definition
    payload[:response] = coerce_exception { client.scroll(scroll: scroll, **definition) }
  end
end

#search(index:, **options) ⇒ Object

Returns results matching a query.

Parameters:

  • options (Hash)
  • [String] (Hash)

    a customizable set of options



9
10
11
12
13
14
15
16
17
18
# File 'lib/esse/transport/search.rb', line 9

def search(index:, **options)
  definition = options.merge(
    index: index,
  )

  Esse::Events.instrument('elasticsearch.search') do |payload|
    payload[:request] = definition
    payload[:response] = coerce_exception { client.search(definition) }
  end
end

#update(id:, index:, body:, **options) ⇒ Object

Updates a document with a script or partial document.

Parameters:

  • [String] (Hash)

    a customizable set of options

  • [List] (Hash)

    a customizable set of options

  • [Number] (Hash)

    a customizable set of options

  • [Time] (Hash)

    a customizable set of options

  • [Boolean] (Hash)

    a customizable set of options

  • [Hash] (Hash)

    a customizable set of options

See Also:



130
131
132
133
134
135
136
137
# File 'lib/esse/transport/documents.rb', line 130

def update(id:, index:, body:, **options)
  throw_error_when_readonly!

  Esse::Events.instrument('elasticsearch.update') do |payload|
    payload[:request] = opts = options.merge(id: id, index: index, body: body)
    payload[:response] = coerce_exception { client.update(**opts) }
  end
end

#update_aliases(body:, **options) ⇒ Object

Updates index aliases.

Parameters:

  • options (Hash)

    Hash of paramenters that will be passed along to elasticsearch request

  • [Hash] (Hash)

    a customizable set of options

See Also:



24
25
26
27
28
29
30
31
# File 'lib/esse/transport/aliases.rb', line 24

def update_aliases(body:, **options)
  throw_error_when_readonly!

  Esse::Events.instrument('elasticsearch.update_aliases') do |payload|
    payload[:request] = options.merge(body: body)
    payload[:response] = coerce_exception { client.indices.update_aliases(**options, body: body) }
  end
end

#update_by_query(index:, **options) ⇒ Object

Performs an update on every document in the index without changing the source, for example to pick up a mapping change.

Parameters:

  • arguments (Hash)

    a customizable set of options

See Also:



257
258
259
260
261
262
263
264
# File 'lib/esse/transport/indices.rb', line 257

def update_by_query(index:, **options)
  throw_error_when_readonly!

  Esse::Events.instrument('elasticsearch.update_by_query') do |payload|
    payload[:request] = opts = options.merge(index: index)
    payload[:response] = coerce_exception { client.update_by_query(**opts) }
  end
end

#update_mapping(index:, body:, **options) ⇒ Object

Updates the index mappings.

Parameters:

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • :index (List)

    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.

  • :timeout (Time)

    Explicit operation timeout

  • :master_timeout (Time)

    Specify timeout for connection to master

  • :ignore_unavailable (Boolean)

    Whether specified concrete indices should be ignored when unavailable (missing or closed)

  • :allow_no_indices (Boolean)

    Whether to ignore if a wildcard indices expression resolves into no concrete indices. (This includes ‘_all` string or when no indices have been specified)

  • :expand_wildcards (String)

    Whether to expand wildcard expression to concrete indices that are open, closed or both. (options: open, closed, hidden, none, all)

  • :write_index_only (Boolean)

    When true, applies mappings only to the write index of an alias or data stream

  • :headers (Hash)

    Custom HTTP headers

  • :body (Hash)

    The mapping definition (Required)

See Also:



157
158
159
160
161
162
163
164
# File 'lib/esse/transport/indices.rb', line 157

def update_mapping(index:, body:, **options)
  throw_error_when_readonly!

  Esse::Events.instrument('elasticsearch.update_mapping') do |payload|
    payload[:request] = opts = options.merge(index: index, body: body)
    payload[:response] = coerce_exception { client.indices.put_mapping(**opts) }
  end
end

#update_settings(index:, body:, **options) ⇒ Object

Updates the index settings.

Parameters:

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • :index (List)

    A comma-separated list of index names; use ‘_all` or empty string to perform the operation on all indices

  • :master_timeout (Time)

    Specify timeout for connection to master

  • :timeout (Time)

    Explicit operation timeout

  • :preserve_existing (Boolean)

    Whether to update existing settings. If set to ‘true` existing settings on an index remain unchanged, the default is `false`

  • :ignore_unavailable (Boolean)

    Whether specified concrete indices should be ignored when unavailable (missing or closed)

  • :allow_no_indices (Boolean)

    Whether to ignore if a wildcard indices expression resolves into no concrete indices. (This includes ‘_all` string or when no indices have been specified)

  • :expand_wildcards (String)

    Whether to expand wildcard expression to concrete indices that are open, closed or both. (options: open, closed, hidden, none, all)

  • :flat_settings (Boolean)

    Return settings in flat format (default: false)

  • :headers (Hash)

    Custom HTTP headers

  • :body (Hash)

    The index settings to be updated (Required)

See Also:



180
181
182
183
184
185
186
187
# File 'lib/esse/transport/indices.rb', line 180

def update_settings(index:, body:, **options)
  throw_error_when_readonly!

  Esse::Events.instrument('elasticsearch.update_settings') do |payload|
    payload[:request] = opts = options.merge(index: index, body: body)
    payload[:response] = coerce_exception { client.indices.put_settings(**opts) }
  end
end