class Elastomer::Client::Index

Attributes

client[R]
name[R]

Public Class Methods

new( client, name ) click to toggle source

Create a new index client for making API requests that pertain to the health and management of individual indexes.

client - Elastomer::Client used for HTTP requests to the server name - The name of the index as a String or an Array of names

# File lib/elastomer/client/index.rb, line 25
def initialize( client, name )
  @client = client
  @name   = @client.assert_param_presence(name, "index name") unless name.nil?
end

Public Instance Methods

add_alias( name, params = {} ) click to toggle source

Add a single alias to this index.

name - Name of the alias to add to the index params - Parameters Hash

:routing - optional routing that can be associated with an alias
:filter  - optional filter that can be associated with an alias

Examples

index.add_alias("foo", routing: "foo")

See www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html

Returns the response body as a Hash

# File lib/elastomer/client/index.rb, line 200
def add_alias( name, params = {} )
  response = client.put "/{index}/_alias/{name}", update_params(params, name: name, action: "index.add_alias", rest_api: "indices.put_alias")
  response.body
end
aliases( params = {} )
Alias for: get_aliases
analyze( text, params = {} ) click to toggle source

Perform the analysis process on some text and return the tokens breakdown of the text.

text - The text to analyze as a String params - Parameters Hash

See www.elastic.co/guide/en/elasticsearch/reference/current/indices-analyze.html

Returns the response body as a Hash

# File lib/elastomer/client/index.rb, line 232
def analyze( text, params = {} )
  body = text.is_a?(Hash) ? text : {text: text.to_s}
  response = client.get "{/index}/_analyze", update_params(params, body: body, action: "index.analyze", rest_api: "indices.analyze")
  response.body
end
app_delete_by_query(query, params = nil) click to toggle source

DEPRECATED: Delete documents from one or more indices and one or more types based on a query using application-level logic.

See Client#app_delete_by_query for more information.

Returns a Hash of statistics about the delete operations simulating the Elasticsearch 2.x delete by query plugin's output.

# File lib/elastomer/client/index.rb, line 546
def app_delete_by_query(query, params = nil)
  docs.app_delete_by_query(query, params)
end
bulk( params = {}, &block ) click to toggle source

Perform bulk indexing and/or delete operations. The current index name will be passed to the bulk API call as part of the request parameters.

params - Parameters Hash that will be passed to the bulk API call. block - Required block that is used to accumulate bulk API operations.

All the operations will be passed to the search cluster via a
single API request.

Yields a Bulk instance for building bulk API call bodies.

Examples

index.bulk do |b|
  b.index( document1 )
  b.index( document2 )
  b.delete( document3 )
  ...
end

See www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html

Returns the response body as a Hash

# File lib/elastomer/client/index.rb, line 387
def bulk( params = {}, &block )
  raise "a block is required" if block.nil?

  params = {index: self.name}.merge params
  client.bulk params, &block
end
clear_cache( params = {} ) click to toggle source

Clear caches for one or more indices. Individual caches can be specified with parameters.

params - Parameters Hash

:index - set to "_all" to clear all index caches

See www.elastic.co/guide/en/elasticsearch/reference/current/indices-clearcache.html

Returns the response body as a Hash

# File lib/elastomer/client/index.rb, line 303
def clear_cache( params = {} )
  response = client.post "{/index}/_cache/clear", update_params(params, action: "index.clear_cache", rest_api: "indices.clear_cache")
  response.body
end
close( params = {} ) click to toggle source

Close the index.

params - Parameters Hash

See www.elastic.co/guide/en/elasticsearch/reference/current/indices-open-close.html

Returns the response body as a Hash

# File lib/elastomer/client/index.rb, line 92
def close( params = {} )
  response = client.post "/{index}/_close", update_params(params, action: "index.close", rest_api: "indices.close")
  response.body
end
create( body, params = {} ) click to toggle source

Create the index.

body - The index settings and mappings as a Hash or a JSON encoded String params - Parameters Hash

See www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html

Returns the response body as a Hash

# File lib/elastomer/client/index.rb, line 56
def create( body, params = {} )
  response = client.put "/{index}", update_params(params, body: body, action: "index.create", rest_api: "indices.create")
  response.body
end
defaults() click to toggle source

Internal: Returns a Hash containing default parameters.

# File lib/elastomer/client/index.rb, line 589
def defaults
  { index: name }
end
delete( params = {} ) click to toggle source

Delete the index.

params - Parameters Hash

See www.elastic.co/guide/en/elasticsearch/reference/current/indices-delete-index.html

Returns the response body as a Hash

