Class: Elasticated::Client

Inherits:
Object
  • Object
show all
Includes:
Configurable
Defined in:
lib/elasticated/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Configurable

#log

Constructor Details

#initialize(opts = {}) ⇒ Client

Returns a new instance of Client.



7
8
9
# File 'lib/elasticated/client.rb', line 7

def initialize(opts={})
  self.transport = ::Elasticsearch::Client.new opts
end

Instance Attribute Details

#transportObject

Returns the value of attribute transport.



5
6
7
# File 'lib/elasticated/client.rb', line 5

def transport
  @transport
end

Instance Method Details

#count(body, opts = {}) ⇒ Object



70
71
72
73
# File 'lib/elasticated/client.rb', line 70

def count(body, opts={})
  log_query :count, body.to_json, opts
  transport.count opts.merge body: body
end

#create_alias(index_name, new_alias) ⇒ Object



38
39
40
41
# File 'lib/elasticated/client.rb', line 38

def create_alias(index_name, new_alias)
  transport.indices.put_alias index: index_name, name: new_alias
  log.info "Alias '#{new_alias}' for index '#{index_name}' created"
end

#create_index(index_name, opts = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/elasticated/client.rb', line 25

def create_index(index_name, opts={})
  args = { index: index_name }
  shards = opts[:shards]
  mapping = opts[:mapping]
  body = Hash.new
  body.merge!(settings: { number_of_shards: shards }) if shards
  body.merge!(mappings: mapping) if mapping
  args.merge! body: body unless body.empty?
  log.info "Creating index '#{index_name}'"
  transport.indices.create args
  log.info "Index '#{index_name}' created"
end

#delete(body, opts = {}) ⇒ Object



85
86
87
88
# File 'lib/elasticated/client.rb', line 85

def delete(body, opts={})
  log_query :delete, body.to_json, opts
  transport.delete_by_query opts.merge body: body
end

#index_document(document_source, opts = {}) ⇒ Object



11
12
13
14
# File 'lib/elasticated/client.rb', line 11

def index_document(document_source, opts={})
  response = transport.index opts.merge body: document_source
  log.debug "Document indexed #{response.to_json}"
end

#index_exists?(index_name) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/elasticated/client.rb', line 21

def index_exists?(index_name)
  transport.indices.exists index: index_name
end

#refreshObject



66
67
68
# File 'lib/elasticated/client.rb', line 66

def refresh
  transport.indices.refresh
end

#refresh_index(index_name) ⇒ Object



55
56
57
58
# File 'lib/elasticated/client.rb', line 55

def refresh_index(index_name)
  transport.indices.refresh index: index_name
  log.debug "Index '#{index_name}' refreshed"
end

#refresh_indices(index_names) ⇒ Object



60
61
62
63
64
# File 'lib/elasticated/client.rb', line 60

def refresh_indices(index_names)
  indices = index_names*','
  transport.indices.refresh index: indices
  log.debug "Indices '#{indices}' refreshed"
end

#remove_alias(index_name, index_alias) ⇒ Object



50
51
52
53
# File 'lib/elasticated/client.rb', line 50

def remove_alias(index_name, index_alias)
  transport.indices.delete_alias index: index_name, name: index_alias
  log.info "Alias '#{index_alias}' removed from index '#{index_name}'"
end

#remove_aliases(index_name) ⇒ Object



43
44
45
46
47
48
# File 'lib/elasticated/client.rb', line 43

def remove_aliases(index_name)
  aliases = transport.indices.get_aliases[index_name]['aliases'].map(&:first) rescue Array.new
  aliases.each do |index_alias|
    remove_alias index_name, index_alias
  end
end

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



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

def scroll(scroll_id, opts={})
  log_query :scroll, scroll_id, opts
  transport.scroll opts.merge scroll_id: scroll_id
end

#search(body, opts = {}) ⇒ Object



75
76
77
78
# File 'lib/elasticated/client.rb', line 75

def search(body, opts={})
  log_query :search, body.to_json, opts
  transport.search opts.merge body: body
end

#update_document(document_source, opts = {}) ⇒ Object



16
17
18
19
# File 'lib/elasticated/client.rb', line 16

def update_document(document_source, opts={})
  response = transport.update opts.merge body: { doc: document_source }
  log.debug "Document updated #{response.to_json}"
end