Module: Stretchy::Utils::ClientActions

Included in:
Stretchy
Defined in:
lib/stretchy/utils/client_actions.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



5
6
7
8
9
# File 'lib/stretchy/utils/client_actions.rb', line 5

def self.extended(base)
  unless base.respond_to?(:client) && base.respond_to?(:index_name)
    raise "ClientActions requires methods 'client' and 'index_name'"
  end
end

Instance Method Details

#bulk(options = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/stretchy/utils/client_actions.rb', line 44

def bulk(options = {})
  type      = options[:type]
  documents = options[:documents]
  requests  = documents.flat_map do |document|
    id = document['id'] || document['_id'] || document[:id] || document[:_id]
    [
      { index: { '_index' => index_name, '_type' => type, '_id' => id } },
      document
    ]
  end
  client.bulk body: requests
end

#countObject



16
17
18
# File 'lib/stretchy/utils/client_actions.rb', line 16

def count
  client.cat.count(index: index_name).split(' ')[2].to_i
end

#create(_index_name = index_name) ⇒ Object



66
67
68
# File 'lib/stretchy/utils/client_actions.rb', line 66

def create(_index_name = index_name)
  client.indices.create(index: _index_name) unless exists?(_index_name)
end

#delete(_index_name = index_name) ⇒ Object



62
63
64
# File 'lib/stretchy/utils/client_actions.rb', line 62

def delete(_index_name = index_name)
  client.indices.delete(index: _index_name) if exists?(_index_name)
end

#exists(_index_name = index_name) ⇒ Object Also known as: exists?



57
58
59
# File 'lib/stretchy/utils/client_actions.rb', line 57

def exists(_index_name = index_name)
  client.indices.exists(index: _index_name)
end

#index(options = {}) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/stretchy/utils/client_actions.rb', line 36

def index(options = {})
  index = options[:index] || index_name
  type  = options[:type]
  body  = options[:body]
  id    = options[:id] || options['id'] || body['id'] || body['_id'] || body[:id] || body[:_id]
  client.index(index: index, type: type, id: id, body: body)
end

#mapping(_index_name, _type, _body) ⇒ Object



70
71
72
# File 'lib/stretchy/utils/client_actions.rb', line 70

def mapping(_index_name, _type, _body)
  client.indices.put_mapping(index: _index_name, type: _type, body: _body)
end

#query(*args, &block) ⇒ Object



20
21
22
# File 'lib/stretchy/utils/client_actions.rb', line 20

def query(*args, &block)
  Stretchy::Clauses::Base.new(*args, &block)
end

#refreshObject

used for ensuring a concistent index in specs



12
13
14
# File 'lib/stretchy/utils/client_actions.rb', line 12

def refresh
  client.indices.refresh index: index_name
end

#search(options = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/stretchy/utils/client_actions.rb', line 24

def search(options = {})
  params = {}
  params[:index]  = options[:index] || index_name
  params[:fields] = Array(options[:fields]) if options[:fields]
  
  [:type, :body, :from, :size, :explain].each do |field|
    params[field] = options[field] if options[field]
  end

  client.search(params)
end