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.



28
29
30
31
32
# File 'lib/elasticity/index_mapper.rb', line 28

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, @index_config.use_new_timestamp_format, @index_config.include_type_name_on_create)
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
25
26
# 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,
    :refresh_index,
    :index_document,
    :search,
    :get,
    :delete,
    :delete_by_search,
    :bulk_index,
    :bulk_update,
    :bulk_delete,
    :map_hit,
    to: to
  )
end

Instance Method Details

#bulk_delete(ids) ⇒ Object

Bulk delete documents matching provided ids



138
139
140
141
142
143
144
# File 'lib/elasticity/index_mapper.rb', line 138

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

#bulk_index(documents) ⇒ Object

Bulk index the provided documents



117
118
119
120
121
122
123
# File 'lib/elasticity/index_mapper.rb', line 117

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

#bulk_update(documents) ⇒ Object

Bulk update the specicied attribute of the provided documents



126
127
128
129
130
131
132
133
134
135
# File 'lib/elasticity/index_mapper.rb', line 126

def bulk_update(documents)
  @strategy.bulk do |b|
    documents.each do |doc|
      b.update(
        doc[:_id],
        { doc: { doc[:attr_name] => doc[:attr_value] } }
      )
    end
  end
end

#create_indexObject

Creates the index for this document



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

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

#delete(id) ⇒ Object

Removes one specific document from the index.



107
108
109
# File 'lib/elasticity/index_mapper.rb', line 107

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

#delete_by_search(search) ⇒ Object

Removes entries based on a search



112
113
114
# File 'lib/elasticity/index_mapper.rb', line 112

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

#delete_indexObject

Deletes the index



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

def delete_index
  @strategy.delete
end

#flush_indexObject

Flushes the index, forcing any writes note that v7 no longer forces any writes on flush



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

def flush_index
  @strategy.flush
end

#get(id) ⇒ Object

Fetches one specific document from the index by ID.



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

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

#index_document(id, document_hash) ⇒ Object

Index the given document



86
87
88
# File 'lib/elasticity/index_mapper.rb', line 86

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

#index_exists?Boolean

Does the index exist?

Returns:

  • (Boolean)


58
59
60
# File 'lib/elasticity/index_mapper.rb', line 58

def index_exists?
  !@strategy.missing?
end

#map_hit(hit) ⇒ Object

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



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/elasticity/index_mapper.rb', line 147

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

  highlighted = nil

  if hit["highlight"]
    highlighted_attrs = hit["highlight"].each_with_object({}) do |(name, v), attrs|
      name = name.gsub(/\..*\z/, "")

      attrs[name] ||= v
    end

    highlighted = @document_klass.new(attrs.merge(highlighted_attrs))
  end

  injected_attrs = attrs.merge({
    highlighted: highlighted,
    highlighted_attrs: highlighted_attrs.try(:keys),
    _explanation: hit["_explanation"]
  })

  if @document_klass.config.subclasses.present?
    @document_klass.config.subclasses[hit["_type"].to_sym].constantize.new(injected_attrs)
  else
    @document_klass.new(injected_attrs)
  end
end

#recreate_indexObject

Re-creates the index for this document



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

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.



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

def ref_index_name
  @strategy.ref_index_name
end

#refresh_indexObject

Resfreshes the index, forcing any writes



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

def refresh_index
  @strategy.refresh
end

#remap!(retry_delete_on_recoverable_errors: true, retry_delay: 30, max_delay: 600) ⇒ Object

Remap retry_delay & max_delay are in seconds



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

def remap!(retry_delete_on_recoverable_errors: true, retry_delay: 30, max_delay: 600)
  @strategy.remap(@index_config.definition, retry_delete_on_recoverable_errors: retry_delete_on_recoverable_errors, retry_delay: retry_delay, max_delay: max_delay)
end

#search(body, search_args = {}) ⇒ Object

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

explain: boolean to specify we should request _explanation of the query


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

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