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
data/spec/spec_helper.rb CHANGED
@@ -1,9 +1,17 @@
1
1
  require 'rubygems'
2
2
  require 'bundler/setup'
3
+ if ENV['COVERAGE'] == 'true'
4
+ require 'simplecov'
5
+ SimpleCov.start do
6
+ track_files 'lib/**/*.rb'
7
+ add_filter '/spec/'
8
+ end
9
+ end
3
10
  require 'rspec'
4
11
 
12
+ require 'logger'
5
13
  require 'mongoid'
6
- require 'database_cleaner'
14
+ require RUBY_VERSION >= '2.7' ? 'database_cleaner/mongoid' : 'database_cleaner'
7
15
 
8
16
  ENV['MONGOID_ENV'] = 'test'
9
17
 
@@ -12,14 +20,18 @@ require File.expand_path('../lib/mongoid_fulltext', __dir__)
12
20
  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
13
21
  Dir["#{File.dirname(__FILE__)}/models/**/*.rb"].each { |f| require f }
14
22
 
15
- DatabaseCleaner.orm = :mongoid
16
- DatabaseCleaner.strategy = :truncation
23
+ DatabaseCleaner[:mongoid].strategy = RUBY_VERSION >= '2.7' ? :deletion : :truncation
17
24
 
18
25
  Mongoid.logger.level = Logger::INFO
19
26
  Mongo::Logger.logger.level = Logger::INFO if Mongoid::Compatibility::Version.mongoid5_or_newer?
20
27
 
21
28
  Mongoid.configure do |config|
22
29
  config.connect_to('mongoid_fulltext_test')
30
+ if Mongoid::Compatibility::Version.mongoid5_or_newer?
31
+ config.clients['default']['hosts'] = [ENV.fetch('MONGODB_HOST', 'localhost:27017')]
32
+ else
33
+ config.sessions['default']['hosts'] = [ENV.fetch('MONGODB_HOST', 'localhost:27017')]
34
+ end
23
35
  end
24
36
 
25
37
  RSpec.configure do |c|
