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 = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/elastic_record/index/documents.rb', line 61

def bulk(options = {})
  connection.bulk_stack.push []

  yield

  if current_bulk_batch.any?
    body = current_bulk_batch.map { |action| "#{ActiveSupport::JSON.encode(action)}\n" }.join
    results = connection.json_post("/_bulk?#{options.to_query}", body)
    verify_bulk_results(results)
  end
ensure
  connection.bulk_stack.pop
end

#bulk_add(batch, index_name = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/elastic_record/index/documents.rb', line 75

def bulk_add(batch, index_name = nil)
  index_name ||= alias_name

  bulk do
    batch.each do |record|
      index_record(record, index_name)
    end
  end
end

#current_bulk_batchObject



85
86
87
# File 'lib/elastic_record/index/documents.rb', line 85

def current_bulk_batch
  connection.bulk_stack.last
end

#delete_by_query(query) ⇒ Object



35
36
37
# File 'lib/elastic_record/index/documents.rb', line 35

def delete_by_query(query)
  connection.json_delete "/#{alias_name}/#{type}/_query", query
end

#delete_document(id, index_name = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/elastic_record/index/documents.rb', line 25

def delete_document(id, index_name = nil)
  index_name ||= alias_name

  if batch = current_bulk_batch
    batch << { delete: { _index: index_name, _type: type, _id: id } }
  else
    connection.json_delete "/#{index_name}/#{type}/#{id}"
  end
end

#explain(id, elastic_query) ⇒ Object



52
53
54
# File 'lib/elastic_record/index/documents.rb', line 52

def explain(id, elastic_query)
  get "_explain", elastic_query
end

#index_document(id, document, index_name = nil) ⇒ Object



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

def index_document(id, document, index_name = nil)
  return if disabled

  index_name ||= alias_name

  if batch = current_bulk_batch
    batch << { index: { _index: index_name, _type: type, _id: id } }
    batch << document
  else
    connection.json_put "/#{index_name}/#{type}/#{id}", document
  end
end

#index_record(record, index_name = nil) ⇒ Object



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

def index_record(record, index_name = nil)
  return if disabled

  index_document(record.send(record.class.primary_key), record.as_search, index_name)
end

#record_exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/elastic_record/index/documents.rb', line 39

def record_exists?(id)
  get(id)['exists']
end

#scroll(scroll_id, scroll_keep_alive) ⇒ Object



56
57
58
59
# File 'lib/elastic_record/index/documents.rb', line 56

def scroll(scroll_id, scroll_keep_alive)
  options = {scroll_id: scroll_id, scroll: scroll_keep_alive}
  connection.json_get("/_search/scroll?#{options.to_query}")
end

#search(elastic_query, options = {}) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/elastic_record/index/documents.rb', line 43

def search(elastic_query, options = {})
  url = "_search"
  if options.any?
    url += "?#{options.to_query}"
  end

  get url, elastic_query
end