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.



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

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
24
# 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,
    :map_hit,
    to: to
  )
end

Instance Method Details

#bulk_delete(ids) ⇒ Object

Bulk delete documents matching provided ids



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

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



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

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



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

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

#delete(id) ⇒ Object

Removes one specific document from the index.



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

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

#delete_by_search(search) ⇒ Object

Removes entries based on a search



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

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

#delete_indexObject

Deletes the index



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

def delete_index
  @strategy.delete
end

#flush_indexObject

Flushes the index, forcing any writes



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

def flush_index
  @strategy.flush
end

#get(id) ⇒ Object

Fetches one specific document from the index by ID.



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

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



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

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

#index_exists?Boolean

Does the index exist?

Returns:

  • (Boolean)


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

def index_exists?
  !@strategy.missing?
end

#map_hit(hit) ⇒ Object

Creates a instance of a document from a ElasticSearch hit data.



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/elasticity/index_mapper.rb', line 123

def map_hit(hit)
  attrs = { _id: hit["_id"] }
  attrs.merge!(hit["_source"]) if hit["_source"]

  if hit["highlight"]
    highlighted_attrs = attrs.dup
    attrs_set = Set.new

    hit["highlight"].each do |name, v|
      name = name.gsub(/\..*\z/, '')
      next if attrs_set.include?(name)
      highlighted_attrs[name] = v
      attrs_set << name
    end

    highlighted = @document_klass.new(highlighted_attrs)
  end

  @document_klass.new(attrs.merge(highlighted: highlighted))
end

#recreate_indexObject

Re-creates the index for this document



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

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.



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

def ref_index_name
  @strategy.ref_index_name
end

#remap!Object

Remap



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

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.



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

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