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

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

  search   = args.join(' ')
  words    = search.strip.split(/\s+/)
  literals = FTS.literals_for(*words)
  search   = [literals, search].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



700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
# File 'lib/mongoid-fts.rb', line 700

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



673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
# File 'lib/mongoid-fts.rb', line 673

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



650
651
652
653
654
# File 'lib/mongoid-fts.rb', line 650

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

.index(*args, &block) ⇒ Object



574
575
576
577
578
579
580
# File 'lib/mongoid-fts.rb', line 574

def FTS.index(*args, &block)
  if args.empty? and block.nil?
    Index
  else
    Index.add(*args, &block)
  end
end

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



586
587
588
# File 'lib/mongoid-fts.rb', line 586

def FTS.index!(*args, &block)
  Index.add!(*args, &block)
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



661
662
663
# File 'lib/mongoid-fts.rb', line 661

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

.literals_for(*words) ⇒ Object



559
560
561
562
563
564
565
566
567
568
569
570
571
572
# File 'lib/mongoid-fts.rb', line 559

def FTS.literals_for(*words)
  words = words.join(' ').strip.split(/\s+/)

  words.map do |word|
    next if word.empty?
    if word =~ /\A__.*__\Z/
      word
    else
      without_delimeters = word.to_s.scan(/[0-9a-zA-Z]/).join
      literal = "__#{ without_delimeters }__"
      literal
    end
  end.compact
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



657
658
659
# File 'lib/mongoid-fts.rb', line 657

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

.normalized_array(*array) ⇒ Object



555
556
557
# File 'lib/mongoid-fts.rb', line 555

def FTS.normalized_array(*array)
  array.flatten.map{|_| _.to_s.strip}.select{|_| !_.empty?}
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



665
666
667
# File 'lib/mongoid-fts.rb', line 665

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

.session=(session) ⇒ Object



669
670
671
# File 'lib/mongoid-fts.rb', line 669

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

.unindex(*args, &block) ⇒ Object



582
583
584
# File 'lib/mongoid-fts.rb', line 582

def FTS.unindex(*args, &block)
  Index.remove(*args, &block)
end

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



590
591
592
# File 'lib/mongoid-fts.rb', line 590

def FTS.unindex!(*args, &block)
  Index.remove!(*args, &block)
end

.versionObject



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

def version
  const_get :Version
end