mongoid_fulltext 0.8.2 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/danger-comment.yml +19 -0
  3. data/.github/workflows/danger.yml +21 -0
  4. data/.github/workflows/lint.yml +29 -0
  5. data/.github/workflows/release.yml +30 -0
  6. data/.github/workflows/test.yml +81 -0
  7. data/.gitignore +2 -0
  8. data/.rubocop.yml +149 -3
  9. data/.rubocop_todo.yml +104 -59
  10. data/.spellcheck.txt +181 -0
  11. data/CHANGELOG.md +54 -33
  12. data/Dangerfile +5 -1
  13. data/Gemfile +22 -20
  14. data/Gemfile.danger +5 -0
  15. data/README.md +171 -178
  16. data/RELEASING.md +47 -30
  17. data/Rakefile +21 -3
  18. data/UPGRADING.md +43 -0
  19. data/lib/mongoid/full_text_search/field_scores.rb +48 -0
  20. data/lib/mongoid/full_text_search/index_definition.rb +31 -0
  21. data/lib/mongoid/full_text_search/ngrams.rb +93 -0
  22. data/lib/mongoid/full_text_search/version.rb +1 -1
  23. data/lib/mongoid/full_text_search.rb +94 -125
  24. data/mongoid_fulltext.gemspec +2 -3
  25. data/script/release.rb +13 -0
  26. data/spec/models/chinese_artwork.rb +7 -0
  27. data/spec/models/localized_artwork.rb +29 -0
  28. data/spec/models/multilingual_artwork.rb +7 -0
  29. data/spec/models/russian_artwork.rb +0 -1
  30. data/spec/models/scoped_artwork.rb +14 -0
  31. data/spec/models/tagged_artwork.rb +9 -0
  32. data/spec/models/weighted_artwork.rb +34 -0
  33. data/spec/mongoid/array_fields_spec.rb +43 -0
  34. data/spec/mongoid/bulk_indexing_spec.rb +56 -0
  35. data/spec/mongoid/candidate_limits_spec.rb +19 -0
  36. data/spec/mongoid/criteria_search_spec.rb +73 -0
  37. data/spec/mongoid/full_text_search_spec.rb +31 -6
  38. data/spec/mongoid/localized_fields_spec.rb +197 -0
  39. data/spec/mongoid/ngram_sampling_spec.rb +55 -0
  40. data/spec/mongoid/non_latin_spec.rb +50 -0
  41. data/spec/mongoid/timeout_option_spec.rb +18 -0
  42. data/spec/mongoid/weighted_fields_spec.rb +102 -0
  43. data/spec/release/release_spec.rb +60 -0
  44. data/spec/spec_helper.rb +15 -3
  45. data/spec/unit/field_scores_spec.rb +54 -0
  46. data/spec/unit/index_definition_spec.rb +48 -0
  47. data/spec/unit/ngrams_spec.rb +73 -0
  48. data/spec/version_spec.rb +7 -0
  49. metadata +41 -11
  50. data/.travis.yml +0 -33
