Class: Mongoid::FTS::Results
- Inherits:
-
Array
- Object
- Array
- Mongoid::FTS::Results
- Defined in:
- lib/mongoid-fts.rb
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
- #paginate(*args) ⇒ Object
- #per(*args) ⇒ Object
- #total_pages ⇒ Object
Constructor Details
#initialize(_searches) ⇒ Results
Returns a new instance of Results.
127 128 129 130 131 132 133 |
# File 'lib/mongoid-fts.rb', line 127 def initialize(_searches) @_searches = _searches @_models = [] _denormalize! @page = 1 @per = size end |
Instance Attribute Details
#_models ⇒ Object
Returns the value of attribute _models.
125 126 127 |
# File 'lib/mongoid-fts.rb', line 125 def _models @_models end |
#_searches ⇒ Object
Returns the value of attribute _searches.
124 125 126 |
# File 'lib/mongoid-fts.rb', line 124 def _searches @_searches end |
Instance Method Details
#_denormalize! ⇒ Object
TODO - text sorting more…
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/mongoid-fts.rb', line 186 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! results.sort! do |a, b| score = Float(b['score']) <=> Float(a['score']) case score when 0 a['_position'] <=> b['_position'] else score 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) # limit = @_searches._limit # replace(@_models = models[0 ... limit]) self end |
#num_pages ⇒ Object
176 177 178 |
# File 'lib/mongoid-fts.rb', line 176 def num_pages (size.to_f / per).ceil end |
#page(*args) ⇒ Object
154 155 156 157 158 159 160 161 162 163 |
# File 'lib/mongoid-fts.rb', line 154 def page(*args) if args.empty? return @page else = Map.(args) page = args.shift || [:page] [:page] = page paginate() end end |
#paginate(*args) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/mongoid-fts.rb', line 135 def paginate(*args) = Map.(args) page = Integer(args.shift || [:page] || @page) per = Integer(args.shift || [:per] || [:size] || @per) @page = [page.abs, 1].max @per = [per.abs, 1].max offset = (@page - 1) * @per length = @per slice = Array(@_models[offset, length]) replace(slice) self end |
#per(*args) ⇒ Object
165 166 167 168 169 170 171 172 173 174 |
# File 'lib/mongoid-fts.rb', line 165 def per(*args) if args.empty? return @per else = Map.(args) per = args.shift || [:per] [:per] = per paginate() end end |
#total_pages ⇒ Object
180 181 182 |
# File 'lib/mongoid-fts.rb', line 180 def total_pages num_pages end |