Module: ElasticRecord::Index::Documents

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

Instance Method Summary collapse

Instance Method Details

#bulkObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/elastic_record/index/documents.rb', line 51

def bulk
  @batch = []
  yield
  if @batch.any?
    body = @batch.map { |action| "#{ActiveSupport::JSON.encode(action)}\n" }.join
    connection.json_post "/_bulk", body
  end
ensure
  @batch = nil
end

#bulk_add(batch, index_name = nil) ⇒ Object



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

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

  bulk do
    batch.each do |record|
      index_document(record.id, record.as_search, index_name)
    end
  end
end

#delete_document(id, index_name = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/elastic_record/index/documents.rb', line 19

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

  if @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



42
43
44
# File 'lib/elastic_record/index/documents.rb', line 42

def explain(id, elastic_query)
  connection.json_get "/#{alias_name}/#{type}/#{id}/_explain", elastic_query
end

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



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

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

  index_name ||= alias_name

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

#record_exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/elastic_record/index/documents.rb', line 29

def record_exists?(id)
  connection.json_get("/#{alias_name}/#{type}/#{id}")['exists']
end

#scroll(scroll_id, scroll_keep_alive) ⇒ Object



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

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



33
34
35
36
37
38
39
40
# File 'lib/elastic_record/index/documents.rb', line 33

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

  connection.json_get url, elastic_query
end