@@ -0,0 +1,73 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Scoped full-text searches' do
4
+ it 'combines criteria with locale filtering' do
5
+ I18n.with_locale(:en) do
6
+ included = LocalizedArtwork.create!(title_translations: { 'en' => 'Flowers' }, artist: 'Picasso')
7
+ LocalizedArtwork.create!(title_translations: { 'en' => 'Flowers' }, artist: 'Monet')
8
+ expect(LocalizedArtwork.where(artist: 'Picasso').fulltext_search('flowers')).to eq([included])
9
+ end
10
+ end
11
+
12
+ it 'applies criteria without raising for excluded documents' do
13
+ excluded = ScopedArtwork.create!(title: 'My Doc', value: 0)
14
+ included = ScopedArtwork.create!(title: 'My Doc 2', value: 10)
15
+ expect(ScopedArtwork.where(value: 10).fulltext_search('doc')).to eq([included])
16
+ expect(ScopedArtwork.fulltext_search('doc')).to include(excluded, included)
17
+ end
18
+
19
+ it 'fills the result limit after filtering higher-ranked documents' do
20
+ ScopedArtwork.create!(title: 'Flowers', value: 0)
21
+ included = ScopedArtwork.create!(title: 'Flowers in a vase', value: 10)
22
+ expect(ScopedArtwork.where(value: 10).fulltext_search('flowers', max_results: 1)).to eq([included])
23
+ end
24
+
25
+ it 'combines criteria on document ids with the result limit' do
26
+ ScopedArtwork.create!(title: 'Flowers')
27
+ included = ScopedArtwork.create!(title: 'Flowers in a vase')
28
+ expect(ScopedArtwork.where(id: included.id).fulltext_search('flowers', max_results: 1)).to eq([included])
29
+ end
30
+
31
+ it 'preserves scores and relevance order in a scoped search' do
32
+ first = ScopedArtwork.create!(title: 'Flowers', value: 10)
33
+ second = ScopedArtwork.create!(title: 'Flowers in a vase', value: 10)
34
+ results = ScopedArtwork.where(value: 10).fulltext_search('flowers', return_scores: true)
35
+ expect(results.map(&:first)).to eq([first, second])
36
+ expect(results.first.last).to be > results.last.last
37
+ end
38
+
39
+ it 'preserves the existing zero-limit behavior for unscoped searches' do
40
+ first = ScopedArtwork.create!(title: 'Flowers')
41
+ second = ScopedArtwork.create!(title: 'Flowers in a vase')
42
+ expect(ScopedArtwork.fulltext_search('flowers', max_results: 0)).to eq([first, second])
43
+ end
44
+
45
+ it 'searches descendants without returning parent or sibling documents' do
46
+ parent = ScopedArtwork.create!(title: 'My Doc')
47
+ child = InheritedArtwork.create!(title: 'My Inherited Doc')
48
+ grandchild = FurtherInheritedArtwork.create!(title: 'My Further Inherited Doc')
49
+ expect(ScopedArtwork.fulltext_search('doc')).to match_array([parent, child, grandchild])
50
+ expect(InheritedArtwork.fulltext_search('doc')).to match_array([child, grandchild])
51
+ expect(FurtherInheritedArtwork.fulltext_search('doc')).to eq([grandchild])
52
+ end
53
+
54
+ it 'does not raise for dangling index references even when Mongoid raises on find' do
55
+ artwork = ScopedArtwork.create!(title: 'Flowers')
56
+ artwork.delete
57
+ previous = Mongoid.raise_not_found_error
58
+ begin
59
+ Mongoid.raise_not_found_error = true
60
+ expect(ScopedArtwork.fulltext_search('flowers')).to eq([])
61
+ expect(ScopedArtwork.fulltext_search('flowers', return_scores: true)).to eq([])
62
+ ensure
63
+ Mongoid.raise_not_found_error = previous
64
+ end
65
+ end
66
+
67
+ it 'keeps unscoped shared-index searches across unrelated models' do
68
+ artist = ExternalArtist.create!(full_name: 'Picasso')
69
+ artwork = ExternalArtwork.create!(title: 'Picasso')
70
+ expect(ExternalArtwork.fulltext_search('picasso')).to match_array([artist, artwork])
71
+ expect(ExternalArtwork.where(title: 'Picasso').fulltext_search('picasso')).to eq([artwork])
72
+ end
73
+ end
@@ -1,4 +1,3 @@
1
-
2
1
  require 'spec_helper'
3
2
 
4
3
  describe Mongoid::FullTextSearch do
@@ -434,7 +433,7 @@ describe Mongoid::FullTextSearch do
434
433
  %w[warho rand].each do |query|
435
434
  results = ExternalArtist.fulltext_search(query, return_scores: true)
436
435
  expect(results.length).to be > 0
437
- expect(results.map { |result| result[-1] if result[0].to_s.starts_with?(query) }.compact.inject(true) { |accum, item| accum &= (item >= 1 && item < 2) }).to be_truthy
436
+ expect(results.map { |result| result[-1] if result[0].to_s.starts_with?(query) }.compact.inject(true) { |accum, item| accum &= item >= 1 && item < 2 }).to be_truthy
438
437
  end
439
438
  end
440
439
 
@@ -491,8 +490,8 @@ describe Mongoid::FullTextSearch do
491
490
  end
492
491
 
493
492
  context 'remove_from_ngram_index' do
494
- let!(:flowers1) { BasicArtwork.create(title: 'Flowers 1') }
495
- let!(:flowers2) { BasicArtwork.create(title: 'Flowers 1') }
493
+ let!(:flowers1) { BasicArtwork.create(title: 'Flowers 1') }
494
+ let!(:flowers2) { BasicArtwork.create(title: 'Flowers 1') }
496
495
 
497
496
  it 'removes all records from the index' do
498
497
  BasicArtwork.remove_from_ngram_index
@@ -506,8 +505,8 @@ describe Mongoid::FullTextSearch do
506
505
  end
507
506
 
508
507
  context 'update_ngram_index' do
509
- let!(:flowers1) { BasicArtwork.create(title: 'Flowers 1') }
510
- let!(:flowers2) { BasicArtwork.create(title: 'Flowers 2') }
508
+ let!(:flowers1) { BasicArtwork.create(title: 'Flowers 1') }
509
+ let!(:flowers2) { BasicArtwork.create(title: 'Flowers 2') }
511
510
 
512
511
  context 'when config[:update_if] exists' do
513
512
  let(:painting) { BasicArtwork.new title: 'Painting' }
