mongoid-haystack 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/article CHANGED
@@ -7,7 +7,7 @@ a good one allows modular methods to be composed into compound ones without
7
7
  fugly code contortions - keeping code nice and readable.
8
8
 
9
9
  although many people spend time considering carefully their method names and
10
- argument lists, few people stop to consider the role of the block on their
10
+ argument lists, but few people stop to consider the role of the block on their
11
11
  methods and how it affects reusability.
12
12
 
13
13
  that's unfortunate since fully groking the block is what unleashes the
@@ -2,7 +2,7 @@
2
2
  #
3
3
  module Mongoid
4
4
  module Haystack
5
- const_set(:Version, '1.3.0') unless const_defined?(:Version)
5
+ const_set(:Version, '1.4.0') unless const_defined?(:Version)
6
6
 
7
7
  class << Haystack
8
8
  def version
@@ -11,13 +11,14 @@
11
11
 
12
12
  def dependencies
13
13
  {
14
- 'mongoid' => [ 'mongoid' , '~> 3.0' ] ,
15
- 'moped' => [ 'moped' , '~> 1.3' ] ,
16
- 'origin' => [ 'origin' , '~> 1.0' ] ,
17
- 'map' => [ 'map' , '~> 6.2' ] ,
18
- 'fattr' => [ 'fattr' , '~> 2.2' ] ,
19
- 'coerce' => [ 'coerce' , '~> 0.0' ] ,
20
- 'unicode_utils' => [ 'unicode_utils' , '~> 1.4' ] ,
14
+ 'mongoid' => [ 'mongoid' , '~> 3.0' ] ,
15
+ 'moped' => [ 'moped' , '~> 1.3' ] ,
16
+ 'origin' => [ 'origin' , '~> 1.0' ] ,
17
+ 'map' => [ 'map' , '~> 6.2' ] ,
18
+ 'fattr' => [ 'fattr' , '~> 2.2' ] ,
19
+ 'coerce' => [ 'coerce' , '~> 0.0' ] ,
20
+ 'unicode_utils' => [ 'unicode_utils' , '~> 1.4' ] ,
21
+ 'threadify' => [ 'threadify' , '~> 1.3' ] ,
21
22
  }
22
23
  end
23
24
 
@@ -14,9 +14,21 @@ module Mongoid
14
14
  results = Haystack.search(*args, &block)
15
15
  end
16
16
 
17
- def search_index_all!
18
- all.each do |doc|
17
+ def search_index_all!(*args, &block)
18
+ options = Map.options_for!(args)
19
+ models = args.shift
20
+
21
+ unless models
22
+ models = where(:haystack_index => nil)
23
+ end
24
+
25
+ threads = options[:threads] || 16
26
+
27
+ models.all.each do |doc|
19
28
  Mongoid::Haystack::Index.remove(doc)
29
+ end
30
+
31
+ models.all.each do |doc|
20
32
  Mongoid::Haystack::Index.add(doc)
21
33
  end
22
34
  end
@@ -208,7 +220,9 @@ module Mongoid
208
220
  end
209
221
 
210
222
  def search_tokens_for(search)
211
- values = Token.values_for(search.to_s)
223
+ #values = Token.values_for(search.to_s)
224
+ #values = Util.phrases_for(search).map{|phrase| Util.stems_for(phrase)}.flatten
225
+ values = Util.search_for(search)
212
226
  tokens = []
213
227
 
214
228
  Token.where(:value.in => values).each do |token|
@@ -216,6 +230,8 @@ module Mongoid
216
230
  tokens[index] = token
217
231
  end
218
232
 
233
+ tokens.compact!
234
+
219
235
  total = Token.total.to_f
220
236
 
221
237
  rarity = {}
@@ -4,8 +4,7 @@ module Mongoid
4
4
  module Haystack
5
5
  module Stemming
6
6
  def stem(*args, &block)
7
- string = args.join(' ')
8
- words = Util.words_for(*args)
7
+ words = args.join(' ').strip.split(/\s+/)
9
8
  list = []
10
9
 
11
10
  words.each do |word|
@@ -80,7 +80,11 @@ module Mongoid
80
80
  index({:count => 1})
81
81
 
82
82
  def frequency(n_tokens = Token.total.value.to_f)
83
- (count / n_tokens).round(2)
83
+ if n_tokens.zero?
84
+ Float::Infinity
85
+ else
86
+ (count / n_tokens).round(2)
87
+ end
84
88
  end
85
89
 
86
90
  def frequency_bin(n_tokens = Token.total.value.to_f)
@@ -92,7 +96,11 @@ module Mongoid
92
96
  end
