Class: Desi::IndexManager

Inherits:
Object
  • Object
show all
Defined in:
lib/desi/index_manager.rb

Overview

Performs some simple index-related operations on a local or distance Elastic Search cluster

Defined Under Namespace

Classes: Index

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ void

Note:

The :http_client_factory should return an instance that responds to #get and #delete

Initializes a Desi::IndexManager instance

Parameters:

  • opts (#to_hash) (defaults to: {})

    Hash of extra opts

Options Hash (opts):

  • :host (#to_s) — default: 'http://localhost:9200'

    Host to manage indices for

  • :verbose (Boolean) — default: nil

    Whether to output the actions’ result on STDOUT

  • :http_client_factory (#new) — default: Desi::HttpClient

    HTTP transport class to use



76
77
78
79
80
81
# File 'lib/desi/index_manager.rb', line 76

def initialize(opts = {})
  @host = to_uri(opts.fetch(:host) { Desi.configuration.server })
  @verbose = opts[:verbose]
  @outputter = opts.fetch(:outputter, Kernel)
  @client = opts.fetch(:http_client_factory, Desi::HttpClient).new(@host)
end

Instance Method Details

#close!(pattern) ⇒ void

Note:

No confirmation is needed, so beware!

Note:

This method will also output its result on STDOUT if @verbose is true

This method returns an undefined value.

Close all indices matching the specified pattern

Examples:

Close all indices whose name begins with “test”

Desi::IndexManager.new.close!('^test') #=> nil

Parameters:

  • pattern (#to_s)

    Regexp pattern used to restrict the selection



149
150
151
152
153
154
155
156
157
158
# File 'lib/desi/index_manager.rb', line 149

def close!(pattern)
  warn "You must provide a pattern" and exit if pattern.nil?

  @outputter.puts "The following indices from host #{@host} are now closed" if @verbose

  indices(Regexp.new(pattern)).each do |index|
    @client.post("/#{index}/_close")
    @outputter.puts " * #{index.inspect}" if @verbose
  end
end

#delete!(pattern) ⇒ void

Note:

No confirmation is needed, so beware!

Note:

This method will also output its result on STDOUT if @verbose is true

This method returns an undefined value.

Delete all indices matching the specified pattern

Examples:

Delete all indices whose name begins with “test”

Desi::IndexManager.new.delete!('^test') #=> nil

Parameters:

  • pattern (#to_s)

    Regexp pattern used to restrict the selection



124
125
126
127
128
129
130
131
132
133
# File 'lib/desi/index_manager.rb', line 124

def delete!(pattern)
  warn "You must provide a pattern" and exit if pattern.nil?

  @outputter.puts "The following indices from host #{@host} are now deleted" if @verbose

  indices(Regexp.new(pattern)).each do |index|
    @client.delete("/#{index}")
    @outputter.puts " * #{index.inspect}" if @verbose
  end
end

#empty!(pattern) ⇒ void

Note:

No confirmation is needed, so beware!

Note:

This method will also output its result on STDOUT if @verbose is true

This method returns an undefined value.

Empty (remove all records) from indices matching the specified pattern

Examples:

Empty all indices whose name begins with “log”

Desi::IndexManager.new.empty!('^log') #=> nil

Parameters:

  • pattern (#to_s)

    Regexp pattern used to restrict the selection



174
175
176
177
178
179
180
181
182
183
# File 'lib/desi/index_manager.rb', line 174

def empty!(pattern)
  warn "You must provide a pattern" and exit if pattern.nil?

  @outputter.puts "The following indices from host #{@host} are now emptied" if @verbose

  indices(Regexp.new(pattern)).each do |index|
    @client.delete("/#{index}/_query?q=*")
    @outputter.puts " * #{index}" if @verbose
  end
end

#list(pattern = '.*') ⇒ Array<String>

Note:

This method will also output its result on STDOUT if @verbose is true

List index names for the specified cluster

You can restrict the list using a regular expression pattern. (The default pattern being /.*/, all releases will be returned if you do not specify anything.)

Examples:

List all indices whose name begins with “foo”

Desi::IndexManager.new.list('^foo') #=> ["foo1", "foo2", "foo3"]

Parameters:

  • pattern (#to_s) (defaults to: '.*')

    (‘.*’) Regexp pattern used to restrict the selection

Returns:

  • (Array<String>)

    List of index names of the ES cluster



100
101
102
103
104
105
106
107
108
# File 'lib/desi/index_manager.rb', line 100

def list(pattern = '.*')
  pattern = Regexp.new(pattern || '.*')

  @outputter.puts "Indices from host #{@host} matching the pattern #{pattern.inspect}\n\n" if @verbose

  list = indices(pattern).sort
  list.each {|i| @outputter.puts i.inspect } if @verbose
  list
end