Method: Mebla::ResultSet#initialize
- Defined in:
- lib/mebla/result_set.rb
#initialize(response) ⇒ ResultSet
Creates a new result set from an elasticsearch response hash
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/mebla/result_set.rb', line 14 def initialize(response) # Keep the query time @time = response['took'] # Keep the facets @facets = response['facets'] # Keep the query total to check against the count @total = response['hits']['total'] # Be efficient only query the database once model_ids = [] # Collect results' ids response['hits']['hits'].each do |hit| model_class = hit['_type'].camelize.constantize model_ids << [model_class] if model_class. model_class_collection = model_ids.assoc(model_class) # collect parent ids # [class, [parent_id, ids]] parent_id = hit['_source']['_parent'] model_class_collection << [parent_id] model_class_collection.assoc(parent_id) << hit['_source']['id'] else # collect ids # [class, ids] model_ids.assoc(model_class) << hit['_source']['id'] end end # Cast the results into their appropriate classes @entries = [] model_ids.each do |model_class_collection| model_class = model_class_collection.first ids = model_class_collection.drop(1) unless model_class. # Retrieve the results from the database ids.each do |id| @entries << model_class.find(id) end else # Get the parent parent_class = model_class. access_method = model_class. ids.each do |parent_id_collection| parent_id = parent_id_collection.first entries_ids = parent_id_collection.drop(1) parent = parent_class.find parent_id # Retrieve the results from the database entries_ids.each do |entry_id| @entries << parent.send(access_method.to_sym).find(entry_id) end end end end Mebla.log("WARNING: Index not synchronized with the database; index total hits: #{@total}, retrieved documents: #{self.count}", :warn) if @total != self.count end |