Module: Stretchy::Utils::ClientActions

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



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

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



64
65
66
# File 'lib/stretchy/utils/client_actions.rb', line 64

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

#delete(_index_name = index_name) ⇒ Object



60
61
62
# File 'lib/stretchy/utils/client_actions.rb', line 60

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?



55
56
57
# File 'lib/stretchy/utils/client_actions.rb', line 55

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

#index(options = {}) ⇒ Object



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

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



68
69
70
# File 'lib/stretchy/utils/client_actions.rb', line 68

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
# File 'lib/stretchy/utils/client_actions.rb', line 24

def search(options = {})
  params = {}
  params[:index]  = options[:index] || index_name
  params[:type]   = options[:type]
  params[:fields] = Array(options[:fields]) if options[:fields]
  params[:body]   = options[:body]

  client.search(params)
end