Module: Caoutsearch::Index::Document

Extended by:
ActiveSupport::Concern
Included in:
Base
Defined in:
lib/caoutsearch/index/document.rb

Instance Method Summary collapse

Instance Method Details

#delete_document(index: index_name, refresh: false) ⇒ Object

Delete the document

record = Article.find(1)
ArticleIndex.new(record).delete_document


63
64
65
# File 'lib/caoutsearch/index/document.rb', line 63

def delete_document(index: index_name, refresh: false)
  self.class.delete_document(record.id, index: index, refresh: refresh)
end

#indexed_documentObject

Return the indexed document

record = Article.find(1)
ArticleIndex.new(record).indexed_document


13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/caoutsearch/index/document.rb', line 13

def indexed_document
  request_payload = {
    index: index_name,
    id: record.id
  }

  response = instrument(:get) do |event_payload|
    event_payload[:request] = request_payload
    event_payload[:response] = client.get(request_payload)
  end

  response.body
end

#update_document(*keys, index: index_name, refresh: false) ⇒ Object

Overwrite or partially update a document

record = Article.find(1)
ArticleIndex.new(record).update_document
ArticleIndex.new(record).update_document(:title, :content)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/caoutsearch/index/document.rb', line 33

def update_document(*keys, index: index_name, refresh: false)
  request_payload = {
    index: index,
    id: id
  }

  if keys.empty?
    request_payload[:body] = as_json

    instrument(:index) do |event_payload|
      event_payload[:request] = request_payload
      event_payload[:response] = client.index(request_payload)
    end
  else
    request_payload[:body] = bulkify(:update, keys)

    instrument(:bulk, method: :update) do |event_payload|
      event_payload[:request] = request_payload
      event_payload[:response] = client.bulk(request_payload)
    end
  end

  refresh_indice(index: index) if refresh
end