Module: Adapter::ElasticSearch

Defined in:
lib/adapter/elasticsearch.rb,
lib/adapter/elasticsearch/version.rb

Constant Summary collapse

NonHashValueKeyName =
'_value'
VERSION =
"0.0.4"

Instance Method Summary collapse

Instance Method Details

#clearObject



25
26
27
# File 'lib/adapter/elasticsearch.rb', line 25

def clear
  client.remove_all(@options[:type])
end

#decode(value) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/adapter/elasticsearch.rb', line 33

def decode(value)
  return if value.nil?

  if value['_source'].key?(NonHashValueKeyName)
    value['_source'][NonHashValueKeyName]
  else
    value['_source'].merge({'id' => value['_id']})
  end
end

#delete(key) ⇒ Object



21
22
23
# File 'lib/adapter/elasticsearch.rb', line 21

def delete(key)
  read(key).tap { client.remove(@options[:type], key_for(key)) }
end

#encode(value) ⇒ Object



29
30
31
# File 'lib/adapter/elasticsearch.rb', line 29

def encode(value)
  value.is_a?(Hash) ? value : {NonHashValueKeyName => value}
end

#read(key) ⇒ Object



8
9
10
11
# File 'lib/adapter/elasticsearch.rb', line 8

def read(key)
  doc = client.mget(@options[:type], [key_for(key)])['docs'].first
  doc['exists'] ? decode(doc) : nil
end

#write(key, value) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/adapter/elasticsearch.rb', line 13

def write(key, value)
  options = {}
  if @options.key?(:parent) && value.is_a?(Hash)
    options[:parent] = value[@options[:parent].to_s]
  end
  client.add(@options[:type], key_for(key), encode(value), options)
end