# File lib/elastomer/client/index.rb, line 68
def delete( params = {} )
  response = client.delete "/{index}", update_params(params, action: "index.delete", rest_api: "indices.delete")
  response.body
end
delete_alias( name, params = {} ) click to toggle source

Delete an alias from this index.

name - Name of the alias to delete from the index params - Parameters Hash

Examples

index.delete_alias("foo")
index.delete_alias(["foo", "bar"])

See www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html

Returns the response body as a Hash

# File lib/elastomer/client/index.rb, line 218
def delete_alias( name, params = {} )
  response = client.delete "/{index}/_alias/{name}", update_params(params, name: name, action: "index.delete_alias", rest_api: "indices.delete_alias")
  response.body
end
delete_by_query(query, params = nil) click to toggle source

Delete documents by query following either the native or application-level delete by query method.

NOTE: The parameters and response format varies by version. To have more control over this, use app_delete_by_query or native_delete_by_query directly.

# File lib/elastomer/client/index.rb, line 535
def delete_by_query(query, params = nil)
  docs.send(client.version_support.delete_by_query_method, query, params)
end
docs( type = nil ) click to toggle source

Provides access to document-level API commands. These commands will be scoped to this index and the give `type`, if any.

type - The document type as a String

See www.elastic.co/guide/en/elasticsearch/reference/current/docs.html

Returns a Docs instance.

# File lib/elastomer/client/index.rb, line 348
def docs( type = nil )
  client.docs name, type
end
exist?( params = {} )
Alias for: exists?
exists?( params = {} ) click to toggle source

Check for the existence of the index. If a `:type` option is given, then we will check for the existence of the document type in the index.

params - Parameters Hash

:type - optional type mapping as a String

See www.elastic.co/guide/en/elasticsearch/reference/current/indices-exists.html and www.elastic.co/guide/en/elasticsearch/reference/current/indices-types-exists.html

Returns true if the index (or type) exists

# File lib/elastomer/client/index.rb, line 42
def exists?( params = {} )
  response = client.head "/{index}{/type}", update_params(params, action: "index.exists", rest_api: "indices.exists")
  response.success?
end
Also aliased as: exist?
flush( params = {} ) click to toggle source

Flush one or more indices to the index storage.

params - Parameters Hash

:index - set to "_all" to flush all indices

See www.elastic.co/guide/en/elasticsearch/reference/current/indices-flush.html

Returns the response body as a Hash

# File lib/elastomer/client/index.rb, line 260
def flush( params = {} )
  response = client.post "{/index}/_flush", update_params(params, action: "index.flush", rest_api: "indices.flush")
  response.body
end
forcemerge( params = {} ) click to toggle source

Force merge one or more indices. Force merging an index allows to reduce the number of segments but can be resource intensive.

params - Parameters Hash

:index - set to "_all" to force merge all indices

See www.elastic.co/guide/en/elasticsearch/reference/current/indices-forcemerge.html

Returns the response body as a Hash

# File lib/elastomer/client/index.rb, line 274
def forcemerge( params = {} )
  response = client.post "{/index}/_forcemerge", update_params(params, action: "index.forcemerge", rest_api: "indices.forcemerge")
  response.body
end
Also aliased as: optimize
get_alias( name, params = {} ) click to toggle source

Return the named aliases associated with this index.

name - Name of the alias to look up params - Parameters Hash

:ignore_unavailable - What to do if a specified index name doesn’t
                      exist. If set to `true` then those indices are ignored.

Examples

index.get_alias("*")       # returns all aliases for the current index
index.get_alias("issue*")  # returns all aliases starting with "issue"

See www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html

Returns the response body as a Hash

# File lib/elastomer/client/index.rb, line 181
def get_alias( name, params = {} )
  response = client.get "/{index}/_alias/{name}", update_params(params, name: name, action: "index.get_alias", rest_api: "indices.get_alias")
  response.body
end
get_aliases( params = {} ) click to toggle source

Return the aliases associated with this index.

params - Parameters Hash

See www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html

Returns the response body as a Hash

# File lib/elastomer/client/index.rb, line 160
def get_aliases( params = {} )
  response = client.get "/{index}/_alias", update_params(action: "index.get_aliases", rest_api: "indices.get_alias")
  response.body
end
Also aliased as: aliases
get_mapping( params = {} ) click to toggle source

Retrieve one or more mappings from the index. To retrieve a specific mapping provide the name as the `:type` parameter.

params - Parameters Hash

:type - specific document type as a String or Array of Strings

See www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-mapping.html

Returns the response body as a Hash

