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



721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
# File 'lib/mongoid-fts.rb', line 721

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



694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
# File 'lib/mongoid-fts.rb', line 694

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



671
672
673
674
675
# File 'lib/mongoid-fts.rb', line 671

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

.index(*args, &block) ⇒ Object



595
596
597
598
599
600
601
# File 'lib/mongoid-fts.rb', line 595

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

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



607
608
609
# File 'lib/mongoid-fts.rb', line 607

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



682
683
684
# File 'lib/mongoid-fts.rb', line 682

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

.literals_for(*words) ⇒ Object



580
581
582
583
584
585
586
587
588
589
590
591
592
593
# File 'lib/mongoid-fts.rb', line 580

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



678
679
680
# File 'lib/mongoid-fts.rb', line 678

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

.normalized_array(*array) ⇒ Object



576
577
578
# File 'lib/mongoid-fts.rb', line 576

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



686
687
688
# File 'lib/mongoid-fts.rb', line 686

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

.session=(session) ⇒ Object



690
691
692
# File 'lib/mongoid-fts.rb', line 690

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

.unindex(*args, &block) ⇒ Object



603
604
605
# File 'lib/mongoid-fts.rb', line 603

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

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



611
612
613
# File 'lib/mongoid-fts.rb', line 611

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