Class: Elastomer::Client::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/elastomer/client/index.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, name) ⇒ Index

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



25
26
27
28
# 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

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



30
31
32
# File 'lib/elastomer/client/index.rb', line 30

def client
  @client
end

#nameObject (readonly)

Returns the value of attribute name.



30
31
32
# File 'lib/elastomer/client/index.rb', line 30

def name
  @name
end

Instance Method Details

#add_alias(name, params = {}) ⇒ Object

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



214
215
216
217
# File 'lib/elastomer/client/index.rb', line 214

def add_alias( name, params = {} )
  response = client.put "/{index}/_alias/{name}", update_params(params, :name => name, :action => "index.add_alias")
  response.body
end

#analyze(text, params = {}) ⇒ Object

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



246
247
248
249
# File 'lib/elastomer/client/index.rb', line 246

def analyze( text, params = {} )
  response = client.get "{/index}/_analyze", update_params(params, :body => text.to_s, :action => "index.analyze")
  response.body
end

#bulk(params = {}, &block) ⇒ Object

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



423
424
425
426
427
428
# File 'lib/elastomer/client/index.rb', line 423

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 = {}) ⇒ Object

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



330
331
332
333
# File 'lib/elastomer/client/index.rb', line 330

def clear_cache( params = {} )
  response = client.post "{/index}/_cache/clear", update_params(params, :action => "index.clear_cache")
  response.body
end

#close(params = {}) ⇒ Object

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



92
93
94
95
# File 'lib/elastomer/client/index.rb', line 92

def close( params = {} )
  response = client.post "/{index}/_close", update_params(params, :action => "index.close")
  response.body
end

#create(body, params = {}) ⇒ Object

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



56
57
58
59
# File 'lib/elastomer/client/index.rb', line 56

def create( body, params = {} )
  response = client.post "/{index}", update_params(params, :body => body, :action => "index.create")
  response.body
end

#defaultsObject

Internal: Returns a Hash containing default parameters.



600
601
602
# File 'lib/elastomer/client/index.rb', line 600

def defaults
  { :index => name }
end

#delete(params = {}) ⇒ Object

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



68
69
70
71
# File 'lib/elastomer/client/index.rb', line 68

def delete( params = {} )
  response = client.delete "/{index}", update_params(params, :action => "index.delete")
  response.body
end

#delete_alias(name, params = {}) ⇒ Object

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



232
233
234
235
# File 'lib/elastomer/client/index.rb', line 232

def delete_alias( name, params = {} )
  response = client.delete "/{index}/_alias/{name}", update_params(params, :name => name, :action => "index.delete_alias")
  response.body
end

#delete_by_query(query, params = nil) ⇒ Object

Delete documents from one or more indices and one or more types based on a query.

See Client#delete_by_query for more information.

Returns a Hash of statistics about the delete operations



571
572
573
# File 'lib/elastomer/client/index.rb', line 571

def delete_by_query(query, params = nil)
  docs.delete_by_query(query, params)
end

#delete_mapping(type, params = {}) ⇒ Object

