Class: Elasticsearch::Extensions::Documents::DirectIndexStore

Inherits:
Object
  • Object
show all
Defined in:
lib/elasticsearch/extensions/documents/direct_index_store.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ DirectIndexStore

Returns a new instance of DirectIndexStore.



7
8
9
10
# File 'lib/elasticsearch/extensions/documents/direct_index_store.rb', line 7

def initialize(options = {})
  @client  = options.fetch(:client)  { Documents.client }
  @storage = options.fetch(:storage) { Storage.new }
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



5
6
7
# File 'lib/elasticsearch/extensions/documents/direct_index_store.rb', line 5

def client
  @client
end

#storageObject (readonly)

Returns the value of attribute storage.



5
6
7
# File 'lib/elasticsearch/extensions/documents/direct_index_store.rb', line 5

def storage
  @storage
end

Instance Method Details

#bulk_index(documents) ⇒ Object



42
43
44
# File 'lib/elasticsearch/extensions/documents/direct_index_store.rb', line 42

def bulk_index(documents)
  client.bulk body: bulk_index_operations(documents)
end

#bulk_index_operation_hash(document) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/elasticsearch/extensions/documents/direct_index_store.rb', line 50

def bulk_index_operation_hash(document)
  {
    index: {
      _index: Documents.index_name,
      _type:  document.class.type,
      _id:    document.id,
      data:   document.as_hash,
    }
  }
end

#bulk_index_operations(documents) ⇒ Object



46
47
48
# File 'lib/elasticsearch/extensions/documents/direct_index_store.rb', line 46

def bulk_index_operations(documents)
  documents.collect { |document| bulk_index_operation_hash(document) }
end

#delete(payload) ⇒ Object



16
17
18
# File 'lib/elasticsearch/extensions/documents/direct_index_store.rb', line 16

def delete(payload)
  client.delete payload.merge(index: Documents.index_name)
end

#index(payload) ⇒ Object



12
13
14
# File 'lib/elasticsearch/extensions/documents/direct_index_store.rb', line 12

def index(payload)
  client.index payload.merge(index: Documents.index_name)
end

#refreshObject



24
25
26
# File 'lib/elasticsearch/extensions/documents/direct_index_store.rb', line 24

def refresh
  client.indices.refresh index: Documents.index_name
end

#reindex(options = {}, &block) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/elasticsearch/extensions/documents/direct_index_store.rb', line 28

def reindex(options = {}, &block)
  force_create = options.fetch(:force_create) { false }

  storage.drop_index(Documents.index_name) if force_create
  storage.create_index(Documents.index_name)

  block.call(self) if block_given?
end

#search(payload) ⇒ Object



20
21
22
# File 'lib/elasticsearch/extensions/documents/direct_index_store.rb', line 20

def search(payload)
  client.search payload.merge(index: Documents.index_name)
end

#setupObject



37
38
39
40
# File 'lib/elasticsearch/extensions/documents/direct_index_store.rb', line 37

def setup
  storage.drop_index(Documents.index_name)
  storage.create_index(Documents.index_name)
end