Module: ElasticRecord::Index::Documents

Included in:
ElasticRecord::Index
Defined in:
lib/elastic_record/index/documents.rb

Instance Method Summary collapse

Instance Method Details

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



89
90
91
92
93
94
95
# File 'lib/elastic_record/index/documents.rb', line 89

def bulk(options = {}, &block)
  if current_bulk_batch
    yield
  else
    start_new_bulk_batch(options, &block)
  end
end

#bulk_add(batch, index_name: alias_name) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/elastic_record/index/documents.rb', line 97

def bulk_add(batch, index_name: alias_name)
  bulk do
    batch.each do |record|
      index_record(record, index_name: index_name)
    end
  end
end

#current_bulk_batchObject



105
106
107
# File 'lib/elastic_record/index/documents.rb', line 105

def current_bulk_batch
  connection.bulk_actions
end

#delete_by_query(query) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/elastic_record/index/documents.rb', line 79

def delete_by_query(query)
  scroll_enumerator = build_scroll_enumerator search: query

  scroll_enumerator.each_slice do |hits|
    bulk do
      hits.each { |hit| delete_document hit['_id'] }
    end
  end
end

#delete_document(id, parent: nil, index_name: alias_name) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/elastic_record/index/documents.rb', line 63

def delete_document(id, parent: nil, index_name: alias_name)
  raise "Cannot delete document with empty id" if id.blank?
  index_name ||= alias_name

  if batch = current_bulk_batch
    instructions = { _index: index_name, _id: id, retry_on_conflict: 3 }
    instructions[:parent] = parent if parent
    batch << { delete: instructions }
  else
    path = "/#{index_name}/_doc/#{id}"
    path << "&parent=#{parent}" if parent

    connection.json_delete path
  end
end

#index_document(id, document, parent: nil, index_name: alias_name) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/elastic_record/index/documents.rb', line 26

def index_document(id, document, parent: nil, index_name: alias_name)
  if batch = current_bulk_batch
    instructions = { _index: index_name, _id: id }
    instructions[:parent] = parent if parent

    batch << { index: instructions }
    batch << document
  else
    path = "/#{index_name}/_doc/#{id}"
    path << "?parent=#{parent}" if parent

    if id
      connection.json_put path, document
    else
      connection.json_post path, document
    end
  end
end

#index_record(record, index_name: alias_name) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/elastic_record/index/documents.rb', line 6

def index_record(record, index_name: alias_name)
  unless disabled
    index_document(
      record.try(:id),
      record.as_search_document,
      index_name: index_name
    )
  end
end

#update_document(id, document, parent: nil, index_name: alias_name) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/elastic_record/index/documents.rb', line 45

def update_document(id, document, parent: nil, index_name: alias_name)
  raise "Cannot update a document with empty id" if id.blank?
  params = {doc: document, doc_as_upsert: true}

  if batch = current_bulk_batch
    instructions = { _index: index_name, _id: id, retry_on_conflict: 3 }
    instructions[:parent] = parent if parent

    batch << { update: instructions }
    batch << params
  else
    path = "/#{index_name}/_doc/#{id}/_update?retry_on_conflict=3"
    path << "&parent=#{parent}" if parent

    connection.json_post path, params
  end
end

#update_record(record, index_name: alias_name) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/elastic_record/index/documents.rb', line 16

def update_record(record, index_name: alias_name)
  unless disabled
    update_document(
      record.id,
      record.as_partial_update_document,
      index_name: index_name
    )
  end
end