34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/mongomapper_ext/filter.rb', line 34
def filter(query, opts = {})
stemmer = nil
original_words = Set.new(query.downcase.split)
language = opts.delete(:language) || 'en'
stemmed = []
if defined?(Lingua)
stemmer = MongoMapperExt::Filter.build_stemmer(language)
original_words.each do |word|
stemmed_word = stemmer.stem(word)
stemmed << stemmed_word unless original_words.include?(stemmed_word)
end
end
regex = (original_words+stemmed).map do |k|
/^#{Regexp.escape(k)}/
end
min_score = opts.delete(:min_score) || 0.0
limit = opts.delete(:per_page) || 25
page = opts.delete(:page) || 1
select = opts.delete(:select) || self.keys.keys
query = Plucky::Query.new(self.collection, opts)
results = self.database.eval("function(collection, q, config) { return filter(collection, q, config); }", self.collection_name, query.criteria.source.merge({"_keywords" => {:$in => regex}}), {:words => original_words.to_a, :stemmed => stemmed.to_a, :limit => limit, :min_score => min_score, :select => select })
= Paginator.new(results["total_entries"], page, limit)
.subject = results['results'].map do |result|
item = self.new(result['doc'])
item.search_score = result['score']
item
end
end
|