@@ -0,0 +1,54 @@
1
+ require 'rspec'
2
+ require_relative '../../lib/mongoid/full_text_search/field_scores'
3
+
4
+ describe Mongoid::FullTextSearch::FieldScores do
5
+ it 'leaves unconfigured indexes on the legacy path' do
6
+ config = {}
7
+ described_class.configure(config, [:title])
8
+ expect(config).to eq({})
9
+ end
10
+
11
+ it 'normalizes field names and defaults weighted indexes to maximum aggregation' do
12
+ priorities = { title: 2, 'description' => 0.5 }
13
+ config = { priorities: priorities }
14
+ described_class.configure(config, %i[title description])
15
+ expect(config).to eq(priorities: { 'title' => 2.0, 'description' => 0.5 }, score_aggregation: :max)
16
+ expect(priorities).to eq(title: 2, 'description' => 0.5)
17
+ end
18
+
19
+ it 'allows aggregation without explicit weights and supports to_s' do
20
+ config = { score_aggregation: :sum }
21
+ described_class.configure(config, [:to_s])
22
+ expect(config[:priorities]).to eq({})
23
+ described_class.configure({ priorities: { to_s: 3 } }, [:to_s])
24
+ end
25
+
26
+ it 'rejects invalid weights' do
27
+ [nil, '2', 0, -1, Float::NAN, Float::INFINITY, Complex(1, 1)].each do |weight|
28
+ expect do
29
+ described_class.configure({ priorities: { title: weight } }, [:title])
30
+ end.to raise_error(ArgumentError, ':priorities values must be positive finite numbers')
31
+ end
32
+ end
33
+
34
+ it 'rejects unknown, duplicate, or invalid field names' do
35
+ [{ missing: 2 }, { nil => 2 }, { title: 2, 'title' => 3 }].each do |priorities|
36
+ expect do
37
+ described_class.configure({ priorities: priorities }, [:title])
38
+ end.to raise_error(ArgumentError)
39
+ end
40
+ end
41
+
42
+ it 'rejects invalid priority maps and aggregation modes' do
43
+ [nil, [], 1].each do |priorities|
44
+ expect do
45
+ described_class.configure({ priorities: priorities }, [:title])
46
+ end.to raise_error(ArgumentError, ':priorities must be a Hash')
47
+ end
48
+ [nil, :average, 'sum'].each do |mode|
49
+ expect do
50
+ described_class.configure({ score_aggregation: mode }, [:title])
51
+ end.to raise_error(ArgumentError, ':score_aggregation must be :max or :sum')
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,48 @@
1
+ require 'rspec'
2
+ require 'mongoid/full_text_search/index_definition'
3
+
4
+ describe Mongoid::FullTextSearch::IndexDefinition do
5
+ it 'builds the nonlocalized search keys without filters' do
6
+ definition = described_class.new(nil, false, [])
7
+ expect(definition.keys).to eq([['ngram', 1], ['score', -1]])
8
+ expect(definition.obsolete_indexes).to eq([])
9
+ end
10
+
11
+ it 'sorts filters and prefixes localized keys with locale' do
12
+ definition = described_class.new({ z: true, a: true }, true, [])
13
+ expect(definition.keys).to eq([['locale', 1], ['ngram', 1], ['score', -1], ['filter_values.a', 1], ['filter_values.z', 1]])
14
+ end
15
+
16
+ it 'retains shared-model filters and identifies incomplete search indexes' do
17
+ old = { 'name' => 'fts_index', 'key' => { 'ngram' => 1, 'score' => -1, 'filter_values.z' => 1 } }
18
+ unrelated = { 'name' => 'document_id_1', 'key' => { 'document_id' => 1 } }
19
+ definition = described_class.new({ a: true }, true, [unrelated, old])
20
+ expect(definition.keys).to eq([['locale', 1], ['ngram', 1], ['score', -1], ['filter_values.a', 1], ['filter_values.z', 1]])
21
+ expect(definition.obsolete_indexes).to eq([old])
22
+ end
23
+
24
+ it 'preserves a complete index that includes other models filters' do
25
+ existing = { 'key' => { 'ngram' => 1, 'score' => -1, 'filter_values.a' => 1, 'filter_values.z' => 1 } }
26
+ definition = described_class.new({ a: true }, false, [existing])
27
+ expect(definition.obsolete_indexes).to eq([])
28
+ expect(definition.keys).to eq(existing['key'].to_a)
29
+ end
30
+
31
+ it 'preserves direction-insensitive matching rather than changing index policy' do
32
+ existing = { 'key' => { 'ngram' => -1, 'score' => 1 } }
33
+ expect(described_class.new(nil, false, [existing]).obsolete_indexes).to eq([])
34
+ end
35
+
36
+ it 'identifies a search index whose keys are in the wrong order' do
37
+ existing = { 'key' => { 'score' => -1, 'ngram' => 1 } }
38
+ expect(described_class.new(nil, false, [existing]).obsolete_indexes).to eq([existing])
39
+ end
40
+
41
+ it 'does not mutate existing definitions or filter configuration' do
42
+ filters = { a: true }.freeze
43
+ existing = [{ 'key' => { 'ngram' => 1, 'filter_values.z' => 1 } }]
44
+ original = Marshal.dump(existing)
45
+ described_class.new(filters, true, existing)
46
+ expect(Marshal.dump(existing)).to eq(original)
47
+ end
48
+ end
@@ -0,0 +1,73 @@
1
+ require 'rspec'
2
+ require 'mongoid/full_text_search/ngrams'
3
+
4
+ describe Mongoid::FullTextSearch::Ngrams do
5
+ let(:config) do
6
+ {
7
+ alphabet: /[\p{L}\p{M}\p{N}]/,
8
+ word_separators: { ' ' => ' ', '-' => '-' },
9
+ ngram_width: 3,
10
+ max_ngrams_to_search: 3,
11
+ remove_accents: true,
12
+ apply_prefix_scoring_to_all_words: true,
13
+ index_full_words: false,
14
+ index_short_prefixes: false,
15
+ stop_words: { 'the' => true }
16
+ }
17
+ end
18
+
19
+ it 'handles nil and strings shorter than a window' do
20
+ [nil, '', 'ab'].each { |text| expect(described_class.calculate(text, config)).to eq({}) }
21
+ end
22
+
23
+ it 'samples endpoints and preserves exact prefix and interior scores' do
24
+ expect(described_class.calculate('abcdefghijk', config)).to eq(
25
+ 'abc' => Math.sqrt(1 + 1.0 / 11),
26
+ 'efg' => Math.sqrt(2.0 / 11),
27
+ 'ijk' => Math.sqrt(2.0 / 11)
28
+ )
29
+ end
30
+
31
+ it 'uses every window for unbounded indexing' do
32
+ expect(described_class.calculate('abcdef', config, false).keys).to eq(%w[abc bcd cde def])
33
+ end
34
+
35
+ it 'keeps the maximum score of repeated windows rather than adding them' do
36
+ expect(described_class.calculate('aaaa', config, false)).to eq('aaa' => Math.sqrt(1 + 1.0 / 4))
37
+ end
38
+
39
+ it 'adds window and full-word scores for the same term only once each' do
40
+ options = config.merge(index_full_words: true)
41
+ expect(described_class.calculate('cat cat', options, false)).to eq(
42
+ 'cat' => Math.sqrt(1 + 1.0 / 7) + (1 + 1.0 / 7),
43
+ 'at ' => Math.sqrt(2.0 / 7),
44
+ 't c' => Math.sqrt(2.0 / 7),
45
+ ' ca' => Math.sqrt(2.0 / 7)
46
+ )
47
+ end
48
+
49
+ it 'combines short-prefix and full-word bonuses without duplicate prefixes' do
50
+ options = config.merge(ngram_width: 4, index_short_prefixes: true, index_full_words: true)
51
+ ngrams = described_class.calculate('cat cats', options, false)
52
+ expect(ngrams['cat']).to eq(2 * (1 + 1.0 / 8))
53
+ end
54
+
55
+ it 'suppresses stop-word bonuses unless the word is the whole input' do
56
+ options = config.merge(index_full_words: true, index_short_prefixes: true)
57
+ expect(described_class.calculate('the cat', options)['th']).to be_nil
58
+ expect(described_class.calculate('the cat', options)['the']).to eq(Math.sqrt(1 + 1.0 / 7))
59
+ expect(described_class.calculate('the', options)['th']).to eq(1 + 1.0 / 3)
60
+ end
61
+
62
+ it 'normalizes Unicode and preserves configured separators' do
63
+ expect(described_class.calculate("CAF\u00c9-\u4e2d\u6587", config).keys).to eq(['caf', "e\u0301-", "-\u4e2d\u6587"])
64
+ end
65
+
66
+ it 'does not mutate configuration or source text' do
67
+ text = 'Cat'.freeze
68
+ original = Marshal.dump(config)
69
+ described_class.calculate(text, config.freeze)
70
+ expect(Marshal.dump(config)).to eq(original)
71
+ expect(text).to eq('Cat')
72
+ end
73
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::FullTextSearch do
4
+ it 'defines a semantic version' do
5
+ expect(Mongoid::FullTextSearch::VERSION).to match(/\A\d+\.\d+\.\d+\z/)
6
+ end
7
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_fulltext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.2
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Windsor
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-08 00:00:00.000000000 Z
11
+ date: 2026-11-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongoid
@@ -19,7 +19,7 @@ dependencies:
19
19
  version: '3.0'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '8'
