Class: Mongoid::FTS::Index

Inherits:
Object
  • Object
show all
Includes:
Document
Defined in:
lib/mongoid-fts.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add(*args, &block) ⇒ Object



460
461
462
463
464
465
466
# File 'lib/mongoid-fts.rb', line 460

def Index.add(*args, &block)
  begin
    add!(*args, &block)
  rescue Object
    false
  end
end

.add!(model) ⇒ Object



397
398
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
# File 'lib/mongoid-fts.rb', line 397

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



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/mongoid-fts.rb', line 381

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



487
488
489
490
491
492
493
# File 'lib/mongoid-fts.rb', line 487

def Index.remove(*args, &block)
  begin
    remove!(*args, &block)
  rescue Object
    false
  end
end

.remove!(*args, &block) ⇒ Object



468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
# File 'lib/mongoid-fts.rb', line 468

def Index.remove!(*args, &block)
  options = args.extract_options!.to_options!
  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



376
377
378
379
# File 'lib/mongoid-fts.rb', line 376

def Index.reset!
  teardown!
  setup!
end

.setup!Object



372
373
374
# File 'lib/mongoid-fts.rb', line 372

def Index.setup!
  Index.create_indexes
end

.teardown!Object



367
368
369
370
# File 'lib/mongoid-fts.rb', line 367

def Index.teardown!
  Index.remove_indexes
  Index.destroy_all
end

.to_search(model) ⇒ Object



495
496
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
# File 'lib/mongoid-fts.rb', line 495

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



363
364
365
# File 'lib/mongoid-fts.rb', line 363

def inspect(*args, &block)
  Map.for(as_document).inspect(*args, &block)
end

#normalizeObject



326
327
328
329
330
# File 'lib/mongoid-fts.rb', line 326

def normalize
  if !defined?(@normalized) or !@normalized
    normalize!
  end
end

#normalize!Object



332
333
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
# File 'lib/mongoid-fts.rb', line 332

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