Class: EsClient::ActiveRecord::Adapter

Inherits:
Object
  • Object
show all
Defined in:
lib/es_client/active_record/adapter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Adapter

Returns a new instance of Adapter.



6
7
8
# File 'lib/es_client/active_record/adapter.rb', line 6

def initialize(model)
  @model = model
end

Instance Attribute Details

#document_type(value = nil) ⇒ Object (readonly)

Returns the value of attribute document_type.



4
5
6
# File 'lib/es_client/active_record/adapter.rb', line 4

def document_type
  @document_type
end

#indexObject (readonly)

Returns the value of attribute index.



4
5
6
# File 'lib/es_client/active_record/adapter.rb', line 4

def index
  @index
end

#index_name(value = nil) ⇒ Object (readonly)

Returns the value of attribute index_name.



4
5
6
# File 'lib/es_client/active_record/adapter.rb', line 4

def index_name
  @index_name
end

#mapping(value = nil) ⇒ Object (readonly)

Returns the value of attribute mapping.



4
5
6
# File 'lib/es_client/active_record/adapter.rb', line 4

def mapping
  @mapping
end

#settings(value = nil) ⇒ Object (readonly)

Returns the value of attribute settings.



4
5
6
# File 'lib/es_client/active_record/adapter.rb', line 4

def settings
  @settings
end

Instance Method Details

#destroy_document(id) ⇒ Object



58
59
60
# File 'lib/es_client/active_record/adapter.rb', line 58

def destroy_document(id)
  index.destroy_document(document_type, id)
end

#find(id) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/es_client/active_record/adapter.rb', line 62

def find(id)
  if id.is_a?(Array)
    query = {query: {ids: {values: id, type: document_type}}, size: id.length}
    index.search(query, type: document_type)
  else
    index.find(document_type, id)
  end
end

#import(records) ⇒ Object



71
72
73
# File 'lib/es_client/active_record/adapter.rb', line 71

def import(records)
  index.bulk :index, document_type, records.map(&:as_indexed_json)
end

#save_document(record) ⇒ Object



48
49
50
# File 'lib/es_client/active_record/adapter.rb', line 48

def save_document(record)
  index.save_document(document_type, record.id, record.as_indexed_json)
end

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



75
76
77
78
# File 'lib/es_client/active_record/adapter.rb', line 75

def search(query, options={})
  options[:type] = document_type
  index.search(query, options)
end

#update_document(record, additional_doc = nil) ⇒ Object



52
53
54
55
56
# File 'lib/es_client/active_record/adapter.rb', line 52

def update_document(record, additional_doc=nil)
  doc = record.changes.map { |k, v| [k, v.last] }.to_h
  doc.deep_merge!(additional_doc) if additional_doc
  index.update_document(document_type, record.id, doc)
end