22
+ version: '10'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -29,7 +29,7 @@ dependencies:
29
29
  version: '3.0'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '8'
32
+ version: '10'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: mongoid-compatibility
35
35
  requirement: !ruby/object:Gem::Requirement
@@ -72,34 +72,46 @@ dependencies:
72
72
  - - ">="
73
73
  - !ruby/object:Gem::Version
74
74
  version: '0'
75
- description:
75
+ description:
76
76
  email: aaron.windsor@gmail.com
77
77
  executables: []
78
78
  extensions: []
79
79
  extra_rdoc_files: []
80
80
  files:
81
81
  - ".document"
82
+ - ".github/workflows/danger-comment.yml"
83
+ - ".github/workflows/danger.yml"
84
+ - ".github/workflows/lint.yml"
85
+ - ".github/workflows/release.yml"
86
+ - ".github/workflows/test.yml"
82
87
  - ".gitignore"
83
88
  - ".rspec"
84
89
  - ".rubocop.yml"
85
90
  - ".rubocop_todo.yml"
86
- - ".travis.yml"
91
+ - ".spellcheck.txt"
87
92
  - CHANGELOG.md
88
93
  - Dangerfile
89
94
  - Gemfile
95
+ - Gemfile.danger
90
96
  - LICENSE
91
97
  - README.md
92
98
  - RELEASING.md
93
99
  - Rakefile
100
+ - UPGRADING.md
94
101
  - lib/mongoid/full_text_search.rb
102
+ - lib/mongoid/full_text_search/field_scores.rb
103
+ - lib/mongoid/full_text_search/index_definition.rb
95
104
  - lib/mongoid/full_text_search/indexable.rb
