mongoid_search 0.3.4 → 0.3.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +128 -115
- data/Rakefile +5 -2
- data/VERSION +1 -1
- data/lib/mongoid_search.rb +4 -8
- data/lib/mongoid_search/log.rb +2 -1
- data/lib/mongoid_search/mongoid_search.rb +23 -22
- data/lib/mongoid_search/tasks/mongoid_search.rake +1 -1
- data/lib/mongoid_search/util.rb +17 -18
- data/spec/models/product.rb +10 -6
- data/spec/models/subproduct.rb +1 -1
- data/spec/models/tag.rb +2 -2
- data/spec/models/variant.rb +1 -1
- data/spec/mongoid_search_spec.rb +158 -147
- data/spec/spec_helper.rb +4 -1
- data/spec/util_spec.rb +28 -29
- metadata +25 -11
@@ -1,6 +1,6 @@
|
|
1
1
|
namespace :mongoid_search do
|
2
2
|
desc 'Goes through all documents with search enabled and indexes the keywords.'
|
3
|
-
task :
|
3
|
+
task index: :environment do
|
4
4
|
::Rails.application.eager_load!
|
5
5
|
if Mongoid::Search.classes.blank?
|
6
6
|
Mongoid::Search::Log.log "No model to index keywords.\n"
|
data/lib/mongoid_search/util.rb
CHANGED
@@ -1,28 +1,28 @@
|
|
1
|
-
|
1
|
+
|
2
2
|
module Mongoid::Search::Util
|
3
3
|
def self.keywords(klass, fields)
|
4
4
|
if fields.is_a?(Array)
|
5
5
|
fields.map do |field|
|
6
|
-
|
6
|
+
keywords(klass, field)
|
7
7
|
end
|
8
8
|
elsif fields.is_a?(Hash)
|
9
9
|
fields.keys.map do |field|
|
10
10
|
attribute = klass.send(field)
|
11
11
|
unless attribute.blank?
|
12
12
|
if attribute.is_a?(Array)
|
13
|
-
attribute.map{ |a|
|
13
|
+
attribute.map { |a| keywords(a, fields[field]) }
|
14
14
|
else
|
15
|
-
|
15
|
+
keywords(attribute, fields[field])
|
16
16
|
end
|
17
17
|
end
|
18
18
|
end
|
19
19
|
else
|
20
|
-
value = if klass.respond_to?(fields.to_s +
|
21
|
-
klass.send(fields.to_s +
|
20
|
+
value = if klass.respond_to?(fields.to_s + '_translations')
|
21
|
+
klass.send(fields.to_s + '_translations').values
|
22
22
|
elsif klass.respond_to?(fields)
|
23
23
|
klass.send(fields)
|
24
24
|
else
|
25
|
-
value = klass[fields]
|
25
|
+
value = klass[fields]
|
26
26
|
end
|
27
27
|
value = value.join(' ') if value.respond_to?(:join)
|
28
28
|
normalize_keywords(value) if value
|
@@ -38,19 +38,18 @@ module Mongoid::Search::Util
|
|
38
38
|
strip_accents = Mongoid::Search.strip_accents
|
39
39
|
|
40
40
|
return [] if text.blank?
|
41
|
-
text = text.to_s
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
41
|
+
text = text.to_s
|
42
|
+
.mb_chars
|
43
|
+
.normalize(:kd)
|
44
|
+
.downcase
|
45
|
+
.to_s
|
46
|
+
.gsub(strip_symbols, ' ') # strip symbols
|
47
|
+
.gsub(strip_accents, '') # strip accents
|
48
|
+
.gsub(/[#{ligatures.keys.join("")}]/) { |c| ligatures[c] }
|
49
|
+
.split(' ')
|
50
|
+
.reject { |word| word.size < Mongoid::Search.minimum_word_size }
|
51
51
|
text = text.reject { |word| ignore_list.include?(word) } unless ignore_list.blank?
|
52
52
|
text = text.map(&stem_proc) if stem_keywords
|
53
53
|
text
|
54
54
|
end
|
55
|
-
|
56
55
|
end
|
data/spec/models/product.rb
CHANGED
@@ -5,13 +5,17 @@ class Product
|
|
5
5
|
|
6
6
|
field :brand
|
7
7
|
field :name
|
8
|
-
field :attrs, :
|
9
|
-
field :info, :
|
8
|
+
field :attrs, type: Array
|
9
|
+
field :info, type: Hash
|
10
10
|
|
11
|
-
has_many
|
12
|
-
|
11
|
+
has_many :tags
|
12
|
+
if Mongoid::Compatibility::Version.mongoid6_or_newer?
|
13
|
+
belongs_to :category, required: false
|
14
|
+
else
|
15
|
+
belongs_to :category
|
16
|
+
end
|
13
17
|
embeds_many :subproducts
|
14
18
|
|
15
|
-
search_in :brand, :name, :outlet, :attrs, :
|
16
|
-
|
19
|
+
search_in :brand, :name, :outlet, :attrs, tags: :name, category: %i[name description],
|
20
|
+
subproducts: %i[brand name], info: %i[summary description]
|
17
21
|
end
|
data/spec/models/subproduct.rb
CHANGED
data/spec/models/tag.rb
CHANGED
@@ -8,8 +8,8 @@ class Tag
|
|
8
8
|
belongs_to :product
|
9
9
|
|
10
10
|
def title
|
11
|
-
|
11
|
+
name
|
12
12
|
end
|
13
13
|
|
14
|
-
search_in :title, :
|
14
|
+
search_in :title, product: [:name, { info: %i[summary description], category: %i[name description] }]
|
15
15
|
end
|
data/spec/models/variant.rb
CHANGED
data/spec/mongoid_search_spec.rb
CHANGED
@@ -1,11 +1,10 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
4
2
|
|
5
3
|
describe Mongoid::Search do
|
6
|
-
|
7
4
|
before(:all) do
|
8
5
|
@default_proc = Mongoid::Search.stem_proc
|
6
|
+
@default_strip_symbols = Mongoid::Search.strip_symbols
|
7
|
+
@default_strip_accents = Mongoid::Search.strip_accents
|
9
8
|
end
|
10
9
|
|
11
10
|
after(:all) do
|
@@ -17,193 +16,192 @@ describe Mongoid::Search do
|
|
17
16
|
Mongoid::Search.stem_keywords = false
|
18
17
|
Mongoid::Search.ignore_list = nil
|
19
18
|
Mongoid::Search.stem_proc = @default_proc
|
20
|
-
@product = Product.create :
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
end
|
28
|
-
|
29
|
-
describe
|
30
|
-
context
|
31
|
-
it
|
32
|
-
expect(Product.full_text_search(
|
33
|
-
expect(Product.full_text_search(
|
19
|
+
@product = Product.create brand: 'Apple',
|
20
|
+
name: 'iPhone',
|
21
|
+
tags: (@tags = %w[Amazing Awesome Olé].map { |tag| Tag.new(name: tag) }),
|
22
|
+
category: Category.new(name: 'Mobile', description: 'Reviews'),
|
23
|
+
subproducts: [Subproduct.new(brand: 'Apple', name: 'Craddle')],
|
24
|
+
info: { summary: 'Info-summary',
|
25
|
+
description: 'Info-description' }
|
26
|
+
end
|
27
|
+
|
28
|
+
describe 'Serialized hash fields' do
|
29
|
+
context 'when the hash is populated' do
|
30
|
+
it 'should return the product' do
|
31
|
+
expect(Product.full_text_search('Info-summary').first).to eq @product
|
32
|
+
expect(Product.full_text_search('Info-description').first).to eq @product
|
34
33
|
end
|
35
34
|
end
|
36
35
|
|
37
|
-
context
|
36
|
+
context 'when the hash is empty' do
|
38
37
|
before(:each) do
|
39
38
|
@product.info = nil
|
40
39
|
@product.save
|
41
40
|
end
|
42
41
|
|
43
|
-
it
|
44
|
-
expect(Product.full_text_search(
|
45
|
-
expect(Product.full_text_search(
|
42
|
+
it 'should not return the product' do
|
43
|
+
expect(Product.full_text_search('Info-description').size).to eq 0
|
44
|
+
expect(Product.full_text_search('Info-summary').size).to eq 0
|
46
45
|
end
|
47
46
|
end
|
48
47
|
end
|
49
48
|
|
50
|
-
context
|
49
|
+
context 'utf-8 characters' do
|
51
50
|
before do
|
52
51
|
Mongoid::Search.stem_keywords = false
|
53
52
|
Mongoid::Search.ignore_list = nil
|
54
|
-
@product = Product.create :
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
53
|
+
@product = Product.create brand: 'Эльбрус',
|
54
|
+
name: 'Процессор',
|
55
|
+
tags: %w[Amazing Awesome Olé].map { |tag| Tag.new(name: tag) },
|
56
|
+
category: Category.new(name: 'процессоры'),
|
57
|
+
subproducts: []
|
59
58
|
end
|
60
59
|
|
61
|
-
it
|
62
|
-
expect(@product._keywords).to eq [
|
60
|
+
it 'should leave utf8 characters' do
|
61
|
+
expect(@product._keywords).to eq %w[amazing awesome ole процессор процессоры эльбрус]
|
63
62
|
end
|
64
63
|
|
65
64
|
it "should return results in search when case doesn't match" do
|
66
|
-
expect(Product.full_text_search(
|
65
|
+
expect(Product.full_text_search('ЭЛЬБРУС').size).to eq 1
|
67
66
|
end
|
68
67
|
end
|
69
68
|
|
70
|
-
context
|
71
|
-
context
|
72
|
-
it
|
69
|
+
context 'when references are nil' do
|
70
|
+
context 'when instance is being created' do
|
71
|
+
it 'should not complain about method missing' do
|
73
72
|
expect { Product.create! }.not_to raise_error
|
74
73
|
end
|
75
74
|
end
|
76
75
|
|
77
76
|
it 'should validate keywords' do
|
78
|
-
product = Product.create :
|
79
|
-
expect(product._keywords).to eq([
|
77
|
+
product = Product.create brand: 'Apple', name: 'iPhone'
|
78
|
+
expect(product._keywords).to eq(%w[apple iphone])
|
80
79
|
end
|
81
80
|
end
|
82
81
|
|
83
|
-
|
84
|
-
it "should set the _keywords field for array fields also" do
|
82
|
+
it 'should set the _keywords field for array fields also' do
|
85
83
|
@product.attrs = ['lightweight', 'plastic', :red]
|
86
84
|
@product.save!
|
87
85
|
expect(@product._keywords).to include 'lightweight', 'plastic', 'red'
|
88
86
|
end
|
89
87
|
|
90
|
-
it
|
91
|
-
variant = Variant.create :
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
88
|
+
it 'should inherit _keywords field and build upon' do
|
89
|
+
variant = Variant.create brand: 'Apple',
|
90
|
+
name: 'iPhone',
|
91
|
+
tags: %w[Amazing Awesome Olé].map { |tag| Tag.new(name: tag) },
|
92
|
+
category: Category.new(name: 'Mobile'),
|
93
|
+
subproducts: [Subproduct.new(brand: 'Apple', name: 'Craddle')],
|
94
|
+
color: :white
|
97
95
|
expect(variant._keywords).to include 'white'
|
98
|
-
expect(Variant.full_text_search(:
|
96
|
+
expect(Variant.full_text_search(name: 'Apple', color: :white)).to eq [variant]
|
99
97
|
end
|
100
98
|
|
101
|
-
it
|
99
|
+
it 'should expand the ligature to ease searching' do
|
102
100
|
# ref: http://en.wikipedia.org/wiki/Typographic_ligature, only for french right now. Rules for other languages are not know
|
103
|
-
variant1 = Variant.create :
|
104
|
-
variant2 = Variant.create :
|
101
|
+
variant1 = Variant.create tags: ['œuvre'].map { |tag| Tag.new(name: tag) }
|
102
|
+
variant2 = Variant.create tags: ['æquo'].map { |tag| Tag.new(name: tag) }
|
105
103
|
|
106
|
-
expect(Variant.full_text_search(
|
107
|
-
expect(Variant.full_text_search(
|
108
|
-
expect(Variant.full_text_search(
|
109
|
-
expect(Variant.full_text_search(
|
104
|
+
expect(Variant.full_text_search('œuvre')).to eq [variant1]
|
105
|
+
expect(Variant.full_text_search('oeuvre')).to eq [variant1]
|
106
|
+
expect(Variant.full_text_search('æquo')).to eq [variant2]
|
107
|
+
expect(Variant.full_text_search('aequo')).to eq [variant2]
|
110
108
|
end
|
111
109
|
|
112
|
-
it
|
110
|
+
it 'should set the _keywords field with stemmed words if stem is enabled' do
|
113
111
|
Mongoid::Search.stem_keywords = true
|
114
112
|
@product.save!
|
115
|
-
expect(@product._keywords.sort).to eq [
|
113
|
+
expect(@product._keywords.sort).to eq %w[amaz appl awesom craddl iphon mobil review ol info descript summari].sort
|
116
114
|
end
|
117
115
|
|
118
|
-
it
|
116
|
+
it 'should set the _keywords field with custom stemmed words if stem is enabled with a custom lambda' do
|
119
117
|
Mongoid::Search.stem_keywords = true
|
120
|
-
Mongoid::Search.stem_proc =
|
118
|
+
Mongoid::Search.stem_proc = proc { |word| word.upcase }
|
121
119
|
@product.save!
|
122
|
-
expect(@product._keywords.sort).to eq [
|
120
|
+
expect(@product._keywords.sort).to eq %w[AMAZING APPLE AWESOME CRADDLE DESCRIPTION INFO IPHONE MOBILE OLE REVIEWS SUMMARY]
|
123
121
|
end
|
124
122
|
|
125
|
-
it
|
126
|
-
Mongoid::Search.ignore_list = YAML.
|
123
|
+
it 'should ignore keywords in an ignore list' do
|
124
|
+
Mongoid::Search.ignore_list = YAML.safe_load(File.open(File.dirname(__FILE__) + '/config/ignorelist.yml'))['ignorelist']
|
127
125
|
@product.save!
|
128
|
-
expect(@product._keywords.sort).to eq [
|
126
|
+
expect(@product._keywords.sort).to eq %w[apple craddle iphone mobile reviews ole info description summary].sort
|
129
127
|
end
|
130
128
|
|
131
|
-
it
|
132
|
-
@product = Product.create :
|
133
|
-
|
134
|
-
|
135
|
-
|
129
|
+
it 'should incorporate numbers as keywords' do
|
130
|
+
@product = Product.create brand: 'Ford',
|
131
|
+
name: 'T 1908',
|
132
|
+
tags: %w[Amazing First Car].map { |tag| Tag.new(name: tag) },
|
133
|
+
category: Category.new(name: 'Vehicle')
|
136
134
|
|
137
135
|
@product.save!
|
138
|
-
expect(@product._keywords).to eq [
|
136
|
+
expect(@product._keywords).to eq %w[1908 amazing car first ford vehicle]
|
139
137
|
end
|
140
138
|
|
141
|
-
it
|
142
|
-
|
139
|
+
it 'should return results in search' do
|
140
|
+
expect(Product.full_text_search('apple').size).to eq 1
|
143
141
|
end
|
144
142
|
|
145
|
-
it
|
146
|
-
@product[:outlet] =
|
143
|
+
it 'should return results in search for dynamic attribute' do
|
144
|
+
@product[:outlet] = 'online shop'
|
147
145
|
@product.save!
|
148
|
-
|
146
|
+
expect(Product.full_text_search('online').size).to eq 1
|
149
147
|
end
|
150
148
|
|
151
|
-
it
|
152
|
-
|
153
|
-
|
149
|
+
it 'should return results in search even searching a accented word' do
|
150
|
+
expect(Product.full_text_search('Ole').size).to eq 1
|
151
|
+
expect(Product.full_text_search('Olé').size).to eq 1
|
154
152
|
end
|
155
153
|
|
156
154
|
it "should return results in search even if the case doesn't match" do
|
157
|
-
|
155
|
+
expect(Product.full_text_search('oLe').size).to eq 1
|
158
156
|
end
|
159
157
|
|
160
|
-
it
|
161
|
-
|
158
|
+
it 'should return results in search with a partial word by default' do
|
159
|
+
expect(Product.full_text_search('iph').size).to eq 1
|
162
160
|
end
|
163
161
|
|
164
|
-
it
|
165
|
-
|
162
|
+
it 'should return results for any matching word with default search' do
|
163
|
+
expect(Product.full_text_search('apple motorola').size).to eq 1
|
166
164
|
end
|
167
165
|
|
168
|
-
it
|
166
|
+
it 'should not return results when all words do not match, if using :match => :all' do
|
169
167
|
Mongoid::Search.match = :all
|
170
|
-
|
168
|
+
expect(Product.full_text_search('apple motorola').size).to eq 0
|
171
169
|
end
|
172
170
|
|
173
|
-
it
|
171
|
+
it 'should return results for any matching word, using :match => :all, passing :match => :any to .full_text_search' do
|
174
172
|
Mongoid::Search.match = :all
|
175
|
-
|
173
|
+
expect(Product.full_text_search('apple motorola', match: :any).size).to eq 1
|
176
174
|
end
|
177
175
|
|
178
|
-
it
|
179
|
-
|
176
|
+
it 'should not return results when all words do not match, passing :match => :all to .full_text_search' do
|
177
|
+
expect(Product.full_text_search('apple motorola', match: :all).size).to eq 0
|
180
178
|
end
|
181
179
|
|
182
|
-
it
|
180
|
+
it 'should return no results when a blank search is made' do
|
183
181
|
Mongoid::Search.allow_empty_search = false
|
184
|
-
|
182
|
+
expect(Product.full_text_search('').size).to eq 0
|
185
183
|
end
|
186
184
|
|
187
|
-
it
|
185
|
+
it 'should return results when a blank search is made when :allow_empty_search is true' do
|
188
186
|
Mongoid::Search.allow_empty_search = true
|
189
|
-
|
187
|
+
expect(Product.full_text_search('').size).to eq 1
|
190
188
|
end
|
191
189
|
|
192
|
-
it
|
193
|
-
|
190
|
+
it 'should search for embedded documents' do
|
191
|
+
expect(Product.full_text_search('craddle').size).to eq 1
|
194
192
|
end
|
195
193
|
|
196
|
-
it
|
197
|
-
|
194
|
+
it 'should search for reference documents' do
|
195
|
+
expect(Product.full_text_search('reviews').size).to eq 1
|
198
196
|
end
|
199
197
|
|
200
198
|
it 'should work in a chainable fashion' do
|
201
|
-
expect(@product.category.products.where(:
|
202
|
-
expect(@product.category.products.full_text_search('craddle').where(:
|
199
|
+
expect(@product.category.products.where(brand: 'Apple').full_text_search('apple').size).to eq 1
|
200
|
+
expect(@product.category.products.full_text_search('craddle').where(brand: 'Apple').size).to eq 1
|
203
201
|
end
|
204
202
|
|
205
203
|
it 'should return the classes that include the search module' do
|
206
|
-
expect(Mongoid::Search.classes).to eq [Product, Tag]
|
204
|
+
expect(Mongoid::Search.classes.sort_by(&:name)).to eq [Product, Tag]
|
207
205
|
end
|
208
206
|
|
209
207
|
it 'should have a method to index keywords' do
|
@@ -214,22 +212,21 @@ describe Mongoid::Search do
|
|
214
212
|
expect(Product.index_keywords!).not_to include(false)
|
215
213
|
end
|
216
214
|
|
217
|
-
context
|
215
|
+
context 'when regex search is false' do
|
218
216
|
before do
|
219
217
|
Mongoid::Search.regex_search = false
|
220
218
|
end
|
221
219
|
|
222
|
-
it
|
223
|
-
|
220
|
+
it 'should not return results in search with a partial word if not using regex search' do
|
221
|
+
expect(Product.full_text_search('iph').size).to eq 0
|
224
222
|
end
|
225
223
|
|
226
|
-
it
|
227
|
-
|
224
|
+
it 'should return results in search with a full word if not using regex search' do
|
225
|
+
expect(Product.full_text_search('iphone').size).to eq 1
|
228
226
|
end
|
229
227
|
end
|
230
228
|
|
231
|
-
context
|
232
|
-
|
229
|
+
context 'when regex search is true' do
|
233
230
|
before do
|
234
231
|
Mongoid::Search.regex_search = true
|
235
232
|
end
|
@@ -238,90 +235,104 @@ describe Mongoid::Search do
|
|
238
235
|
Mongoid::Search.regex_search = false
|
239
236
|
end
|
240
237
|
|
241
|
-
it
|
242
|
-
expect(Product.full_text_search(
|
238
|
+
it 'should not return results in search with a partial word if using regex search' do
|
239
|
+
expect(Product.full_text_search('iph').size).to eq 1
|
243
240
|
end
|
244
241
|
|
245
|
-
it
|
246
|
-
expect(Product.full_text_search(
|
242
|
+
it 'should return results in search with a full word if using regex search' do
|
243
|
+
expect(Product.full_text_search('iphone').size).to eq 1
|
244
|
+
end
|
245
|
+
|
246
|
+
context 'when query include special characters that should not be stripped' do
|
247
|
+
before do
|
248
|
+
Mongoid::Search.strip_symbols = /[\n]/
|
249
|
+
Mongoid::Search.strip_accents = /[^\s\p{Graph}]/
|
250
|
+
end
|
251
|
+
|
252
|
+
after do
|
253
|
+
Mongoid::Search.strip_symbols = @default_strip_symbols
|
254
|
+
Mongoid::Search.strip_accents = @default_strip_accents
|
255
|
+
end
|
256
|
+
|
257
|
+
it 'should escape regex special characters' do
|
258
|
+
expect(Product.full_text_search('iphone (steve').size).to eq 1
|
259
|
+
end
|
247
260
|
end
|
248
261
|
|
249
262
|
context 'Match partial words on the beginning' do
|
250
|
-
|
251
263
|
before do
|
252
|
-
Mongoid::Search.regex =
|
264
|
+
Mongoid::Search.regex = proc { |query| /^#{query}/ }
|
253
265
|
end
|
254
266
|
|
255
|
-
it
|
256
|
-
expect(Product.full_text_search(
|
267
|
+
it 'should return results in search which starts with query string' do
|
268
|
+
expect(Product.full_text_search('iphone').size).to eq 1
|
257
269
|
end
|
258
270
|
|
259
|
-
it
|
260
|
-
expect(Product.full_text_search(
|
271
|
+
it 'should not return results in search which does not start with query string' do
|
272
|
+
expect(Product.full_text_search('phone').size).to eq 0
|
261
273
|
end
|
262
274
|
end
|
263
275
|
|
264
276
|
context 'Match partial words on the end' do
|
265
|
-
|
266
277
|
before do
|
267
|
-
Mongoid::Search.regex =
|
278
|
+
Mongoid::Search.regex = proc { |query| /#{query}$/ }
|
268
279
|
end
|
269
|
-
|
270
|
-
it
|
271
|
-
expect(Product.full_text_search(
|
280
|
+
|
281
|
+
it 'should return results in search which ends with query string' do
|
282
|
+
expect(Product.full_text_search('phone').size).to eq 1
|
272
283
|
end
|
273
284
|
|
274
|
-
it
|
275
|
-
expect(Product.full_text_search(
|
285
|
+
it 'should not return results in search which does not end with query string' do
|
286
|
+
expect(Product.full_text_search('phon').size).to eq 0
|
276
287
|
end
|
277
288
|
end
|
278
289
|
end
|
279
290
|
|
280
|
-
context
|
291
|
+
context 'relevant search' do
|
281
292
|
before do
|
282
293
|
Mongoid::Search.relevant_search = true
|
283
|
-
@imac = Product.create :
|
294
|
+
@imac = Product.create name: 'apple imac'
|
284
295
|
end
|
285
296
|
|
286
|
-
it
|
287
|
-
|
297
|
+
it 'should return results ordered by relevance and with correct ids' do
|
298
|
+
expect(Product.full_text_search('apple imac').map(&:_id)).to eq [@imac._id, @product._id]
|
288
299
|
end
|
289
300
|
|
290
|
-
it
|
291
|
-
|
301
|
+
it 'results should be recognized as persisted objects' do
|
302
|
+
expect(Product.full_text_search('apple imac').map(&:persisted?)).not_to include false
|
292
303
|
end
|
293
304
|
|
294
|
-
it
|
295
|
-
|
305
|
+
it 'should include relevance information' do
|
306
|
+
expect(Product.full_text_search('apple imac').map(&:relevance)).to eq [2, 1]
|
296
307
|
end
|
297
308
|
end
|
298
309
|
|
299
|
-
context
|
300
|
-
it
|
301
|
-
expect(@tags.first._keywords).to include
|
310
|
+
context 'when using methods for keywords' do
|
311
|
+
it 'should set the _keywords from methods' do
|
312
|
+
expect(@tags.first._keywords).to include 'amazing'
|
302
313
|
end
|
303
314
|
end
|
304
315
|
|
305
|
-
context
|
306
|
-
context
|
307
|
-
it
|
316
|
+
context 'when using deeply nested fields for keywords' do
|
317
|
+
context 'when explicitly calling set_keywords' do
|
318
|
+
it 'should set the _keywords from parent' do
|
308
319
|
@tags.first.send(:set_keywords)
|
309
|
-
expect(@tags.first._keywords).to eq [
|
320
|
+
expect(@tags.first._keywords).to eq %w[amazing description info iphone mobile reviews summary]
|
310
321
|
end
|
311
322
|
end
|
312
323
|
end
|
313
324
|
|
314
|
-
context
|
315
|
-
it
|
316
|
-
@product = Product.create :
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
expect(@product._keywords).to include(
|
325
|
+
context 'when using localized fields' do
|
326
|
+
it 'should set the keywords from all localizations' do
|
327
|
+
@product = Product.create brand: 'Ford',
|
328
|
+
name: 'T 1908',
|
329
|
+
tags: %w[Amazing First Car].map { |tag| Tag.new(name: tag) },
|
330
|
+
category: Category.new(name_translations: { en: 'Vehicle', de: 'Fahrzeug' })
|
331
|
+
expect(@product._keywords).to include('fahrzeug')
|
321
332
|
end
|
322
333
|
end
|
323
334
|
|
324
|
-
context
|
335
|
+
context 'minimum word size' do
|
325
336
|
before(:each) do
|
326
337
|
Mongoid::Search.minimum_word_size = 3
|
327
338
|
end
|
@@ -330,14 +341,14 @@ describe Mongoid::Search do
|
|
330
341
|
Mongoid::Search.minimum_word_size = 2
|
331
342
|
end
|
332
343
|
|
333
|
-
it
|
334
|
-
@product = Product.create :
|
344
|
+
it 'should ignore keywords with length less than minimum word size' do
|
345
|
+
@product = Product.create name: 'a'
|
335
346
|
expect(@product._keywords.size).to eq 0
|
336
347
|
|
337
|
-
@product = Product.create :
|
348
|
+
@product = Product.create name: 'ap'
|
338
349
|
expect(@product._keywords.size).to eq 0
|
339
350
|
|
340
|
-
@product = Product.create :
|
351
|
+
@product = Product.create name: 'app'
|
341
352
|
expect(@product._keywords.size).to eq 1
|
342
353
|
end
|
343
354
|
end
|