Class: ElasticMapper::MultiSearch
- Inherits:
-
Object
- Object
- ElasticMapper::MultiSearch
- Defined in:
- lib/elastic_mapper/multi_search.rb
Overview
search across multiple models at the same time. NO WAY!
Instance Method Summary collapse
-
#find(ids_hash) ⇒ Object
receives queries in the form: { id: “#objobj.<em>type#objobj.id”, obj_id: obj.id, type: obj._type }.
-
#initialize(obj_map) ⇒ MultiSearch
constructor
takes a hash lookup which maps from a mapping name on to an ActiveRecord model.
Constructor Details
#initialize(obj_map) ⇒ MultiSearch
takes a hash lookup which maps from a mapping name on to an ActiveRecord model.
7 8 9 10 11 |
# File 'lib/elastic_mapper/multi_search.rb', line 7 def initialize(obj_map) @obj_map = obj_map # used to create classes from mappings. @_mapping_name = @obj_map.keys.join(',') self.extend(ElasticMapper::Search::ClassMethods) end |
Instance Method Details
#find(ids_hash) ⇒ Object
receives queries in the form:
id: "#{obj._type_#ElasticMapper::MultiSearch.objobj.id",
obj_id: obj.id,
type: obj._type
}
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/elastic_mapper/multi_search.rb', line 19 def find(ids_hash) results = {} # 1. iterate over each active model we care about. # 2. perform a bulk lookup based on id. # 3. map everything onto results hash, so that we can maintain sort order. @obj_map.keys.each do |key| ids = ids_hash.select {|id_hash| id_hash[:type] == key.to_s}.map {|obj| obj[:obj_id]} next unless ids.count > 0 @obj_map[key].find(ids).each do |record| results["#{key}_#{record.id}"] = record end end results end |