Class: Elasticity::IndexMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/elasticity/index_mapper.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document_klass, index_config) ⇒ IndexMapper

Returns a new instance of IndexMapper.



25
26
27
28
29
# File 'lib/elasticity/index_mapper.rb', line 25

def initialize(document_klass, index_config)
  @document_klass = document_klass
  @index_config   = index_config
  @strategy       = @index_config.strategy.new(@index_config.client, @index_config.fq_index_base_name, @index_config.document_type)
end

Class Method Details

.set_delegates(obj, to) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/elasticity/index_mapper.rb', line 3

def self.set_delegates(obj, to)
  obj.delegate(
    :document_type,
    :mapping,
    :ref_index_name,
    :create_index,
    :recreate_index,
    :delete_index,
    :index_exists?,
    :remap!,
    :flush_index,
    :index_document,
    :search,
    :get,
    :delete,
    :delete_by_search,
    :bulk_index,
    :bulk_delete,
    to: to
  )
end

Instance Method Details

#bulk_delete(ids) ⇒ Object

Bulk delete documents matching provided ids



113
114
115
116
117
118
119
# File 'lib/elasticity/index_mapper.rb', line 113

def bulk_delete(ids)
  @strategy.bulk do |b|
    ids.each do |id|
      b.delete(document_type, id)
    end
  end
end

#bulk_index(documents) ⇒ Object

Bulk index the provided documents



104
105
106
107
108
109
110
# File 'lib/elasticity/index_mapper.rb', line 104

def bulk_index(documents)
  @strategy.bulk do |b|
    documents.each do |doc|
      b.index(document_type, doc._id, doc.to_document)
    end
  end
end

#create_indexObject

Creates the index for this document



39
40
41
# File 'lib/elasticity/index_mapper.rb', line 39

def create_index
  @strategy.create_if_undefined(@index_config.definition)
end

#delete(id) ⇒ Object

Removes one specific document from the index.



94
95
96
# File 'lib/elasticity/index_mapper.rb', line 94

def delete(id)
  @strategy.delete_document(document_type, id)
end

#delete_by_search(search) ⇒ Object

Removes entries based on a search



99
100
101
# File 'lib/elasticity/index_mapper.rb', line 99

def delete_by_search(search)
  @strategy.delete_by_query(document_type, search.body)
end

#delete_indexObject

Deletes the index



49
50
51
# File 'lib/elasticity/index_mapper.rb', line 49

def delete_index
  @strategy.delete
end

#flush_indexObject

Flushes the index, forcing any writes



70
71
72
# File 'lib/elasticity/index_mapper.rb', line 70

def flush_index
  @strategy.flush
end

#get(id) ⇒ Object

Fetches one specific document from the index by ID.



88
89
90
91
# File 'lib/elasticity/index_mapper.rb', line 88

def get(id)
  doc = @strategy.get_document(document_type, id)
  @document_klass.new(doc["_source"].merge(_id: doc['_id'])) if doc.present?
end

#index_document(id, document_hash) ⇒ Object

Index the given document



75
76
77
# File 'lib/elasticity/index_mapper.rb', line 75

def index_document(id, document_hash)
  @strategy.index_document(document_type, id, document_hash)
end

#index_exists?Boolean

Does the index exist?

Returns:

  • (Boolean)


54
55
56
# File 'lib/elasticity/index_mapper.rb', line 54

def index_exists?
  !@strategy.missing?
end

#recreate_indexObject

Re-creates the index for this document



44
45
46
# File 'lib/elasticity/index_mapper.rb', line 44

def recreate_index
  @strategy.recreate(@index_config.definition)
end

#ref_index_nameObject

Gets the index name to be used when you need to reference the index somewhere. This depends on the @strategy being used, but it always refers to the search index.



60
61
62
# File 'lib/elasticity/index_mapper.rb', line 60

def ref_index_name
  @strategy.ref_index_name
end

#remap!Object

Remap



65
66
67
# File 'lib/elasticity/index_mapper.rb', line 65

def remap!
  @strategy.remap(@index_config.definition)
end

#search(body) ⇒ Object

Searches the index using the parameters provided in the body hash, following the same structure Elasticsearch expects. Returns a DocumentSearch object.



82
83
84
85
# File 'lib/elasticity/index_mapper.rb', line 82

def search(body)
  search_obj = Search.build(@index_config.client, @strategy.search_index, document_type, body)
  Search::DocumentProxy.new(search_obj, self.method(:map_hit))
end