# File lib/elastomer/client/index.rb, line 132
def get_mapping( params = {} )
  response = client.get "/{index}/_mapping{/type}", update_params(params, action: "index.get_mapping", rest_api: "indices.get_mapping")
  response.body
end
Also aliased as: mapping
get_settings( params = {} ) click to toggle source

Retrieve the settings for the index.

params - Parameters Hash

See www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-settings.html

Returns the response body as a Hash

# File lib/elastomer/client/index.rb, line 104
def get_settings( params = {} )
  response = client.get "{/index}/_settings", update_params(params, action: "index.get_settings", rest_api: "indices.get_settings")
  response.body
end
Also aliased as: settings
mapping( params = {} )
Alias for: get_mapping
multi_percolate(params = {}, &block) click to toggle source

Execute an array of percolate actions in bulk. Results are returned in an array in the order the actions were sent. The current index name will be passed to the API call as part of the request parameters.

See www.elastic.co/guide/en/elasticsearch/reference/current/search-percolate.html#_multi_percolate_api

params - Optional request parameters as a Hash block - Passed to a MultiPercolate instance which assembles the

percolate actions into a single request.

Examples

# block form
multi_percolate do |m|
  m.percolate({ author: "pea53" }, { type: 'default-type' })
  m.count({ author: "pea53" }, { type: 'type2' })
  ...
end

Returns the response body as a Hash

# File lib/elastomer/client/index.rb, line 502
def multi_percolate(params = {}, &block)
  params = defaults.merge params
  client.multi_percolate(params, &block)
end
native_delete_by_query(query, params = nil) click to toggle source

Delete documents from one or more indices and one or more types based on a query using Elasticsearch's _delete_by_query API.

See Client#native_delete_by_query for more information.

Returns a Hash of statistics about the delete operations as returned by _delete_by_query.

Raises Elastomer::Client::IncompatibleVersionException if this version of Elasticsearch does not support _delete_by_query.

# File lib/elastomer/client/index.rb, line 560
def native_delete_by_query(query, params = nil)
  docs.native_delete_by_query(query, params)
end
open( params = {} ) click to toggle source

Open the index.

params - Parameters Hash

See www.elastic.co/guide/en/elasticsearch/reference/current/indices-open-close.html

Returns the response body as a Hash

# File lib/elastomer/client/index.rb, line 80
def open( params = {} )
  response = client.post "/{index}/_open", update_params(params, action: "index.open", rest_api: "indices.open")
  response.body
end
optimize( params = {} )

DEPRECATED: ES 5.X has removed the `/_optimize` endpoint.

Alias for: forcemerge
percolator(id) click to toggle source

Constructs a Percolator with the given id on this index.

Examples

index.percolator "1"

Returns a Percolator

# File lib/elastomer/client/index.rb, line 571
def percolator(id)
  Percolator.new(client, name, id)
end
put_mapping( type, body, params = {} )
Alias for: update_mapping
recovery( params = {} ) click to toggle source

Provides insight into ongoing index shard recoveries. Recovery status may be reported for specific indices, or cluster-wide.

params - Parameters Hash

See www.elastic.co/guide/en/elasticsearch/reference/current/indices-recovery.html

Returns the response body as a Hash

# File lib/elastomer/client/index.rb, line 289
def recovery( params = {} )
  response = client.get "{/index}/_recovery", update_params(params, action: "index.recovery", rest_api: "indices.recovery")
  response.body
end
refresh( params = {} ) click to toggle source

Explicitly refresh one or more index, making all operations performed since the last refresh available for search.

params - Parameters Hash

:index - set to "_all" to refresh all indices

See www.elastic.co/guide/en/elasticsearch/reference/current/indices-refresh.html

Returns the response body as a Hash

# File lib/elastomer/client/index.rb, line 247
def refresh( params = {} )
  response = client.post "{/index}/_refresh", update_params(params, action: "index.refresh", rest_api: "indices.refresh")
  response.body
end
scan( query, opts = {} ) click to toggle source

Create a new Scroller instance for scanning all results from a `query`. The Scroller will be scoped to the current index. The Scroller is configured to use `scan` semantics which are more efficient than a standard scroll query; the caveat is that the returned documents cannot be sorted.

query - The query to scan as a Hash or a JSON encoded String opts - Options Hash

:index  - the name of the index to search
:type   - the document type to search
:scroll - the keep alive time of the scrolling request (5 minutes by default)
:size   - the number of documents per shard to fetch per scroll

Examples

scan = index.scan('{"query":{"match_all":{}}}')
scan.each_document do |document|
  document['_id']
  document['_source']
end

See www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html

Returns a new Scroller instance

