mongomapper_ext 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.2
1
+ 0.2.3
@@ -14,6 +14,8 @@ module MongoMapperExt
14
14
  ensure_index :_keywords
15
15
 
16
16
  before_save :_update_keywords
17
+
18
+ attr_accessor :search_score
17
19
  end
18
20
  end
19
21
 
@@ -47,38 +49,25 @@ module MongoMapperExt
47
49
  /^#{Regexp.escape(k)}/
48
50
  end
49
51
 
50
- if opts[:per_page]
51
- result = self.paginate(opts.deep_merge(:conditions => {:_keywords => regex }))
52
- pagination = MongoMapper::Plugins::Pagination::Proxy.new(result.total_entries, result.current_page, result.per_page)
52
+ min_score = opts.delete(:min_score) || 0.0
53
+ limit = opts.delete(:per_page) || 25
54
+ page = opts.delete(:page) || 1
55
+ select = opts.delete(:select) || self.keys.keys
53
56
 
54
- pagination.subject = result.sort_by do |e|
55
- evaluate_result(e._keywords, original_words, stemmed) * -1
56
- end
57
+ criteria, options = to_query(opts)
57
58
 
58
- pagination
59
- else
60
- self.all(opts.deep_merge(:conditions => {:_keywords => regex })).sort_by do |e|
61
- evaluate_result(e._keywords, original_words, stemmed)
62
- end
63
- end
64
- end
59
+ results = self.database.eval("function(collection, q, config) { return filter(collection, q, config); }", self.collection_name, criteria.merge({"_keywords" => {:$in => regex}}), {:words => original_words.to_a, :stemmed => stemmed.to_a, :limit => limit, :min_score => min_score, :select => select })
65
60
 
66
- private
67
- def evaluate_result(keywords, original_words, stemmed)
68
- score = 0.0
69
- original_words.each do |word|
70
- if keywords.include?(word)
71
- score += 15.0
72
- end
73
- end
61
+ pagination = MongoMapper::Plugins::Pagination::Proxy.new(results["total_entries"], page, limit)
74
62
 
75
- stemmed.each do |word|
76
- if keywords.include?(word)
77
- score += 1.0 + word.length
78
- end
63
+ pagination.subject = results['results'].map do |result|
64
+ item = self.new(result['doc'])
65
+ item.search_score = result['score']
66
+
67
+ item
79
68
  end
80
69
 
81
- score
70
+ pagination
82
71
  end
83
72
  end
84
73
 
@@ -0,0 +1,41 @@
1
+ function filter(collection, q, config) {
2
+ var results = [];
3
+ var counter = 0;
4
+
5
+ var fields = {_keywords: 1};
6
+ for(var i in config.select) {
7
+ fields[config.select[i]] = 1;
8
+ }
9
+
10
+ var time = new Date().getTime();
11
+ db[collection].find(q, fields).limit(500).forEach(
12
+ function(doc) {
13
+ var score = 0.0;
14
+ for(var i in config.words) {
15
+ var word = config.words[i];
16
+ if(doc._keywords.indexOf(word) != -1 )
17
+ score += 15.0;
18
+ }
19
+
20
+ for(var i in config.stemmed) {
21
+ var word = config.stemmed[i];
22
+ if(doc._keywords.indexOf(word) != -1 )
23
+ score += (1.0 + word.length);
24
+ }
25
+
26
+ if(score >= config.min_score || 1.0 ) {
27
+ delete doc._keywords;
28
+ results.push({'score': score, 'doc': doc});
29
+ counter += 1;
30
+ }
31
+ }
32
+ );
33
+
34
+ var sorted = results.sort(function(a,b) {
35
+ return b.score - a.score;
36
+ });
37
+
38
+ time = (new Date().getTime() - time);
39
+
40
+ return {total_entries: counter, elapsed_time: time, results: sorted.slice(0, config.limit||500)};
41
+ }
@@ -18,6 +18,7 @@ end
18
18
  # types
19
19
  require 'mongomapper_ext/types/open_struct'
20
20
  require 'mongomapper_ext/types/timestamp'
21
+ require 'mongomapper_ext/types/translation'
21
22
 
22
23
  # storage
23
24
  require 'mongomapper_ext/file_list'
@@ -51,3 +52,4 @@ module MongoMapperExt
51
52
  end
52
53
  end
53
54
  end
55
+
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongomapper_ext}
8
- s.version = "0.2.2"
8
+ s.version = "0.2.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["David A. Cuadrado"]
12
- s.date = %q{2010-04-20}
12
+ s.date = %q{2010-04-25}
13
13
  s.description = %q{MongoMapper extensions}
14
14
  s.email = %q{krawek@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -35,6 +35,7 @@ Gem::Specification.new do |s|
35
35
  "lib/mongomapper_ext/file.rb",
36
36
  "lib/mongomapper_ext/file_list.rb",
37
37
  "lib/mongomapper_ext/filter.rb",
38
+ "lib/mongomapper_ext/js/filter.js",
38
39
  "lib/mongomapper_ext/js/find_tags.js",
39
40
  "lib/mongomapper_ext/js/tag_cloud.js",
40
41
  "lib/mongomapper_ext/slugizer.rb",
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 2
9
- version: 0.2.2
8
+ - 3
9
+ version: 0.2.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - David A. Cuadrado
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-20 00:00:00 -05:00
17
+ date: 2010-04-25 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -115,6 +115,7 @@ files:
115
115
  - lib/mongomapper_ext/file.rb
116
116
  - lib/mongomapper_ext/file_list.rb
117
117
  - lib/mongomapper_ext/filter.rb
118
+ - lib/mongomapper_ext/js/filter.js
118
119
  - lib/mongomapper_ext/js/find_tags.js
119
120
  - lib/mongomapper_ext/js/tag_cloud.js
120
121
  - lib/mongomapper_ext/slugizer.rb