@@ -634,6 +633,18 @@ describe Mongoid::FullTextSearch do
634
633
  BasicArtwork.create_indexes
635
634
  end
636
635
  end
636
+
637
+ context 'using update index options' do
638
+ it 'will not timeout' do
639
+ expect(BasicArtwork).to receive_message_chain(:all, :no_timeout).and_return([])
640
+ BasicArtwork.update_ngram_index(timeout: false)
641
+ end
642
+
643
+ it 'will timeout' do
644
+ expect(BasicArtwork).to receive_message_chain(:all).and_return([])
645
+ BasicArtwork.update_ngram_index(timeout: true)
646
+ end
647
+ end
637
648
  end
638
649
 
639
650
  context 'batched reindexing' do
@@ -646,6 +657,20 @@ describe Mongoid::FullTextSearch do
646
657
  end
647
658
  end
648
659
 
660
+ context 'creating indexes before any documents exist' do
661
+ it 'creates both indexes in a missing collection without inserting documents' do
662
+ coll = BasicArtwork.collection.database[BasicArtwork.mongoid_fulltext_config.keys.first]
663
+ BasicArtwork.create_indexes
664
+ coll.drop
665
+ BasicArtwork.create_indexes
666
+
667
+ indexes = []
668
+ coll.indexes.each { |index| indexes << index['name'] }
669
+ expect(indexes).to include('fts_index', 'document_id_1') if Mongoid::Compatibility::Version.mongoid5_or_newer?
670
+ expect(coll.find.count).to eq(0)
671
+ end
672
+ end
673
+
649
674
  # For =~ operator documentation
650
675
  # https://github.com/dchelimsky/rspec/blob/master/lib/spec/matchers/match_array.rb#L53
651
676
 