# File lib/elastomer/client/index.rb, line 444
def scan( query, opts = {} )
  opts = {index: name}.merge opts
  client.scan query, opts
end
scroll( query, opts = {} ) click to toggle source

Create a new Scroller instance for scrolling all results from a `query`. The Scroller will be scoped to the current index.

query - The query to scroll as a Hash or a JSON encoded String opts - Options Hash

:index  - the name of the index to search
:type   - the document type to search
:scroll - the keep alive time of the scrolling request (5 minutes by default)
:size   - the number of documents per shard to fetch per scroll

Examples

scroll = index.scroll('{"query":{"match_all":{}},"sort":{"date":"desc"}}')
scroll.each_document do |document|
  document['_id']
  document['_source']
end

See www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html

Returns a new Scroller instance

# File lib/elastomer/client/index.rb, line 415
def scroll( query, opts = {} )
  opts = {index: name}.merge opts
  client.scroll query, opts
end
segments( params = {} ) click to toggle source

Retrieve low level Lucene segments information for shards of one or more indices.

params - Parameters Hash

See www.elastic.co/guide/en/elasticsearch/reference/current/indices-segments.html

Returns the response body as a Hash

# File lib/elastomer/client/index.rb, line 335
def segments( params = {} )
  response = client.get "{/index}/_segments", update_params(params, action: "index.segments", rest_api: "indices.segments")
  response.body
end
settings( params = {} )
Alias for: get_settings
stats( params = {} ) click to toggle source

Retrieve statistics about one or more indices. Specific statistics can be retrieved with parameters.

params - Parameters Hash

:stats - a single stats value or an Array of stats values

Examples

stats(stats: "docs")
stats(stats: %w[flush merge])

See www.elastic.co/guide/en/elasticsearch/reference/current/indices-stats.html

Returns the response body as a Hash

# File lib/elastomer/client/index.rb, line 322
def stats( params = {} )
  response = client.get "{/index}/_stats{/stats}", update_params(params, action: "index.stats", rest_api: "indices.stats")
  response.body
end
suggest(query, params = {}) click to toggle source

Exposes the `/_suggest` endpoint of the Elasticsearch API.

query - The query body as a Hash params - Parameters Hash

See www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters.html

Returns the response body as a Hash

# File lib/elastomer/client/index.rb, line 360
def suggest(query, params = {})
  response = client.post "{/index}/_suggest", update_params(params, body: query, action: "index.suggest", rest_api: "suggest")
  response.body
end
update_mapping( type, body, params = {} ) click to toggle source

Register specific mapping definition for a specific type.

type - Name of the mapping to update as a String body - The mapping values to update as a Hash or a JSON encoded String params - Parameters Hash

See www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html

Returns the response body as a Hash

# File lib/elastomer/client/index.rb, line 147
def update_mapping( type, body, params = {} )
  response = client.put "/{index}/_mapping/{type}", update_params(params, body: body, type: type, action: "index.update_mapping", rest_api: "indices.put_mapping")
  response.body
end
Also aliased as: put_mapping
update_params( params, overrides = nil ) click to toggle source

Internal: Add default parameters to the `params` Hash and then apply `overrides` to the params if any are given.

params - Parameters Hash overrides - Optional parameter overrides as a Hash

Returns a new params Hash.

# File lib/elastomer/client/index.rb, line 582
def update_params( params, overrides = nil )
  h = defaults.update params
  h.update overrides unless overrides.nil?
  h
end
update_settings( body, params = {} ) click to toggle source

Change specific index level settings in real time.

body - The index settings as a Hash or a JSON encoded String params - Parameters Hash

See www.elastic.co/guide/en/elasticsearch/reference/current/indices-update-settings.html

Returns the response body as a Hash

# File lib/elastomer/client/index.rb, line 118
def update_settings( body, params = {} )
  response = client.put "{/index}/_settings", update_params(params, body: body, action: "index.update_settings", rest_api: "indices.put_settings")
  response.body
end
warmer(warmer_name) click to toggle source

Provides access to warmer API commands. Index warmers run search requests to warm up the index before it is available for searching. Warmers are useful for searches that require heavy data loading, such as faceting or sorting.

The warmer api allows creating, deleting, and retrieving registered warmers.

warmer_name - The name of the warmer to operate on.

Examples

index.warmer('warmer1').create(query: {match_all: {}})
index.warmer('warmer1').get
index.warmer('warmer1').delete

See www.elastic.co/guide/en/elasticsearch/reference/current/indices-warmers.html

Returns a new Warmer instance

# File lib/elastomer/client/index.rb, line 525
def warmer(warmer_name)
  Warmer.new(client, name, warmer_name)
end