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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/stretchy/utils/client_actions.rb', line 58

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
  Stretchy.log("Bulk indexing documents:", {body: requests})
  response = client.bulk body: requests
  Stretchy.log("Received response:", response)
end

#countObject



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

def count
  Stretchy.log("Counting all documents in index: #{index_name}")
  client.cat.count(index: index_name).split(' ')[2].to_i
end

#create(_index_name = index_name) ⇒ Object



84
85
86
87
# File 'lib/stretchy/utils/client_actions.rb', line 84

def create(_index_name = index_name)
  Stretchy.log("Creating index: #{_index_name}")
  client.indices.create(index: _index_name) unless exists?(_index_name)
end

#delete(_index_name = index_name) ⇒ Object



79
80
81
82
# File 'lib/stretchy/utils/client_actions.rb', line 79

def delete(_index_name = index_name)
  Stretchy.log("Deleting index: #{_index_name}")
  client.indices.delete(index: _index_name) if exists?(_index_name)
end

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



73
74
75
76
# File 'lib/stretchy/utils/client_actions.rb', line 73

def exists(_index_name = index_name)
  Stretchy.log("Checking index existence for: #{_index_name}")
  client.indices.exists(index: _index_name)
end

#index(options = {}) ⇒ Object



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

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]
  params = {
    index:  index,
    type:   type,
    id:     id,
    body:   body
  }
  Stretchy.log("Indexing document:", params)
  response = client.index(params)
  Stretchy.log("Received response:", response)
  response
end

#mapping(_index_name, _type, _body) ⇒ Object



89
90
91
92
# File 'lib/stretchy/utils/client_actions.rb', line 89

def mapping(_index_name, _type, _body)
  Stretchy.log("Putting mapping:", {index_name: _index_name, type: _type, body: _body})
  client.indices.put_mapping(index: _index_name, type: _type, body: _body)
end

#query(*args, &block) ⇒ Object



22
23
24
# File 'lib/stretchy/utils/client_actions.rb', line 22

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

#refreshObject

used for ensuring a consistent index in specs



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

def refresh
  Stretchy.log("Refreshing index: #{index_name}")
  client.indices.refresh index: index_name
end

#search(options = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/stretchy/utils/client_actions.rb', line 26

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

  Stretchy.log("Querying Elastic:", params)
  response = client.search(params)
  Stretchy.log("Received response:", response)
  response
end