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
462 463 464 465 466 467 468 |
# File 'lib/mongoid-fts.rb', line 462 def Index.add(*args, &block) begin add!(*args, &block) rescue Object false end end |
.add!(model) ⇒ Object
399 400 401 402 403 404 405 406 407 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 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 |
# File 'lib/mongoid-fts.rb', line 399 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
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 |
# File 'lib/mongoid-fts.rb', line 383 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
489 490 491 492 493 494 495 |
# File 'lib/mongoid-fts.rb', line 489 def Index.remove(*args, &block) begin remove!(*args, &block) rescue Object false end end |
.remove!(*args, &block) ⇒ Object
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 |
# File 'lib/mongoid-fts.rb', line 470 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
378 379 380 381 |
# File 'lib/mongoid-fts.rb', line 378 def Index.reset! teardown! setup! end |
.setup! ⇒ Object
374 375 376 |
# File 'lib/mongoid-fts.rb', line 374 def Index.setup! Index.create_indexes end |
.teardown! ⇒ Object
369 370 371 372 |
# File 'lib/mongoid-fts.rb', line 369 def Index.teardown! Index.remove_indexes Index.destroy_all end |
.to_search(model) ⇒ Object
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 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 |
# File 'lib/mongoid-fts.rb', line 497 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
365 366 367 |
# File 'lib/mongoid-fts.rb', line 365 def inspect(*args, &block) Map.for(as_document).inspect(*args, &block) end |
#normalize ⇒ Object
328 329 330 331 332 |
# File 'lib/mongoid-fts.rb', line 328 def normalize if !defined?(@normalized) or !@normalized normalize! end end |
#normalize! ⇒ Object
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 |
# File 'lib/mongoid-fts.rb', line 334 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 |