Class: Mongoid::FTS::Results
- Inherits:
-
Array
- Object
- Array
- Mongoid::FTS::Results
- Defined in:
- lib/mongoid-fts/results.rb
Defined Under Namespace
Classes: Promise
Instance Attribute Summary collapse
-
#_models ⇒ Object
Returns the value of attribute _models.
-
#_searches ⇒ Object
Returns the value of attribute _searches.
Instance Method Summary collapse
-
#_denormalize! ⇒ Object
TODO - text sorting more…
-
#initialize(_searches) ⇒ Results
constructor
A new instance of Results.
- #num_pages ⇒ Object
- #page(*args) ⇒ Object (also: #current_page)
- #paginate(*args) ⇒ Object
- #per(*args) ⇒ Object
- #total_pages ⇒ Object
Constructor Details
#initialize(_searches) ⇒ Results
Returns a new instance of Results.
7 8 9 10 11 12 13 14 |
# File 'lib/mongoid-fts/results.rb', line 7 def initialize(_searches) @_searches = _searches @_models = [] _denormalize! @page = 1 @per = size @num_pages = 1 end |
Instance Attribute Details
#_models ⇒ Object
Returns the value of attribute _models.
5 6 7 |
# File 'lib/mongoid-fts/results.rb', line 5 def _models @_models end |
#_searches ⇒ Object
Returns the value of attribute _searches.
4 5 6 |
# File 'lib/mongoid-fts/results.rb', line 4 def _searches @_searches end |
Instance Method Details
#_denormalize! ⇒ Object
TODO - text sorting more…
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/mongoid-fts/results.rb', line 91 def _denormalize! # collection = self collection.clear @_models = [] return self if @_searches.empty? # _models = @_searches._models _position = proc do |model| _models.index(model) or raise("no position for #{ model.inspect }!?") end results = @_searches.map do |_search| _search['results'] ||= [] _search['results'].each do |result| result['_model'] = _search._model result['_position'] = _position[_search._model] end _search['results'] end results.flatten! results.compact! =begin results.sort! do |a, b| score = Float(b['score']) <=> Float(a['score']) case score when 0 a['_position'] <=> b['_position'] else score end end =end # batches = Hash.new{|h,k| h[k] = []} results.each do |entry| obj = entry['obj'] context_type, context_id = obj['context_type'], obj['context_id'] batches[context_type].push(context_id) end # models = FTS.find_in_batches(batches) # result_index = {} results.each do |result| context_type = result['obj']['context_type'].to_s context_id = result['obj']['context_id'].to_s key = [context_type, context_id] result_index[key] = result end # models.each do |model| context_type = model.class.name.to_s context_id = model.id.to_s key = [context_type, context_id] result = result_index[key] model['_fts_index'] = result end # models.sort! do |model_a, model_b| a = model_a['_fts_index'] b = model_b['_fts_index'] score = Float(b['score']) <=> Float(a['score']) case score when 0 a['_position'] <=> b['_position'] else score end end # limit = @_searches._limit # replace(@_models = models[0 ... limit]) self end |
#num_pages ⇒ Object
81 82 83 |
# File 'lib/mongoid-fts/results.rb', line 81 def num_pages @num_pages end |
#page(*args) ⇒ Object Also known as: current_page
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/mongoid-fts/results.rb', line 57 def page(*args) if args.empty? return @page else = Map.(args) page = args.shift || [:page] [:page] = page paginate() end end |
#paginate(*args) ⇒ Object
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 |
# File 'lib/mongoid-fts/results.rb', line 16 def paginate(*args) results = self = Map.(args) page = Integer(args.shift || [:page] || @page) per = args.shift || [:per] || [:size] if per.nil? return Promise.new(results, page) else per = Integer(per) end @page = [page.abs, 1].max @per = [per.abs, 1].max @num_pages = (size.to_f / @per).ceil offset = (@page - 1) * @per length = @per slice = Array(@_models[offset, length]) replace(slice) self end |
#per(*args) ⇒ Object
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/mongoid-fts/results.rb', line 70 def per(*args) if args.empty? return @per else = Map.(args) per = args.shift || [:per] [:per] = per paginate() end end |
#total_pages ⇒ Object
85 86 87 |
# File 'lib/mongoid-fts/results.rb', line 85 def total_pages num_pages end |