# File lib/tire/results/collection.rb, line 10 def initialize(response, options={}) @response = response @options = options @time = response['took'].to_i @total = response['hits']['total'].to_i @facets = response['facets'] @wrapper = options[:wrapper] || Configuration.wrapper end
# File lib/tire/results/collection.rb, line 78 def [](index) results[index] end
Handles _source prefixed fields properly: strips the prefix and converts fields to nested Hashes
# File lib/tire/results/collection.rb, line 88 def __parse_fields__(fields={}) ( fields ||= {} ).clone.each_pair do |key,value| next unless key.to_s =~ %r_source/ # Skip regular JSON immediately keys = key.to_s.split('.').reject { |n| n == '_source' } fields.delete(key) result = {} path = [] keys.each do |name| path << name eval "result[:#{path.join('][:')}] ||= {}" eval "result[:#{path.join('][:')}] = #{value.inspect}" if keys.last == name end fields.update result end fields end
# File lib/tire/results/collection.rb, line 65 def each(&block) results.each(&block) end
# File lib/tire/results/collection.rb, line 69 def empty? results.empty? end
# File lib/tire/results/collection.rb, line 19 def results @results ||= begin hits = @response['hits']['hits'] unless @options[:load] if @wrapper == Hash hits else hits.map do |h| document = {} # Update the document with content and ID document = h['_source'] ? document.update( h['_source'] || {} ) : document.update( __parse_fields__(h['fields']) ) document.update( {'id' => h['_id']} ) # Update the document with meta information ['_score', '_type', '_index', '_version', 'sort', 'highlight'].each { |key| document.update( {key => h[key]} || {} ) } # Return an instance of the "wrapper" class @wrapper.new(document) end end else return [] if hits.empty? type = @response['hits']['hits'].first['_type'] raise NoMethodError, "You have tried to eager load the model instances, " + "but Tire cannot find the model class because " + "document has no _type property." unless type begin klass = type.camelize.constantize rescue NameError => e raise NameError, "You have tried to eager load the model instances, but " + "Tire cannot find the model class '#{type.camelize}' " + "based on _type '#{type}'.", e.backtrace end ids = @response['hits']['hits'].map { |h| h['_id'] } records = @options[:load] === true ? klass.find(ids) : klass.find(ids, @options[:load]) # Reorder records to preserve order from search results ids.map { |id| records.detect { |record| record.id.to_s == id.to_s } } end end end
# File lib/tire/results/collection.rb, line 73 def size results.size end
# File lib/tire/results/collection.rb, line 82 def to_ary self end