93
97
 
94
98
  def rarity_bin(n_tokens = Token.total.value.to_f)
95
- (rarity(n_tokens) * 10).truncate
99
+ if n_tokens.zero?
100
+ Float::Infinity
101
+ else
102
+ (rarity(n_tokens) * 10).truncate
103
+ end
96
104
  end
97
105
  end
98
106
  end
@@ -160,6 +160,10 @@ module Mongoid
160
160
  Stemming.stem(*args, &block)
161
161
  end
162
162
 
163
+ def search_for(*args, &block)
164
+ phrases_for(*args).map{|phrase| [phrase, stems_for(phrase)]}.flatten.compact.uniq
165
+ end
166
+
163
167
  def stopword?(word)
164
168
  word = UnicodeUtils.nfkd(word.to_s.strip.downcase)
165
169
  word.empty? or Stemming::Stopwords.stopword?(word)
@@ -3,7 +3,7 @@
3
3
 
4
4
  Gem::Specification::new do |spec|
5
5
  spec.name = "mongoid-haystack"
6
- spec.version = "1.3.0"
6
+ spec.version = "1.4.0"
7
7
  spec.platform = Gem::Platform::RUBY
8
8
  spec.summary = "mongoid-haystack"
9
9
  spec.description = "a mongoid 3 zero-config, zero-integration, POLS pure mongo fulltext solution"
@@ -72,6 +72,8 @@ Gem::Specification::new do |spec|
72
72
 
73
73
  spec.add_dependency(*["unicode_utils", "~> 1.4"])
74
74
 
75
+ spec.add_dependency(*["threadify", "~> 1.3"])
76
+
75
77
 
76
78
  spec.extensions.push(*[])
77
79
 
@@ -45,12 +45,13 @@ Testing Mongoid::Haystack do
45
45
  d = A.create!(:content => 'dog dog dog cat')
46
46
 
47
47
  assert{ Mongoid::Haystack.index(A) }
48
+ Mongoid::Haystack.search('cat dog').map(&:model) == [d, c, b, a]
48
49
  assert{ Mongoid::Haystack.search('cat dog').map(&:model) == [d, c, b, a] }
49
50
  end
50
51
 
51
52
  ##
52
53
  #
53
- testing 'that word specificity affects the sort' do
54
+ testing 'that word specificity affects the search' do
54
55
  a = A.create!(:content => 'cat@dog.com')
55
56
  b = A.create!(:content => 'dogs')
56
57
  c = A.create!(:content => 'dog')
@@ -59,7 +60,7 @@ Testing Mongoid::Haystack do
59
60
 
60
61
  assert{ Mongoid::Haystack.index(A) }
61
62
 
62
- assert{ Mongoid::Haystack.search('cat@dog.com').map(&:model) == [a, e, d, c, b] }
63
+ assert{ Mongoid::Haystack.search('cat@dog.com').map(&:model) == [a] }
63
64
  assert{ Mongoid::Haystack.search('cat').map(&:model) == [e, d, a] }
64
65
  assert{ Mongoid::Haystack.search('cats').map(&:model) == [d, e, a] }
65
66
  assert{ Mongoid::Haystack.search('dog').map(&:model) == [c, b, a] }
@@ -392,9 +393,9 @@ Testing Mongoid::Haystack do
392
393
  test '.stems_for' do
393
394
  {
394
395
  ' cats and dogs ' => %w( cat dog ),
395
- ' cats!and?dogs ' => %w( cat dog ),
396
+ ' cats!and?dogs ' => %w( cats!and?dog ),
396
397
  ' fishing and hunting ' => %w( fish hunt ),
397
- ' fishing-and-hunting ' => %w( fish hunt ),
398
+ ' fishing-and-hunting ' => %w( fishing-and-hunt ),
398
399
  }.each do |src, dst|
399
400
  assert{ Mongoid::Haystack::stems_for(src) == dst }
400
401
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-haystack
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-04 00:00:00.000000000 Z
12
+ date: 2013-05-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongoid
@@ -123,6 +123,22 @@ dependencies:
123
123
  - - ~>
124
124
  - !ruby/object:Gem::Version
125
125
  version: '1.4'
126
+ - !ruby/object:Gem::Dependency
127
+ name: threadify
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ~>
132
+ - !ruby/object:Gem::Version
133
+ version: '1.3'
134
+ type: :runtime
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ~>
140
+ - !ruby/object:Gem::Version
141
+ version: '1.3'
126
142
  description: a mongoid 3 zero-config, zero-integration, POLS pure mongo fulltext solution
127
143
  email: ara.t.howard@gmail.com
128
144
  executables: []