Class: Appfuel::ElasticSearch::Adapter

Inherits:
Object
  • Object
show all
Includes:
Application::AppContainer
Defined in:
lib/appfuel/storage/elastic_search/adapter.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Application::AppContainer

#app_container, #feature_name, included, #qualify_container_key

Class Method Details

.clientObject



52
53
54
# File 'lib/appfuel/storage/elastic_search/adapter.rb', line 52

def client
  @client ||= app_container[CLIENT_CONTAINER_KEY]
end

.configObject



48
49
50
# File 'lib/appfuel/storage/elastic_search/adapter.rb', line 48

def config
  @config ||= load_config
end

.config_key(value = nil) ⇒ Object



24
25
26
27
28
29
# File 'lib/appfuel/storage/elastic_search/adapter.rb', line 24

def config_key(value = nil)
  if value.nil?
    return @config_key ||= DEFAULT_CONFIG_KEY
  end
  @config_key = value.to_s
end

.container_class_typeObject



20
21
22
# File 'lib/appfuel/storage/elastic_search/adapter.rb', line 20

def container_class_type
  'elastic_search'
end

.index_name(value = nil) ⇒ Object



56
57
58
59
# File 'lib/appfuel/storage/elastic_search/adapter.rb', line 56

def index_name(value = nil)
  return @index_name if value.nil?
  @index_name = value.to_s
end

.inherited(klass) ⇒ Object



61
62
63
# File 'lib/appfuel/storage/elastic_search/adapter.rb', line 61

def inherited(klass)
  stage_class_for_registration(klass)
end

.load_configObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/appfuel/storage/elastic_search/adapter.rb', line 31

def load_config
  config = app_container[:config]
  key = config_key.to_s
  if key.include?('.')
    keys = key.split('.').map {|k| k.to_sym}
  else
    keys = [config_key]
  end

  @config ||= keys.each.inject(config) do |c, k|
    unless c.key?(k)
      fail "[elastic_search] config key (#{k}) not found - #{self}"
    end
    c[k]
  end
end

Instance Method Details

#batch_documents(operations) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/appfuel/storage/elastic_search/adapter.rb', line 118

def batch_documents(operations)
  unless operations.is_a?(Array)
    fail "[elastic_search] operations should be an array"
  end

  client.bulk body: operations
end

#clientObject

Instance methods



68
69
70
# File 'lib/appfuel/storage/elastic_search/adapter.rb', line 68

def client
  self.class.client
end

#count(type: nil, body: nil) ⇒ Object



76
77
78
79
80
81
# File 'lib/appfuel/storage/elastic_search/adapter.rb', line 76

def count(type: nil, body: nil)
  options = { index: index_name }
  options[:type] = type unless type.nil?
  options[:body] = body unless body.nil?
  client.count options
end

#delete_document(type, id) ⇒ Object



126
127
128
# File 'lib/appfuel/storage/elastic_search/adapter.rb', line 126

def delete_document(type, id)
  client.delete index: index_name, type: type, id: id
end

#index_document(type, id, data) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/appfuel/storage/elastic_search/adapter.rb', line 95

def index_document(type, id, data)
  client.index(
    index: index_name,
    type: type,
    id: id,
    data: data
  )
end

#index_nameObject



72
73
74
# File 'lib/appfuel/storage/elastic_search/adapter.rb', line 72

def index_name
  self.class.index_name
end

#prepare_operation(operation:, type:, id:, data: {}) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/appfuel/storage/elastic_search/adapter.rb', line 104

def prepare_operation(operation:, type:, id:, data: {})
  unless VALID_OPERATIONS.include?(operation)
    fail "[elastic_search] prepare operation '#{opeartion}' is not valid"
  end
  prepare = {}
  prepare[operation.to_sym] = {
    _index: index_name,
    _type: type,
    _id: id,
  }
  prepare[operation.to_sym][:data] = data unless operation == OPS_DELETE
  prepare
end

#quick_search(type: nil, query:) ⇒ Object



89
90
91
92
93
# File 'lib/appfuel/storage/elastic_search/adapter.rb', line 89

def quick_search(type: nil, query:)
  options = { index: index_name, q: query }
  options[:type] = type unless type.nil?
  client.search options
end

#search(type: nil, body:) ⇒ Object



83
84
85
86
87
# File 'lib/appfuel/storage/elastic_search/adapter.rb', line 83

def search(type: nil, body:)
  options = { index: index_name, body: body }
  options[:type] = type unless type.nil?
  client.search options
end