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
|
.dependencies ⇒ Object
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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
|
# File 'lib/mongoid-fts.rb', line 698
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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
|
# File 'lib/mongoid-fts.rb', line 671
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
648
649
650
651
652
|
# File 'lib/mongoid-fts.rb', line 648
def FTS.included(other)
unless other.is_a?(FTS::Mixin)
other.send(:include, FTS::Mixin)
end
end
|
.index(*args, &block) ⇒ Object
572
573
574
575
576
577
578
|
# File 'lib/mongoid-fts.rb', line 572
def FTS.index(*args, &block)
if args.empty? and block.nil?
Index
else
Index.add(*args, &block)
end
end
|
.index!(*args, &block) ⇒ Object
584
585
586
|
# File 'lib/mongoid-fts.rb', line 584
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
659
660
661
|
# File 'lib/mongoid-fts.rb', line 659
def FTS.list_of_strings(*args)
args.flatten.compact.map{|arg| arg.to_s}.select{|arg| !arg.empty?}.uniq
end
|
.literals_for(*words) ⇒ Object
557
558
559
560
561
562
563
564
565
566
567
568
569
570
|
# File 'lib/mongoid-fts.rb', line 557
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
|
.models ⇒ Object
655
656
657
|
# File 'lib/mongoid-fts.rb', line 655
def FTS.models
@models ||= []
end
|
.normalized_array(*array) ⇒ Object
553
554
555
|
# File 'lib/mongoid-fts.rb', line 553
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
|
.session ⇒ Object
663
664
665
|
# File 'lib/mongoid-fts.rb', line 663
def FTS.session
@session ||= Mongoid::Sessions.default
end
|
.session=(session) ⇒ Object
667
668
669
|
# File 'lib/mongoid-fts.rb', line 667
def FTS.session=(session)
@session = session
end
|
.unindex(*args, &block) ⇒ Object
580
581
582
|
# File 'lib/mongoid-fts.rb', line 580
def FTS.unindex(*args, &block)
Index.remove(*args, &block)
end
|
.unindex!(*args, &block) ⇒ Object
588
589
590
|
# File 'lib/mongoid-fts.rb', line 588
def FTS.unindex!(*args, &block)
Index.remove!(*args, &block)
end
|
.version ⇒ Object
7
8
9
|
# File 'lib/mongoid-fts.rb', line 7
def version
const_get :Version
end
|