Module: Mongoid::FTS

Defined in:
lib/mongoid-fts.rb

Defined Under Namespace

Modules: Mixin Classes: Engine, Index, Raw, Results

Class Method Summary collapse

Class Method Details

._search(*args) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/mongoid-fts.rb', line 68

def FTS._search(*args)
  options = Map.options_for!(args)

  search = args.join(' ')

  text   = options.delete(:text) || Index.default_collection_name.to_s
  limit  = [Integer(options.delete(:limit) || 128), 1].max
  models = [options.delete(:models), options.delete(:model)].flatten.compact

  models = FTS.models if models.empty?

  _searches =
    models.map do |model|
      context_type = model.name.to_s
      
      cmd = Hash.new

      cmd[:text] ||= text

      cmd[:limit] ||= limit

      (cmd[:search] ||= '') << search

      cmd[:project] ||= {'_id' => 1, 'context_type' => 1, 'context_id' => 1}

      cmd[:filter] ||= {'context_type' => context_type}

      options.each do |key, value|
        cmd[key] = value
      end

      Map.for(session.command(cmd)).tap do |_search|
        _search[:_model] = model
        _search[:_cmd] = cmd
      end
    end

  Raw.new(_searches, :_search => search, :_text => text, :_limit => limit, :_models => models)
end

.dependenciesObject



11
12
13
14
15
16
17
# File 'lib/mongoid-fts.rb', line 11

def dependencies
  {
    'mongoid'       => [ 'mongoid'       , '~> 3.1' ] ,
    'map'           => [ 'map'           , '~> 6.5' ] ,
    'coerce'        => [ 'coerce'        , '~> 0.0' ] ,
  }
end

.enable!(*args) ⇒ Object



518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
# File 'lib/mongoid-fts.rb', line 518

def FTS.enable!(*args)
  options = Map.options_for!(args)

  unless options.has_key?(:warn)
    options[:warn] = true
  end

  begin
    session = Mongoid::Sessions.default
    session.with(database: :admin).command({ setParameter: 1, textSearchEnabled: true })
  rescue Object => e
    unless e.is_a?(Mongoid::Errors::NoSessionsConfig)
      warn "failed to enable search with #{ e.class }(#{ e.message })"
    end
  end
end

.find_in_batches(queries = {}) ⇒ Object



491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
# File 'lib/mongoid-fts.rb', line 491

def FTS.find_in_batches(queries = {})
  models =
    queries.map do |model_class, model_ids|
      unless model_class.is_a?(Class)
        model_class = eval(model_class.to_s)
      end

      model_ids = Array(model_ids)

      begin
        model_class.find(model_ids)
      rescue Mongoid::Errors::DocumentNotFound
        model_ids.map do |model_id|
          begin
            model_class.find(model_id)
          rescue Mongoid::Errors::DocumentNotFound
            nil
          end
        end
      end
    end

  models.flatten!
  models.compact!
  models
end

.included(other) ⇒ Object



468
469
470
471
472
# File 'lib/mongoid-fts.rb', line 468

def FTS.included(other)
  unless other.is_a?(FTS::Mixin)
    other.send(:include, FTS::Mixin)
  end
end

.indexObject



419
420
421
# File 'lib/mongoid-fts.rb', line 419

def FTS.index
  Index
end

.libdir(*args, &block) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mongoid-fts.rb', line 19

def libdir(*args, &block)
  @libdir ||= File.expand_path(__FILE__).sub(/\.rb$/,'')
  args.empty? ? @libdir : File.join(@libdir, *args)
ensure
  if block
    begin
      $LOAD_PATH.unshift(@libdir)
      block.call()
    ensure
      $LOAD_PATH.shift()
    end
  end
end

.list_of_strings(*args) ⇒ Object



479
480
481
# File 'lib/mongoid-fts.rb', line 479

def FTS.list_of_strings(*args)
  args.flatten.compact.map{|arg| arg.to_s}.select{|arg| !arg.empty?}.uniq
end

.load(*libs) ⇒ Object



33
34
35
36
# File 'lib/mongoid-fts.rb', line 33

def load(*libs)
  libs = libs.join(' ').scan(/[^\s+]+/)
  libdir{ libs.each{|lib| Kernel.load(lib) } }
end

.modelsObject



475
476
477
# File 'lib/mongoid-fts.rb', line 475

def FTS.models
  @models ||= []
end

.search(*args) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/mongoid-fts.rb', line 60

def FTS.search(*args)
  options = Map.options_for(args)

  _searches = FTS._search(*args)

  Results.new(_searches)
end

.sessionObject



483
484
485
# File 'lib/mongoid-fts.rb', line 483

def FTS.session
  @session ||= Mongoid::Sessions.default
end

.session=(session) ⇒ Object



487
488
489
# File 'lib/mongoid-fts.rb', line 487

def FTS.session=(session)
  @session = session
end

.versionObject



7
8
9
# File 'lib/mongoid-fts.rb', line 7

def version
  const_get :Version
end