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.



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

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
25
# 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_update,
    :bulk_delete,
    :map_hit,
    to: to
  )
end

Instance Method Details

#bulk_delete(ids) ⇒ Object

Bulk delete documents matching provided ids



132
133
134
135
136
137
138
# File 'lib/elasticity/index_mapper.rb', line 132

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



110
111
112
113
114
115
116
# File 'lib/elasticity/index_mapper.rb', line 110

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

#bulk_update(documents) ⇒ Object

Bulk update the specicied attribute of the provided documents



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/elasticity/index_mapper.rb', line 119

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

#create_indexObject

Creates the index for this document



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

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

#delete(id) ⇒ Object

Removes one specific document from the index.



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

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

#delete_by_search(search) ⇒ Object

Removes entries based on a search



105
106
107
# File 'lib/elasticity/index_mapper.rb', line 105

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

#delete_indexObject

Deletes the index



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

def delete_index
  @strategy.delete
end

#flush_indexObject

Flushes the index, forcing any writes



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

def flush_index
  @strategy.flush
end

#get(id) ⇒ Object

Fetches one specific document from the index by ID.



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

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



79
80
81
# File 'lib/elasticity/index_mapper.rb', line 79

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

#index_exists?Boolean

Does the index exist?

Returns:

  • (Boolean)


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

def index_exists?
  !@strategy.missing?
end

#map_hit(hit) ⇒ Object

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



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/elasticity/index_mapper.rb', line 141

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"]

  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



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

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.



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

def ref_index_name
  @strategy.ref_index_name
end

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

Remap retry_delay & max_delay are in seconds



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

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


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

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