Module: Mongoid::FTS

Defined in:
lib/mongoid-fts.rb

Defined Under Namespace

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

Class Method Summary collapse

Class Method Details

._search(*args) ⇒ Object



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
107
108
# File 'lib/mongoid-fts.rb', line 70

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



545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
# File 'lib/mongoid-fts.rb', line 545

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



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

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



495
496
497
498
499
# File 'lib/mongoid-fts.rb', line 495

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

.indexObject



440
441
442
# File 'lib/mongoid-fts.rb', line 440

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



506
507
508
# File 'lib/mongoid-fts.rb', line 506

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



502
503
504
# File 'lib/mongoid-fts.rb', line 502

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

.search(*args) ⇒ Object



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

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

  _searches = FTS._search(*args)

  Results.new(_searches)
end

.sessionObject



510
511
512
# File 'lib/mongoid-fts.rb', line 510

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

.session=(session) ⇒ Object



514
515
516
# File 'lib/mongoid-fts.rb', line 514

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

.versionObject



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

def version
  const_get :Version
end