Delete the mapping identified by ‘type`. This deletes all documents of that type from the index.

type - Name of the mapping to update as a String params - Parameters Hash

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

Returns the response body as a Hash



162
163
164
165
# File 'lib/elastomer/client/index.rb', line 162

def delete_mapping( type, params = {} )
  response = client.delete "/{index}/{type}", update_params(params, :type => type, :action => "index.delete_mapping")
  response.body
end

#docs(type = nil) ⇒ Object

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.



384
385
386
# File 'lib/elastomer/client/index.rb', line 384

def docs( type = nil )
  client.docs name, type
end

#exists?(params = {}) ⇒ Boolean Also known as: exist?

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

Returns:

  • (Boolean)


42
43
44
45
# File 'lib/elastomer/client/index.rb', line 42

def exists?( params = {} )
  response = client.head "/{index}{/type}", update_params(params, :action => "index.exists")
  response.success?
end

#flush(params = {}) ⇒ Object

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



273
274
275
276
# File 'lib/elastomer/client/index.rb', line 273

def flush( params = {} )
  response = client.post "{/index}/_flush", update_params(params, :action => "index.flush")
  response.body
end

#get_alias(name, params = {}) ⇒ Object

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



195
196
197
198
# File 'lib/elastomer/client/index.rb', line 195

def get_alias( name, params = {} )
  response = client.get "/{index}/_alias/{name}", update_params(params, :name => name, :action => "index.get_alias")
  response.body
end

#get_aliases(params = {}) ⇒ Object Also known as: aliases

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



174
175
176
177
# File 'lib/elastomer/client/index.rb', line 174

def get_aliases( params = {} )
  response = client.get "/{index}/_aliases", update_params(:action => "index.get_aliases")
  response.body
end

#get_mapping(params = {}) ⇒ Object Also known as: mapping

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



132
133
134
135
# File 'lib/elastomer/client/index.rb', line 132

def get_mapping( params = {} )
  response = client.get "/{index}{/type}/_mapping", update_params(params, :action => "index.get_mapping")
  response.body
end

#get_settings(params = {}) ⇒ Object Also known as: settings

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



104
105
106
107
# File 'lib/elastomer/client/index.rb', line 104

def get_settings( params = {} )
  response = client.get "{/index}/_settings", update_params(params, :action => "index.get_settings")
  response.body
end

#multi_percolate(params = {}, &block) ⇒ Object

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



538
539
540
541
# File 'lib/elastomer/client/index.rb', line 538

def multi_percolate(params = {}, &block)
  params = defaults.merge params
  client.multi_percolate(params, &block)
end

#multi_search(params = {}, &block) ⇒ Object

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

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

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

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

Yields a MultiSearch instance for building multi_search API call bodies.

Examples

index.multi_search do |m|
  m.search({:query => {:match_all => {}}, :search_type => :count)
  m.search({:query => {:field => {"author" => "grantr"}}}, :type => 'tweet')
  ...
end

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

Returns the response body as a Hash



511
512
513
514
515
516
# File 'lib/elastomer/client/index.rb', line 511

def multi_search( params = {}, &block )
  raise "a block is required" if block.nil?

  params = {:index => self.name}.merge params
  client.multi_search params, &block
end

#open(params = {}) ⇒ Object

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



80
81
82
83
# File 'lib/elastomer/client/index.rb', line 80

def open( params = {} )
  response = client.post "/{index}/_open", update_params(params, :action => "index.open")
  response.body
end

#optimize(params = {}) ⇒ Object

Optimize one or more indices. Optimizing an index allows for faster search operations but can be resource intensive.

params - Parameters Hash

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

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

Returns the response body as a Hash



287
288
289
290
# File 'lib/elastomer/client/index.rb', line 287

def optimize( params = {} )
  response = client.post "{/index}/_optimize", update_params(params, :action => "index.optimize")
  response.body
end

#percolator(id) ⇒ Object

Constructs a Percolator with the given id on this index.

Examples

index.percolator "1"

Returns a Percolator



582
583
584
# File 'lib/elastomer/client/index.rb', line 582

def percolator(id)
  Percolator.new(client, name, id)
end

#recovery(params = {}) ⇒ Object

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



316
317
318
319
# File 'lib/elastomer/client/index.rb', line 316

def recovery( params = {} )
  response = client.get "{/index}/_recovery", update_params(params, :action => "index.recovery")
  response.body
end

#refresh(params = {}) ⇒ Object

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



260
261
262
263
# File 'lib/elastomer/client/index.rb', line 260

def refresh( params = {} )
  response = client.post "{/index}/_refresh", update_params(params, :action => "index.refresh")
  response.body
end

#scan(query, opts = {}) ⇒ Object

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



480
481
482
483
# File 'lib/elastomer/client/index.rb', line 480

def scan( query, opts = {} )
  opts = {:index => name}.merge opts
  client.scan query, opts
end

#scroll(query, opts = {}) ⇒ Object

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



451
452
453
454
# File 'lib/elastomer/client/index.rb', line 451

def scroll( query, opts = {} )
  opts = {:index => name}.merge opts
  client.scroll query, opts
end

#segments(params = {}) ⇒ Object

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



371
372
373
374
# File 'lib/elastomer/client/index.rb', line 371

def segments( params = {} )
  response = client.get "{/index}/_segments", update_params(params, :action => "index.segments")
  response.body
end

#snapshot(params = {}) ⇒ Object

Deprecated: Explicitly snapshot (backup) one or more indices to the gateway. By default this happens periodically (every 1 second) but the period can be changed or disabled completely.

This API was removed in ES 1.2.

params - Parameters Hash

See www.elastic.co/guide/en/elasticsearch/reference/0.90/indices-gateway-snapshot.html

Returns the response body as a Hash



303
304
305
306
# File 'lib/elastomer/client/index.rb', line 303

def snapshot( params = {} )
  response = client.post "{/index}/_gateway/snapshot", update_params(params, :action => "index.snapshot")
  response.body
end

#stats(params = {}) ⇒ Object

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

params - Parameters Hash

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

Returns the response body as a Hash



343
344
345
346
# File 'lib/elastomer/client/index.rb', line 343

def stats( params = {} )
  response = client.get "{/index}/_stats", update_params(params, :action => "index.stats")
  response.body
end

#status(params = {}) ⇒ Object

Deprecated: Retrieve the status of one or more indices. Recovery and snapshot status can be retrieved with parameters.

This API was deprecated in 1.2 and is slated for removal.

params - Parameters Hash

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

Returns the response body as a Hash



358
359
360
361
# File 'lib/elastomer/client/index.rb', line 358

def status( params = {} )
  response = client.get "{/index}/_status", update_params(params, :action => "index.status")
  response.body
end

#suggest(query, params = {}) ⇒ Object

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



396
397
398
399
# File 'lib/elastomer/client/index.rb', line 396

def suggest(query, params = {})
  response = client.post "{/index}/_suggest", update_params(params, :body => query, :action => "index.suggest")
  response.body
end

#update_mapping(type, body, params = {}) ⇒ Object Also known as: put_mapping

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



147
148
149
150
# File 'lib/elastomer/client/index.rb', line 147

def update_mapping( type, body, params = {} )
  response = client.put "/{index}/{type}/_mapping", update_params(params, :body => body, :type => type, :action => "index.update_mapping")
  response.body
end

#update_params(params, overrides = nil) ⇒ Object

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.



593
594
595
596
597
# File 'lib/elastomer/client/index.rb', line 593

def update_params( params, overrides = nil )
  h = defaults.update params
  h.update overrides unless overrides.nil?
  h
end

#update_settings(body, params = {}) ⇒ Object

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



118
119
120
121
# 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")
  response.body
end

#warmer(warmer_name) ⇒ Object

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



561
562
563
# File 'lib/elastomer/client/index.rb', line 561

def warmer(warmer_name)
  Warmer.new(client, name, warmer_name)
end