Class: Mongoid::FTS::Index
- Inherits:
-
Object
- Object
- Mongoid::FTS::Index
- Includes:
- Document
- Defined in:
- lib/mongoid-fts.rb
Class Method Summary collapse
- .add(*args, &block) ⇒ Object
- .add!(model) ⇒ Object
- .rebuild! ⇒ Object
- .remove(*args, &block) ⇒ Object
- .remove!(*args, &block) ⇒ Object
- .reset! ⇒ Object
- .setup! ⇒ Object
- .teardown! ⇒ Object
- .to_search(model) ⇒ Object
Instance Method Summary collapse
Class Method Details
.add(*args, &block) ⇒ Object
483 484 485 486 487 488 489 |
# File 'lib/mongoid-fts.rb', line 483 def Index.add(*args, &block) begin add!(*args, &block) rescue Object false end end |
.add!(model) ⇒ Object
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 |
# File 'lib/mongoid-fts.rb', line 420 def Index.add!(model) to_search = Index.to_search(model) literals = to_search.has_key?(:literals) ? Coerce.list_of_strings(to_search[:literals]) : nil title = to_search.has_key?(:title) ? Coerce.string(to_search[:title]) : nil literal_title = to_search.has_key?(:literal_title) ? Coerce.string(to_search[:literal_title]) : nil keywords = to_search.has_key?(:keywords) ? Coerce.list_of_strings(to_search[:keywords]) : nil literal_keywords = to_search.has_key?(:literal_keywords) ? Coerce.list_of_strings(to_search[:literal_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 = { :literals => literals, :title => title, :literal_title => literal_title, :keywords => keywords, :literal_keywords => literal_keywords, :fulltext => fulltext } index = nil n = 42 n.times do |i| index = where(conditions).first break if index begin index = create!(conditions) break if index rescue Object nil end sleep(rand) if i < (n - 1) end if index begin index.update_attributes!(attributes) rescue Object raise Error.new("failed to update index for #{ conditions.inspect }") end else raise Error.new("failed to create index for #{ conditions.inspect }") end index end |
.rebuild! ⇒ Object
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 |
# File 'lib/mongoid-fts.rb', line 404 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(*args, &block) ⇒ Object
510 511 512 513 514 515 516 |
# File 'lib/mongoid-fts.rb', line 510 def Index.remove(*args, &block) begin remove!(*args, &block) rescue Object false end end |
.remove!(*args, &block) ⇒ Object
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 |
# File 'lib/mongoid-fts.rb', line 491 def Index.remove!(*args, &block) = args.. models = args.flatten.compact model_ids = {} models.each do |model| model_name = model.class.name.to_s model_ids[model_name] ||= [] model_ids[model_name].push(model.id) end conditions = model_ids.map do |model_name, model_ids| {:context_type => model_name, :context_id.in => model_ids} end any_of(conditions).destroy_all end |
.reset! ⇒ Object
399 400 401 402 |
# File 'lib/mongoid-fts.rb', line 399 def Index.reset! teardown! setup! end |
.setup! ⇒ Object
395 396 397 |
# File 'lib/mongoid-fts.rb', line 395 def Index.setup! Index.create_indexes end |
.teardown! ⇒ Object
390 391 392 393 |
# File 'lib/mongoid-fts.rb', line 390 def Index.teardown! Index.remove_indexes Index.destroy_all end |
.to_search(model) ⇒ Object
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 |
# File 'lib/mongoid-fts.rb', line 518 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[:literals] = %w( id ).map do |attr| model.send(attr) if model.respond_to?(attr) end to_search[:title] = %w( title ).map do |attr| model.send(attr) if model.respond_to?(attr) end to_search[:keywords] = %w( keywords tags ).map do |attr| model.send(attr) if model.respond_to?(attr) end to_search[:fulltext] = %w( fulltext text content body description ).map do |attr| model.send(attr) if model.respond_to?(attr) end end # unless %w( literals title keywords fulltext ).detect{|key| to_search.has_key?(key)} raise ArgumentError, "you need to define #{ model }#to_search" end # literals = FTS.normalized_array(to_search[:literals]) title = FTS.normalized_array(to_search[:title]) keywords = FTS.normalized_array(to_search[:keywords]) fulltext = FTS.normalized_array(to_search[:fulltext]) # to_search[:literals] = FTS.literals_for(literals) to_search[:literal_title] = FTS.literals_for(title).join(' ').strip to_search[:title] = title.join(' ').strip to_search[:literal_keywords] = FTS.literals_for(keywords).join(' ').strip to_search[:keywords] = keywords.join(' ').strip to_search[:fulltext] = fulltext.join(' ').strip # to_search end |
Instance Method Details
#inspect(*args, &block) ⇒ Object
386 387 388 |
# File 'lib/mongoid-fts.rb', line 386 def inspect(*args, &block) Map.for(as_document).inspect(*args, &block) end |
#normalize ⇒ Object
349 350 351 352 353 |
# File 'lib/mongoid-fts.rb', line 349 def normalize if !defined?(@normalized) or !@normalized normalize! end end |
#normalize! ⇒ Object
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 |
# File 'lib/mongoid-fts.rb', line 355 def normalize! index = self unless [index.literals].join.strip.empty? index.literals = FTS.list_of_strings(index.literals) end unless [index.title].join.strip.empty? index.title = index.title.to_s.strip end unless [index.literal_title].join.strip.empty? index.literal_title = index.literal_title.to_s.strip end unless [index.keywords].join.strip.empty? index.keywords = FTS.list_of_strings(index.keywords) end unless [index.literal_keywords].join.strip.empty? index.literal_keywords = FTS.list_of_strings(index.literal_keywords) end unless [index.fulltext].join.strip.empty? index.fulltext = index.fulltext.to_s.strip end ensure @normalized = true end |