Class: Elasticated::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mixins::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

#bulk(bulk_body, opts = {}) ⇒ Object



126
127
128
129
# File 'lib/elasticated/client.rb', line 126

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

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



91
92
93
94
# File 'lib/elasticated/client.rb', line 91

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.to_h) if mapping # both Hash and Mapping::Builder responds to 'to_h'
  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_by_query(body, opts = {}) ⇒ Object



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

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

#delete_document(document_id, opts = {}) ⇒ Object



121
122
123
124
# File 'lib/elasticated/client.rb', line 121

def delete_document(document_id, opts={})
  log_query :delete, document_id, opts
  transport.delete opts.merge id: document_id
end

#get_document(document_id, opts = {}) ⇒ Object



111
112
113
114
# File 'lib/elasticated/client.rb', line 111

def get_document(document_id, opts={})
  log_query :get, document_id, opts
  transport.get opts.merge id: document_id
end

#get_documents(documents_ids, opts = {}) ⇒ Object



116
117
118
119
# File 'lib/elasticated/client.rb', line 116

def get_documents(documents_ids, opts={})
  log_query :mget, documents_ids, opts
  transport.mget opts.merge body: { ids: documents_ids }
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



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

def refresh
  transport.indices.refresh
end

#refresh_index(index_name) ⇒ Object



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

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

#refresh_indices(index_names) ⇒ Object



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

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



101
102
103
104
# File 'lib/elasticated/client.rb', line 101

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

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



96
97
98
99
# File 'lib/elasticated/client.rb', line 96

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

#update_mapping(index_name, type_name, mapping, opts = {}) ⇒ Object



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

def update_mapping(index_name, type_name, mapping, opts={})
  transport.indices.put_mapping opts.merge(index: index_name, type: type_name, body: mapping)
end

#update_settings(index_name, settings_body, opts = {}) ⇒ Object



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

def update_settings(index_name, settings_body, opts={})
  transport.indices.put_settings opts.merge(index: index_name, body: settings_body)
end