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 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



20
21
22
23
# File 'lib/elastomer/client/index.rb', line 20

def initialize( client, name )
  @client = client
  @name   = @client.assert_param_presence(name, 'index name')
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



25
26
27
# File 'lib/elastomer/client/index.rb', line 25

def client
  @client
end

#nameObject (readonly)

Returns the value of attribute name.



25
26
27
# File 'lib/elastomer/client/index.rb', line 25

def name
  @name
end

Instance Method Details

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

Performs the analysis process on a text and return the tokens breakdown of the text. See www.elasticsearch.org/guide/reference/api/admin-indices-analyze/

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

Returns the response body as a Hash



171
172
173
174
# File 'lib/elastomer/client/index.rb', line 171

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

Returns the response body as a Hash



305
306
307
308
309
310
# File 'lib/elastomer/client/index.rb', line 305

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. See www.elasticsearch.org/guide/reference/api/admin-indices-clearcache/

params - Parameters Hash

Returns the response body as a Hash



234
235
236
237
# File 'lib/elastomer/client/index.rb', line 234

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. See www.elasticsearch.org/guide/reference/api/admin-indices-open-close/

params - Parameters Hash

Returns the response body as a Hash



82
83
84
85
# File 'lib/elastomer/client/index.rb', line 82

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

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

Create the index. See www.elasticsearch.org/guide/reference/api/admin-indices-create-index/

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

Returns the response body as a Hash



49
50
51
52
# File 'lib/elastomer/client/index.rb', line 49

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.



401
402
403
# File 'lib/elastomer/client/index.rb', line 401

def defaults
  { :index => name }
end

#delete(params = {}) ⇒ Object

Delete the index. See www.elasticsearch.org/guide/reference/api/admin-indices-delete-index/

params - Parameters Hash

Returns the response body as a Hash



60
61
62
63
# File 'lib/elastomer/client/index.rb', line 60

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

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

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

See www.elasticsearch.org/guide/reference/api/admin-indices-delete-mapping/

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

Returns the response body as a Hash



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

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

Returns a Docs instance.



281
282
283
# File 'lib/elastomer/client/index.rb', line 281

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.

See www.elasticsearch.org/guide/reference/api/admin-indices-indices-exists/ and www.elasticsearch.org/guide/reference/api/admin-indices-types-exists/

params - Parameters Hash

Returns true if the index (or type) exists

Returns:

  • (Boolean)


36
37
38
39
# File 'lib/elastomer/client/index.rb', line 36

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. See www.elasticsearch.org/guide/reference/api/admin-indices-flush/

params - Parameters Hash

Returns the response body as a Hash



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

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

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

Return the aliases associated with this index. See www.elasticsearch.org/guide/reference/api/admin-indices-aliases/

params - Parameters Hash

Returns the response body as a Hash



158
159
160
161
# File 'lib/elastomer/client/index.rb', line 158

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

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

Retrieve the settings for the index. See www.elasticsearch.org/guide/reference/api/admin-indices-get-settings/

params - Parameters Hash

Returns the response body as a Hash



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

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

#mapping(params = {}) ⇒ Object

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

See www.elasticsearch.org/guide/reference/api/admin-indices-get-mapping/

params - Parameters Hash

Returns the response body as a Hash



119
120
121
122
# File 'lib/elastomer/client/index.rb', line 119

def mapping( params = {} )
  response = client.get '/{index}{/type}/_mapping', update_params(params, :action => 'index.mapping')
  response.body
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.elasticsearch.org/guide/reference/api/multi-search/

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

Returns the response body as a Hash



360
361
362
363
364
365
# File 'lib/elastomer/client/index.rb', line 360

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. See www.elasticsearch.org/guide/reference/api/admin-indices-open-close/

params - Parameters Hash

Returns the response body as a Hash



71
72
73
74
# File 'lib/elastomer/client/index.rb', line 71

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. See www.elasticsearch.org/guide/reference/api/admin-indices-optimize/

params - Parameters Hash

Returns the response body as a Hash



207
208
209
210
# File 'lib/elastomer/client/index.rb', line 207

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

#refresh(params = {}) ⇒ Object

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

See www.elasticsearch.org/guide/reference/api/admin-indices-refresh/

params - Parameters Hash

Returns the response body as a Hash



184
185
186
187
# File 'lib/elastomer/client/index.rb', line 184

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

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

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

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

Returns a new Scan instance



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

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

#segments(params = {}) ⇒ Object

Retrieve low level Lucene segments information for shards of one or more indices. See www.elasticsearch.org/guide/reference/api/admin-indices-segments/

params - Parameters Hash

Returns the response body as a Hash



270
271
272
273
# File 'lib/elastomer/client/index.rb', line 270

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. See www.elasticsearch.org/guide/reference/api/admin-indices-gateway-snapshot/

This API was removed in ES 1.2.

params - Parameters Hash

Returns the response body as a Hash



222
223
224
225
# File 'lib/elastomer/client/index.rb', line 222

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. See www.elasticsearch.org/guide/reference/api/admin-indices-stats/

params - Parameters Hash

Returns the response body as a Hash



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

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

#status(params = {}) ⇒ Object

Retrieve the status of one or more indices. Recovery and snapshot status can be retrieved with parameters. See www.elasticsearch.org/guide/reference/api/admin-indices-status/

params - Parameters Hash

Returns the response body as a Hash



258
259
260
261
# File 'lib/elastomer/client/index.rb', line 258

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

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

Register specific mapping definition for a specific type. See www.elasticsearch.org/guide/reference/api/admin-indices-put-mapping/

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

Returns the response body as a Hash



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

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.



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

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. See www.elasticsearch.org/guide/reference/api/admin-indices-update-settings/

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

Returns the response body as a Hash



106
107
108
109
# File 'lib/elastomer/client/index.rb', line 106

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

Returns a new Warmer instance



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

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