96
105
  - lib/mongoid/full_text_search/indexes.rb
106
+ - lib/mongoid/full_text_search/ngrams.rb
97
107
  - lib/mongoid/full_text_search/version.rb
98
108
  - lib/mongoid_fulltext.rb
99
109
  - mongoid_fulltext.gemspec
110
+ - script/release.rb
100
111
  - spec/models/accentless_artwork.rb
101
112
  - spec/models/advanced_artwork.rb
102
113
  - spec/models/basic_artwork.rb
114
+ - spec/models/chinese_artwork.rb
103
115
  - spec/models/delayed_artwork.rb
104
116
  - spec/models/external_artist.rb
105
117
  - spec/models/external_artwork.rb
@@ -109,21 +121,40 @@ files:
109
121
  - spec/models/filtered_other.rb
110
122
  - spec/models/gallery/basic_artwork.rb
111
123
  - spec/models/hidden_dragon.rb
124
+ - spec/models/localized_artwork.rb
112
125
  - spec/models/multi_external_artwork.rb
113
126
  - spec/models/multi_field_artist.rb
114
127
  - spec/models/multi_field_artwork.rb
128
+ - spec/models/multilingual_artwork.rb
115
129
  - spec/models/partitioned_artist.rb
116
130
  - spec/models/russian_artwork.rb
131
+ - spec/models/scoped_artwork.rb
117
132
  - spec/models/short_prefixes_artwork.rb
118
133
  - spec/models/stopwords_artwork.rb
134
+ - spec/models/tagged_artwork.rb
135
+ - spec/models/weighted_artwork.rb
136
+ - spec/mongoid/array_fields_spec.rb
137
+ - spec/mongoid/bulk_indexing_spec.rb
138
+ - spec/mongoid/candidate_limits_spec.rb
139
+ - spec/mongoid/criteria_search_spec.rb
119
140
  - spec/mongoid/full_text_search_spec.rb
141
+ - spec/mongoid/localized_fields_spec.rb
142
+ - spec/mongoid/ngram_sampling_spec.rb
143
+ - spec/mongoid/non_latin_spec.rb
144
+ - spec/mongoid/timeout_option_spec.rb
145
+ - spec/mongoid/weighted_fields_spec.rb
146
+ - spec/release/release_spec.rb
120
147
  - spec/spec_helper.rb
121
148
  - spec/support/mongoid.rb
149
+ - spec/unit/field_scores_spec.rb
150
+ - spec/unit/index_definition_spec.rb
151
+ - spec/unit/ngrams_spec.rb
152
+ - spec/version_spec.rb
122
153
  homepage: https://github.com/mongoid/mongoid_fulltext
123
154
  licenses:
124
155
  - MIT
125
156
  metadata: {}
126
- post_install_message:
157
+ post_install_message:
127
158
  rdoc_options: []
128
159
  require_paths:
129
160
  - lib
@@ -138,9 +169,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
138
169
  - !ruby/object:Gem::Version
139
170
  version: 1.3.6
140
171
  requirements: []
141
- rubyforge_project:
142
- rubygems_version: 2.7.6
143
- signing_key:
172
+ rubygems_version: 3.5.22
173
+ signing_key:
144
174
  specification_version: 4
145
175
  summary: Full-text search for the Mongoid ORM, using n-grams extracted from text.
146
176
  test_files: []
data/.travis.yml DELETED
@@ -1,33 +0,0 @@
1
- sudo: false
2
-
3
- matrix:
4
- include:
5
- - rvm: 2.5.0
6
- env:
7
- - MONGOID_VERSION=7.0
8
- before_script:
9
- - bundle exec danger
10
- - rvm: 2.3.1
11
- env:
12
- - MONGOID_VERSION=6.0
13
- - rvm: 2.3.1
14
- env:
15
- - MONGOID_VERSION=5.0
16
- - rvm: 2.3.1
17
- env:
18
- - MONGOID_VERSION=4.0
19
- - rvm: 2.2
20
- env:
21
- - MONGOID_VERSION=3.1.0
22
- - rvm: 2.1
23
- env:
24
- - MONGOID_VERSION=3.0.0
25
-
26
- services: mongodb
27
-
28
- addons:
29
- apt:
30
- sources:
31
- - mongodb-3.4-precise
32
- packages:
33
- - mongodb-org-server