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

delegated

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 Configuration.transport_options.merge 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



83
84
85
86
# File 'lib/elasticated/client.rb', line 83

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



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

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



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/elasticated/client.rb', line 38

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

#create_percolator(query_source, opts = {}) ⇒ Object



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

def create_percolator(query_source, opts={})
  transport.index index: opts.fetch(:index),
                  type: '.percolator',
                  id: opts.fetch(:id),
                  body: { query: query_source }
end

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



98
99
100
101
# File 'lib/elasticated/client.rb', line 98

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)


34
35
36
# File 'lib/elasticated/client.rb', line 34

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

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



28
29
30
31
32
# File 'lib/elasticated/client.rb', line 28

def percolate(document_source, opts={})
  transport.percolate index: opts.fetch(:index),
                      type: opts.fetch(:type),
                      body: { doc: document_source }
end

#refreshObject



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

def refresh
  transport.indices.refresh
end

#refresh_index(index_name) ⇒ Object



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

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

#refresh_indices(index_names) ⇒ Object



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

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



63
64
65
66
# File 'lib/elasticated/client.rb', line 63

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



56
57
58
59
60
61
# File 'lib/elasticated/client.rb', line 56

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



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

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

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



88
89
90
91
# File 'lib/elasticated/client.rb', line 88

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