Class: Mongoid::FTS::Index
- Inherits:
-
Object
- Object
- Mongoid::FTS::Index
- Includes:
- Document
- Defined in:
- lib/mongoid-fts.rb
Class Method Summary collapse
- .add(model) ⇒ Object
- .rebuild! ⇒ Object
- .remove(model) ⇒ Object
- .reset! ⇒ Object
- .setup! ⇒ Object
- .teardown! ⇒ Object
- .to_search(model) ⇒ Object
Instance Method Summary collapse
Class Method Details
.add(model) ⇒ Object
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 |
# File 'lib/mongoid-fts.rb', line 345 def Index.add(model) to_search = Index.to_search(model) title = to_search.has_key?(:title) ? Coerce.string(to_search[:title]) : nil keywords = to_search.has_key?(:keywords) ? Coerce.list_of_strings(to_search[:keywords]) : nil fulltext = to_search.has_key?(:fulltext) ? Coerce.string(to_search[:fulltext]) : nil context_type = model.class.name.to_s context_id = model.id conditions = { :context_type => context_type, :context_id => context_id } attributes = { :title => title, :keywords => keywords, :fulltext => fulltext } begin new(conditions).upsert rescue Object => e warn "#{ e. } (#{ e.class })" 4.times do begin break if create(conditions) rescue Object => e warn "#{ e. } (#{ e.class })" nil end end end # FIXME - go BOOM here if none found... # index = where(conditions).first if index index.update_attributes(attributes) else raise Error.new("failed to create index for #{ conditions.inspect }") end end |
.rebuild! ⇒ Object
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
# File 'lib/mongoid-fts.rb', line 329 def Index.rebuild! batches = Hash.new{|h,k| h[k] = []} each do |index| context_type, context_id = index.context_type, index.context_id next unless context_type && context_id (batches[context_type] ||= []).push(context_id) end models = FTS.find_in_batches(batches) reset! models.each{|model| add(model)} end |
.remove(model) ⇒ Object
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
# File 'lib/mongoid-fts.rb', line 392 def Index.remove(model) context_type = model.class.name.to_s context_id = model.id conditions = { :context_type => context_type, :context_id => context_id } where(conditions).first.tap do |index| if index index.destroy rescue nil end end end |
.reset! ⇒ Object
324 325 326 327 |
# File 'lib/mongoid-fts.rb', line 324 def Index.reset! teardown! setup! end |
.setup! ⇒ Object
320 321 322 |
# File 'lib/mongoid-fts.rb', line 320 def Index.setup! Index.create_indexes end |
.teardown! ⇒ Object
315 316 317 318 |
# File 'lib/mongoid-fts.rb', line 315 def Index.teardown! Index.remove_indexes Index.destroy_all end |
.to_search(model) ⇒ Object
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 |
# File 'lib/mongoid-fts.rb', line 408 def Index.to_search(model) to_search = nil if model.respond_to?(:to_search) to_search = Map.for(model.to_search) else to_search = Map.new to_search[:title] = %w( title ).map do |attr| model.send(attr) if model.respond_to?(attr) end.compact.join(' ') to_search[:keywords] = %w( keywords tags ).map do |attr| model.send(attr) if model.respond_to?(attr) end.compact to_search[:fulltext] = %w( fulltext text content body description ).map do |attr| model.send(attr) if model.respond_to?(attr) end.compact.join(' ') end unless %w( title keywords fulltext ).detect{|key| to_search.has_key?(key)} raise ArgumentError, "you need to define #{ model }#to_search" end to_search end |
Instance Method Details
#normalize ⇒ Object
286 287 288 289 290 |
# File 'lib/mongoid-fts.rb', line 286 def normalize if !defined?(@normalized) or !@normalized normalize! end end |
#normalize! ⇒ Object
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/mongoid-fts.rb', line 292 def normalize! index = self unless [index.keywords].join.strip.empty? index.keywords = FTS.list_of_strings(index.keywords) end unless [index.title].join.strip.empty? index.title = index.title.to_s.strip end unless [index.keywords].join.strip.empty? index.keywords = index.keywords.map{|keyword| keyword.strip} end unless [index.fulltext].join.strip.empty? index.fulltext = index.fulltext.to_s.strip end ensure @normalized = true end |