@@ -0,0 +1,197 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Localized fields' do
4
+ around do |example|
5
+ locales = I18n.available_locales
6
+ begin
7
+ I18n.available_locales = %i[en cs]
8
+ I18n.with_locale(:en) { example.run }
9
+ ensure
10
+ I18n.available_locales = locales
11
+ end
12
+ end
13
+
14
+ let(:artwork) { LocalizedArtwork.create!(title_translations: { 'en' => 'Flowers', 'cs' => 'Kvetiny' }, artist: 'Picasso') }
15
+
16
+ it 'searches the current locale only' do
17
+ artwork
18
+ expect(LocalizedArtwork.fulltext_search('flowers')).to eq([artwork])
19
+ expect(LocalizedArtwork.fulltext_search('kvetiny')).to eq([])
20
+ I18n.with_locale(:cs) do
21
+ expect(LocalizedArtwork.fulltext_search('kvetiny')).to eq([artwork])
22
+ expect(LocalizedArtwork.fulltext_search('flowers')).to eq([])
23
+ end
24
+ end
25
+
26
+ it 'indexes scalar fields and computed methods in every locale' do
27
+ artwork
28
+ %i[en cs].each do |locale|
29
+ I18n.with_locale(locale) { expect(LocalizedArtwork.fulltext_search('picasso')).to eq([artwork]) }
30
+ end
31
+ expect(LocalizedArtwork.fulltext_search('english')).to eq([artwork])
32
+ I18n.with_locale(:cs) { expect(LocalizedArtwork.fulltext_search('cesky')).to eq([artwork]) }
33
+ end
34
+
35
+ it 'evaluates filters in the locale of the index' do
36
+ artwork
37
+ expect(LocalizedArtwork.fulltext_search('flowers', language: 'en')).to eq([artwork])
38
+ I18n.with_locale(:cs) { expect(LocalizedArtwork.fulltext_search('kvetiny', language: 'cs')).to eq([artwork]) }
39
+ end
40
+
41
+ it 'replaces all translations without changing the caller locale' do
42
+ artwork
43
+ I18n.with_locale(:cs) do
44
+ artwork.title_translations = { 'en' => 'Painting', 'cs' => 'Obraz' }
45
+ artwork.save!
46
+ expect(I18n.locale).to eq(:cs)
47
+ expect(LocalizedArtwork.fulltext_search('kvetiny')).to eq([])
48
+ expect(LocalizedArtwork.fulltext_search('obraz')).to eq([artwork])
49
+ end
50
+ expect(LocalizedArtwork.fulltext_search('flowers')).to eq([])
51
+ expect(LocalizedArtwork.fulltext_search('painting')).to eq([artwork])
52
+ end
53
+
54
+ it 'continues after an empty translation in the first locale' do
55
+ document = OnlyLocalizedArtwork.create!(title_translations: { 'en' => '', 'cs' => 'Obraz' })
56
+ I18n.with_locale(:cs) { expect(OnlyLocalizedArtwork.fulltext_search('obraz')).to eq([document]) }
57
+ end
58
+
59
+ it 'removes records for a translation that becomes empty' do
60
+ document = OnlyLocalizedArtwork.create!(title_translations: { 'en' => 'Flowers', 'cs' => 'Obraz' })
61
+ document.title_translations = { 'en' => 'Flowers', 'cs' => '' }
62
+ document.save!
63
+ expect(OnlyLocalizedArtwork.fulltext_search('flowers')).to eq([document])
64
+ I18n.with_locale(:cs) { expect(OnlyLocalizedArtwork.fulltext_search('obraz')).to eq([]) }
65
+ end
66
+
67
+ it 'evaluates update_if independently in each locale' do
68
+ config = OnlyLocalizedArtwork.mongoid_fulltext_config.values.first
69
+ begin
70
+ config[:update_if] = ->(_document) { I18n.locale == :cs }
71
+ document = OnlyLocalizedArtwork.create!(title_translations: { 'en' => 'Flowers', 'cs' => 'Obraz' })
72
+ expect(OnlyLocalizedArtwork.fulltext_search('flowers')).to eq([])
73
+ I18n.with_locale(:cs) { expect(OnlyLocalizedArtwork.fulltext_search('obraz')).to eq([document]) }
74
+ ensure
75
+ config.delete(:update_if)
76
+ end
77
+ end
78
+
79
+ it 'removes all locale indexes for a document' do
80
+ artwork.destroy
81
+ expect(LocalizedArtwork.fulltext_search('flowers')).to eq([])
82
+ I18n.with_locale(:cs) { expect(LocalizedArtwork.fulltext_search('kvetiny')).to eq([]) }
83
+ end
84
+
85
+ it 'removes and rebuilds all locales through the class API' do
86
+ artwork
87
+ LocalizedArtwork.remove_from_ngram_index
88
+ expect(LocalizedArtwork.fulltext_search('flowers')).to eq([])
89
+ I18n.with_locale(:cs) { expect(LocalizedArtwork.fulltext_search('kvetiny')).to eq([]) }
90
+ LocalizedArtwork.update_ngram_index
91
+ expect(LocalizedArtwork.fulltext_search('flowers')).to eq([artwork])
92
+ I18n.with_locale(:cs) { expect(LocalizedArtwork.fulltext_search('kvetiny')).to eq([artwork]) }
93
+ end
94
+
95
+ it 'stores locales together in the original index collection' do
96
+ artwork
97
+ name = LocalizedArtwork.mongoid_fulltext_config.keys.first
98
+ records = LocalizedArtwork.collection.database[name].find.to_a
99
+ expect(records.map { |record| record['locale'] }.uniq.sort).to eq(%w[cs en])
100
+ expect(records.map { |record| record['document_id'] }.uniq).to eq([artwork.id])
101
+ names = []
102
+ LocalizedArtwork.each_fulltext_index { |index_name, _config| names << index_name }
103
+ expect(names).to eq([name, name])
104
+ end
105
+
106
+ it 'keeps the same collection when available locales change' do
107
+ I18n.available_locales = [:en]
108
+ artwork
109
+ I18n.available_locales = %i[en cs]
110
+ expect(LocalizedArtwork.fulltext_search('flowers')).to eq([artwork])
111
+ end
112
+
113
+ it 'leaves nonlocalized index records unchanged' do
114
+ artwork = PartlyLocalizedArtwork.create!(artist: 'Picasso')
115
+ expect(PartlyLocalizedArtwork.fulltext_search('picasso')).to eq([artwork])
116
+ collections = []
117
+ PartlyLocalizedArtwork.each_fulltext_index { |name, _config| collections << name }
118
+ expect(collections).to eq(['mongoid_fulltext.untranslated'])
119
+ records = PartlyLocalizedArtwork.collection.database[collections.first].find.to_a
120
+ expect(records).not_to be_empty
121
+ expect(records.any? { |record| record.key?('locale') }).to eq(false)
122
+ end
123
+
124
+ it 'restores the caller locale when a field reader raises' do
125
+ document = LocalizedArtwork.new
126
+ allow(document).to receive(:description).and_raise('reader failure')
127
+ I18n.with_locale(:cs) do
128
+ expect { document.update_ngram_index }.to raise_error(RuntimeError, 'reader failure')
129
+ expect(I18n.locale).to eq(:cs)
130
+ end
131
+ end
132
+
133
+ it 'creates a locale-prefixed compound index in the original collection' do
134
+ LocalizedArtwork.create_indexes
135
+ name = LocalizedArtwork.mongoid_fulltext_config.keys.first
136
+ indexes = []
137
+ LocalizedArtwork.collection.database[name].indexes.each { |index| indexes << index }
138
+ if Mongoid::Compatibility::Version.mongoid5_or_newer?
139
+ expect(indexes.map { |index| index['name'] }).to include('fts_index', 'document_id_1')
140
+ keys = indexes.find { |index| index['name'] == 'fts_index' }['key']
141
+ expect(keys.to_a).to eq([['locale', 1], ['ngram', 1], ['score', -1], ['filter_values.language', 1]])
142
+ end
143
+ expect(I18n.locale).to eq(:en)
144
+ end
145
+
146
+ it 'removes records for every locale even after a locale is no longer configured' do
147
+ artwork
148
+ I18n.available_locales = [:en]
149
+ artwork.remove_from_ngram_index
150
+ name = LocalizedArtwork.mongoid_fulltext_config.keys.first
151
+ expect(LocalizedArtwork.collection.database[name].find.count).to eq(0)
152
+ end
153
+
154
+ it 'removes unconfigured locales through the class API' do
155
+ artwork
156
+ I18n.available_locales = [:en]
157
+ LocalizedArtwork.remove_from_ngram_index
158
+ name = LocalizedArtwork.mongoid_fulltext_config.keys.first
159
+ expect(LocalizedArtwork.collection.database[name].find.count).to eq(0)
160
+ end
161
+
162
+ it 'destroys records for locales that are no longer configured' do
163
+ artwork
164
+ I18n.available_locales = [:en]
165
+ artwork.destroy
166
+ name = LocalizedArtwork.mongoid_fulltext_config.keys.first
167
+ expect(LocalizedArtwork.collection.database[name].find.count).to eq(0)
168
+ end
169
+
170
+ it 'upgrades an existing search index to include locale' do
171
+ next unless Mongoid::Compatibility::Version.mongoid5_or_newer?
172
+
173
+ name = LocalizedArtwork.mongoid_fulltext_config.keys.first
174
+ coll = LocalizedArtwork.collection.database[name]
175
+ coll.indexes.drop_all
176
+ coll.indexes.create_one({ 'ngram' => 1, 'score' => -1 }, name: 'fts_index')
177
+ LocalizedArtwork.create_indexes
178
+ keys = coll.indexes.find { |index| index['name'] == 'fts_index' }['key']
179
+ expect(keys.keys.first).to eq('locale')
180
+ end
181
+
182
+ it 'keeps scores isolated when the same term exists in both locales' do
183
+ artwork
184
+ en_score = LocalizedArtwork.fulltext_search('picasso', return_scores: true).first.last
185
+ cs_score = I18n.with_locale(:cs) { LocalizedArtwork.fulltext_search('picasso', return_scores: true).first.last }
186
+ config = LocalizedArtwork.mongoid_fulltext_config.values.first
187
+ name = LocalizedArtwork.mongoid_fulltext_config.keys.first
188
+ coll = LocalizedArtwork.collection.database[name]
189
+ %w[en cs].zip([en_score, cs_score]).each do |locale, score|
190
+ query_ngrams = LocalizedArtwork.all_ngrams('picasso', config)
191
+ expected = coll.find('locale' => locale).inject(0.0) do |sum, record|
192
+ sum + record['score'] * (query_ngrams[record['ngram']] || 0)
193
+ end
194
+ expect(score).to be_within(0.00001).of(expected)
195
+ end
196
+ end
197
+ end
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Bounded ngram sampling' do
4
+ let(:config) do
5
+ BasicArtwork.mongoid_fulltext_config.values.first.merge(
6
+ max_ngrams_to_search: 3, index_full_words: false, index_short_prefixes: false
7
+ )
8
+ end
9
+
10
+ it 'preserves the model calculation entry point and its unbounded option' do
11
+ expect(Mongoid::FullTextSearch::Ngrams).to receive(:calculate).with('abcdef', config, false).and_call_original
12
+ expect(BasicArtwork.all_ngrams('abcdef', config, false).keys).to eq(%w[abc bcd cde def])
13
+ end
14
+
15
+ it 'selects the documented evenly spaced sample including the last ngram' do
16
+ expect(BasicArtwork.all_ngrams('abcdefghijk', config).keys).to eq(%w[abc efg ijk])
17
+ expect(BasicArtwork.all_ngrams('abcdefghij', config).keys).to eq(%w[abc efg hij])
18
+ end
19
+
20
+ it 'never exceeds the configured number of sliding-window ngrams' do
21
+ (1..12).each do |limit|
22
+ (0..26).each do |length|
23
+ text = 'abcdefghijklmnopqrstuvwxyz'[0, length]
24
+ ngrams = BasicArtwork.all_ngrams(text, config.merge(max_ngrams_to_search: limit))
25
+ expected_count = [[length - config[:ngram_width] + 1, 0].max, limit].min
26
+ expect(ngrams.length).to eq(expected_count)
27
+ next unless expected_count > 1
28
+
29
+ expect(ngrams.keys.first).to eq(text[0, 3])
30
+ expect(ngrams.keys.last).to eq(text[-3, 3])
31
+ end
32
+ end
33
+ end
34
+
35
+ it 'selects the prefix when only one ngram is requested' do
36
+ expect(BasicArtwork.all_ngrams('abcdefghijk', config.merge(max_ngrams_to_search: 1)).keys).to eq(['abc'])
37
+ end
38
+
39
+ it 'still indexes every sliding window when the bound is disabled' do
40
+ expect(BasicArtwork.all_ngrams('abcdefghijk', config, false).keys).to eq(%w[abc bcd cde def efg fgh ghi hij ijk])
41
+ end
42
+
43
+ it 'preserves full-word and short-prefix bonuses in addition to sampled ngrams' do
44
+ options = config.merge(index_full_words: true, index_short_prefixes: true)
45
+ expect(BasicArtwork.all_ngrams('abcdefghijk', options).keys).to eq(%w[abc efg ijk ab abcdefghijk])
46
+ end
47
+
48
+ it 'rejects a nonpositive or noninteger search bound' do
49
+ [0, -1, 1.5, nil].each do |limit|
50
+ expect do
51
+ BasicArtwork.fulltext_search_in :title, max_ngrams_to_search: limit
52
+ end.to raise_error(ArgumentError, ':max_ngrams_to_search must be a positive integer')
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Non-Latin alphabets' do
4
+ it 'searches Chinese characters included in the configured alphabet' do
5
+ artwork = ChineseArtwork.create!(title: "\u5367\u864e\u85cf\u9f99")
6
+ expect(ChineseArtwork.fulltext_search("\u85cf\u9f99")).to eq([artwork])
7
+ expect(ChineseArtwork.fulltext_search("\u5367\u864e")).to eq([artwork])
8
+ end
9
+
10
+ it 'does not index characters outside the configured alphabet' do
11
+ artwork = BasicArtwork.create!(title: "\u5367\u864e\u85cf\u9f99")
12
+ expect(BasicArtwork.fulltext_search(artwork.title)).to eq([])
13
+ end
14
+
15
+ it 'supports Han characters outside the example title without enumerating them' do
16
+ artwork = ChineseArtwork.create!(title: "\u4e2d\u6587\u641c\u7d22")
17
+ expect(ChineseArtwork.fulltext_search("\u641c\u7d22")).to eq([artwork])
18
+ expect(ChineseArtwork.fulltext_search("\u4e2d\u6587")).to eq([artwork])
19
+ expect(ChineseArtwork.fulltext_search('search')).to eq([])
20
+ end
21
+
22
+ it 'supports Unicode letters, combining marks, and numbers across scripts' do
23
+ artwork = MultilingualArtwork.create!(title: "Flowers \u0428\u0418\u0428\u041a\u0418\u041d \u4e2d\u6587 123")
24
+ %w[flowers шишкин 中文 123].each do |query|
25
+ expect(MultilingualArtwork.fulltext_search(query)).to eq([artwork])
26
+ end
27
+ config = MultilingualArtwork.mongoid_fulltext_config.values.first.merge(index_full_words: false)
28
+ expect(MultilingualArtwork.all_ngrams("e\u0301", config).keys).to eq(["e\u0301"])
29
+ end
30
+
31
+ it 'retains word separators independently of the regular expression' do
32
+ config = ChineseArtwork.mongoid_fulltext_config.values.first.merge(index_full_words: false)
33
+ expect(ChineseArtwork.all_ngrams("\u4e2d-\u6587", config).keys).to eq(["\u4e2d-", "-\u6587"])
34
+ expect(ChineseArtwork.all_ngrams("\u4e2d!\u6587", config).keys).to eq(["\u4e2d\u6587"])
35
+ end
36
+
37
+ it 'preserves explicit string allowlists' do
38
+ expect(RussianArtwork.mongoid_fulltext_config.values.first[:alphabet]).to be_a(Hash)
39
+ artwork = RussianArtwork.create!(title: "\u0428\u0438\u0448\u043a\u0438\u043d")
40
+ expect(RussianArtwork.fulltext_search("\u0448\u0438\u0448\u043a\u0438\u043d")).to eq([artwork])
41
+ end
42
+
43
+ it 'rejects unsupported alphabet types at declaration time' do
44
+ [nil, [], 123].each do |alphabet|
45
+ expect do
46
+ ChineseArtwork.fulltext_search_in :title, alphabet: alphabet
47
+ end.to raise_error(ArgumentError, ':alphabet must be a String or Regexp')
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Class-level indexing timeout options' do
4
+ it 'rebuilds all records with cursor timeouts disabled' do
5
+ first = DelayedArtwork.create!(title: 'Flowers 1')
6
+ second = DelayedArtwork.create!(title: 'Flowers 2')
7
+ expect(DelayedArtwork.fulltext_search('flower')).to eq([])
8
+ DelayedArtwork.update_ngram_index(timeout: false)
9
+ expect(DelayedArtwork.fulltext_search('flower')).to match_array([first, second])
10
+ end
11
+
12
+ it 'applies the no_timeout option to the class criteria' do
13
+ items = DelayedArtwork.all
14
+ expect(DelayedArtwork).to receive(:all).and_return(items)
15
+ expect(items).to receive(:no_timeout).and_call_original
16
+ DelayedArtwork.update_ngram_index(timeout: false)
17
+ end
18
+ end
@@ -0,0 +1,102 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Weighted fields' do
4
+ def records(artwork, index)
5
+ Hash[artwork.collection.database[index].find('document_id' => artwork.id).map do |record|
6
+ [record['ngram'], record['score']]
7
+ end]
8
+ end
9
+
10
+ it 'ranks identical title matches above description and default-weight tag matches' do
11
+ title = WeightedArtwork.create!(title: 'ghost')
12
+ description = WeightedArtwork.create!(description: 'ghost')
13
+ tags = WeightedArtwork.create!(tags: ['ghost'])
14
+ %w[weighted_max weighted_sum].each do |index|
15
+ results = WeightedArtwork.fulltext_search('ghost', index: index, return_scores: true)
16
+ expect(results.map(&:first)).to eq([title, description, tags])
17
+ expect(results[0][1]).to be_within(0.000001).of(results[2][1] * 10)
18
+ expect(results[1][1]).to be_within(0.000001).of(results[2][1] * 5)
19
+ end
20
+ end
21
+
22
+ it 'takes the maximum within array fields and combines fields with the configured mode' do
23
+ artwork = WeightedArtwork.create!(title: 'ghost', description: 'ghost', tags: %w[ghost ghost])
24
+ config = WeightedArtwork.mongoid_fulltext_config['weighted_max']
25
+ base = WeightedArtwork.all_ngrams('ghost', config, false)
26
+ expect(records(artwork, 'weighted_max')).to eq(Hash[base.map { |term, score| [term, score * 10] }])
27
+ expect(records(artwork, 'weighted_sum')).to eq(Hash[base.map { |term, score| [term, score * 16] }])
28
+ end
29
+
30
+ it 'retains the strongest array-element contribution regardless of element order' do
31
+ artwork = WeightedArtwork.create!(tags: ['ghost', 'ghost story', nil, ''])
32
+ original = records(artwork, 'weighted_sum')
33
+ artwork.update_attributes!(tags: ['ghost story', '', 'ghost', nil, 'ghost'])
34
+ expect(records(artwork, 'weighted_sum')).to eq(original)
35
+ end
36
+
37
+ it 'replaces scores on save and removes terms when fields are cleared or destroyed' do
38
+ artwork = WeightedArtwork.create!(title: 'ghost', tags: ['ghost'])
39
+ initial = records(artwork, 'weighted_max')
40
+ artwork.update_attributes!(title: nil)
41
+ expect(records(artwork, 'weighted_max')).to eq(Hash[initial.map { |term, score| [term, score / 10] }])
42
+ artwork.update_attributes!(tags: [])
43
+ expect(records(artwork, 'weighted_max')).to eq({})
44
+ artwork.update_attributes!(description: 'painting')
45
+ artwork.destroy
46
+ expect(records(artwork, 'weighted_sum')).to eq({})
47
+ end
48
+
49
+ it 'does not weight query ngrams' do
50
+ config = WeightedArtwork.mongoid_fulltext_config['weighted_max']
51
+ expect(WeightedArtwork.all_ngrams('ghost', config)).to eq(
52
+ WeightedArtwork.all_ngrams('ghost', config.merge(priorities: {}, score_aggregation: :sum))
53
+ )
54
+ end
55
+
56
+ it 'weights computed readers and supports class-level rebuilding' do
57
+ artwork = WeightedComputedArtwork.create!(title: 'ghost')
58
+ config = WeightedComputedArtwork.mongoid_fulltext_config.values.first
59
+ index = WeightedComputedArtwork.mongoid_fulltext_config.keys.first
60
+ expected = Hash[WeightedComputedArtwork.all_ngrams('ghost', config, false).map { |term, score| [term, score + score * 2.5] }]
61
+ expect(records(artwork, index)).to eq(expected)
62
+ WeightedComputedArtwork.remove_from_ngram_index
63
+ WeightedComputedArtwork.update_ngram_index(timeout: false)
64
+ expect(records(artwork, index)).to eq(expected)
65
+ end
66
+
67
+ it 'preserves exact legacy last-field scores when neither option is specified' do
68
+ artwork = TaggedArtwork.create!(title: 'ghost', tags: ['ghost story'])
69
+ config = TaggedArtwork.mongoid_fulltext_config.values.first
70
+ expected = TaggedArtwork.all_ngrams('ghost', config, false).merge(
71
+ TaggedArtwork.all_ngrams('ghost story', config, false)
72
+ )
73
+ expect(records(artwork, TaggedArtwork.mongoid_fulltext_config.keys.first)).to eq(expected)
74
+ end
75
+
76
+ it 'rejects invalid configuration before registering a new index' do
77
+ before = BasicArtwork.mongoid_fulltext_config.dup
78
+ expect do
79
+ BasicArtwork.fulltext_search_in :title, priorities: { missing: 2 }
80
+ end.to raise_error(ArgumentError, ':priorities keys must name indexed fields')
81
+ expect(BasicArtwork.mongoid_fulltext_config).to eq(before)
82
+ end
83
+
84
+ it 'keeps localized weighted scores and filters isolated' do
85
+ original_locales = I18n.available_locales
86
+ begin
87
+ I18n.available_locales = %i[en cs]
88
+ artwork = WeightedLocalizedArtwork.new(description: 'ghost')
89
+ artwork.title_translations = { 'en' => 'ghost', 'cs' => 'painting' }
90
+ artwork.save!
91
+ english = I18n.with_locale(:en) do
92
+ WeightedLocalizedArtwork.fulltext_search('ghost', return_scores: true, language: 'en').first[1]
93
+ end
94
+ czech = I18n.with_locale(:cs) do
95
+ WeightedLocalizedArtwork.fulltext_search('ghost', return_scores: true, language: 'cs').first[1]
96
+ end
97
+ expect(english).to be_within(0.000001).of(czech * 10)
98
+ ensure
99
+ I18n.available_locales = original_locales
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,60 @@
1
+ if RUBY_ENGINE == 'ruby' && Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.3')
2
+ require_relative '../../script/release'
3
+ require 'ruby-gem-release/rspec'
4
+
5
+ RSpec.describe MongoidFulltextRelease do
6
+ include RubyGemRelease::RSpec
7
+
8
+ def with_mongoid_fulltext_project
9
+ with_release_project do |project|
10
+ project.copy_files(path: File.expand_path('../..', __dir__), files: %w[
11
+ script/release.rb mongoid_fulltext.gemspec lib/**/*.rb
12
+ README.md CHANGELOG.md LICENSE UPGRADING.md
13
+ ])
14
+ project.configure_workflow(require: 'script/release', builder: 'MongoidFulltextRelease.workflow')
15
+ yield project
16
+ end
17
+ end
18
+
19
+ def write_changelog(project, notes)
20
+ project.write('CHANGELOG.md', [
21
+ '### 1.2.0 (Next)', '', notes, '* Your contribution here.', '',
22
+ '### 1.1.0 (2026/01/01)', '', '* Previous release.', ''
23
+ ].join("\n"))
24
+ project.write('lib/mongoid/full_text_search/version.rb', [
25
+ 'module Mongoid', ' module FullTextSearch', " VERSION = '1.2.0'.freeze", ' end', 'end', ''
26
+ ].join("\n"))
27
+ project.commit!
28
+ end
29
+
30
+ it 'dates the release and prepares the next patch version' do
31
+ with_mongoid_fulltext_project do |project|
32
+ write_changelog(project, '* [#1](https://github.com/mongoid/mongoid_fulltext/pull/1): Added a feature - [@dblock](https://github.com/dblock).')
33
+
34
+ project.release!
35
+
36
+ released = project.remote_file('v1.2.0', 'CHANGELOG.md')
37
+ expect(released).to match(%r{\A### 1\.2\.0 \(\d{4}/\d{2}/\d{2}\)\n\n\* \[#1\]})
38
+ expect(released).not_to include('(Next)', 'Your contribution here.')
39
+ expect(project.remote_file('v1.2.0', 'lib/mongoid/full_text_search/version.rb')).to include("VERSION = '1.2.0'")
40
+
41
+ pending = project.remote_file('master', 'CHANGELOG.md')
42
+ expect(pending).to start_with("### 1.2.1 (Next)\n\n* Your contribution here.\n\n### 1.2.0 (")
43
+ expect(project.remote_file('master', 'lib/mongoid/full_text_search/version.rb')).to include("VERSION = '1.2.1'")
44
+ expect(project.git('status', '--porcelain')).to be_empty
45
+ end
46
+ end
47
+
48
+ it 'rejects placeholder-only notes without changing files or remote refs' do
49
+ with_mongoid_fulltext_project do |project|
50
+ write_changelog(project, '')
51
+ original = project.git('rev-parse', 'origin/master')
52
+ project.run_task('release:workflow:capture')
53
+
54
+ expect { project.run_task('release:validate') }.to raise_error(RuntimeError, /no pending changelog notes/)
55
+ expect(project.git('status', '--porcelain')).to be_empty
56
+ expect(project.git('rev-parse', 'origin/master')).to eq(original)
57
+ end
58
+ end
59
+ end
60
+ end