Class: Tire::Results::Collection
- Inherits:
-
Object
- Object
- Tire::Results::Collection
- Includes:
- Enumerable, Pagination
- Defined in:
- lib/tire/results/collection.rb
Instance Attribute Summary collapse
-
#facets ⇒ Object
readonly
Returns the value of attribute facets.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#time ⇒ Object
readonly
Returns the value of attribute time.
-
#total ⇒ Object
readonly
Returns the value of attribute total.
Instance Method Summary collapse
- #[](index) ⇒ Object
-
#__parse_fields__(fields = {}) ⇒ Object
Handles _source prefixed fields properly: strips the prefix and converts fields to nested Hashes.
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(response, options = {}) ⇒ Collection
constructor
A new instance of Collection.
- #results ⇒ Object
- #size ⇒ Object (also: #length)
- #to_ary ⇒ Object
Methods included from Pagination
#current_page, #next_page, #offset, #out_of_bounds?, #per_page, #previous_page, #total_entries, #total_pages
Constructor Details
#initialize(response, options = {}) ⇒ Collection
Returns a new instance of Collection.
10 11 12 13 14 15 16 17 |
# File 'lib/tire/results/collection.rb', line 10 def initialize(response, ={}) @response = response @options = @time = response['took'].to_i @total = response['hits']['total'].to_i @facets = response['facets'] @wrapper = [:wrapper] || Configuration.wrapper end |
Instance Attribute Details
#facets ⇒ Object (readonly)
Returns the value of attribute facets.
8 9 10 |
# File 'lib/tire/results/collection.rb', line 8 def facets @facets end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/tire/results/collection.rb', line 8 def @options end |
#time ⇒ Object (readonly)
Returns the value of attribute time.
8 9 10 |
# File 'lib/tire/results/collection.rb', line 8 def time @time end |
#total ⇒ Object (readonly)
Returns the value of attribute total.
8 9 10 |
# File 'lib/tire/results/collection.rb', line 8 def total @total end |
Instance Method Details
#[](index) ⇒ Object
81 82 83 |
# File 'lib/tire/results/collection.rb', line 81 def [](index) results[index] end |
#__parse_fields__(fields = {}) ⇒ Object
Handles _source prefixed fields properly: strips the prefix and converts fields to nested Hashes
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/tire/results/collection.rb', line 91 def __parse_fields__(fields={}) ( fields ||= {} ).clone.each_pair do |key,value| next unless key.to_s =~ /_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 |
#each(&block) ⇒ Object
68 69 70 |
# File 'lib/tire/results/collection.rb', line 68 def each(&block) results.each(&block) end |
#empty? ⇒ Boolean
72 73 74 |
# File 'lib/tire/results/collection.rb', line 72 def empty? results.empty? end |
#results ⇒ Object
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 |
# File 'lib/tire/results/collection.rb', line 19 def results @results ||= begin hits = @response['hits']['hits'].map { |d| d.update '_type' => Utils.unescape(d['_type']) } 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', '_explanation'].each { |key| document.update( {key => h[key]} || {} ) } # Return an instance of the "wrapper" class @wrapper.new(document) end end else return [] if hits.empty? records = {} @response['hits']['hits'].group_by { |item| item['_type'] }.each do |type, items| 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 = items.map { |h| h['_id'] } records[type] = @options[:load] === true ? klass.find(ids) : klass.find(ids, @options[:load]) end # Reorder records to preserve order from search results @response['hits']['hits'].map { |item| records[item['_type']].detect { |record| record.id.to_s == item['_id'].to_s } } end end end |
#size ⇒ Object Also known as: length
76 77 78 |
# File 'lib/tire/results/collection.rb', line 76 def size results.size end |
#to_ary ⇒ Object
85 86 87 |
# File 'lib/tire/results/collection.rb', line 85 def to_ary self end |