mongoid_fulltext 0.8.0 → 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.
- checksums.yaml +5 -5
- data/.github/workflows/danger-comment.yml +19 -0
- data/.github/workflows/danger.yml +21 -0
- data/.github/workflows/lint.yml +29 -0
- data/.github/workflows/release.yml +30 -0
- data/.github/workflows/test.yml +81 -0
- data/.gitignore +2 -0
- data/.rubocop.yml +149 -3
- data/.rubocop_todo.yml +131 -51
- data/.spellcheck.txt +181 -0
- data/CHANGELOG.md +57 -31
- data/Dangerfile +5 -1
- data/Gemfile +22 -18
- data/Gemfile.danger +5 -0
- data/README.md +171 -179
- data/RELEASING.md +60 -0
- data/Rakefile +21 -3
- data/UPGRADING.md +43 -0
- data/lib/mongoid/full_text_search/field_scores.rb +48 -0
- data/lib/mongoid/full_text_search/index_definition.rb +31 -0
- data/lib/mongoid/full_text_search/indexable.rb +1 -1
- data/lib/mongoid/full_text_search/indexes.rb +1 -1
- data/lib/mongoid/full_text_search/ngrams.rb +93 -0
- data/lib/mongoid/full_text_search/version.rb +1 -1
- data/lib/mongoid/full_text_search.rb +112 -144
- data/mongoid_fulltext.gemspec +4 -4
- data/script/release.rb +13 -0
- data/spec/models/chinese_artwork.rb +7 -0
- data/spec/models/filtered_artist.rb +1 -2
- data/spec/models/filtered_artwork.rb +1 -2
- data/spec/models/filtered_other.rb +1 -2
- data/spec/models/localized_artwork.rb +29 -0
- data/spec/models/multilingual_artwork.rb +7 -0
- data/spec/models/partitioned_artist.rb +1 -1
- data/spec/models/russian_artwork.rb +1 -2
- data/spec/models/scoped_artwork.rb +14 -0
- data/spec/models/tagged_artwork.rb +9 -0
- data/spec/models/weighted_artwork.rb +34 -0
- data/spec/mongoid/array_fields_spec.rb +43 -0
- data/spec/mongoid/bulk_indexing_spec.rb +56 -0
- data/spec/mongoid/candidate_limits_spec.rb +19 -0
- data/spec/mongoid/criteria_search_spec.rb +73 -0
- data/spec/mongoid/full_text_search_spec.rb +41 -12
- data/spec/mongoid/localized_fields_spec.rb +197 -0
- data/spec/mongoid/ngram_sampling_spec.rb +55 -0
- data/spec/mongoid/non_latin_spec.rb +50 -0
- data/spec/mongoid/timeout_option_spec.rb +18 -0
- data/spec/mongoid/weighted_fields_spec.rb +102 -0
- data/spec/release/release_spec.rb +60 -0
- data/spec/spec_helper.rb +22 -7
- data/spec/support/mongoid.rb +6 -4
- data/spec/unit/field_scores_spec.rb +54 -0
- data/spec/unit/index_definition_spec.rb +48 -0
- data/spec/unit/ngrams_spec.rb +73 -0
- data/spec/version_spec.rb +7 -0
- metadata +62 -11
- data/.travis.yml +0 -30
|
@@ -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
|
data/spec/spec_helper.rb
CHANGED
|
@@ -1,29 +1,44 @@
|
|
|
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'
|
|
14
|
+
require RUBY_VERSION >= '2.7' ? 'database_cleaner/mongoid' : 'database_cleaner'
|
|
6
15
|
|
|
7
16
|
ENV['MONGOID_ENV'] = 'test'
|
|
8
17
|
|
|
9
|
-
require File.expand_path('
|
|
18
|
+
require File.expand_path('../lib/mongoid_fulltext', __dir__)
|
|
10
19
|
|
|
11
20
|
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
|
12
21
|
Dir["#{File.dirname(__FILE__)}/models/**/*.rb"].each { |f| require f }
|
|
13
22
|
|
|
23
|
+
DatabaseCleaner[:mongoid].strategy = RUBY_VERSION >= '2.7' ? :deletion : :truncation
|
|
24
|
+
|
|
25
|
+
Mongoid.logger.level = Logger::INFO
|
|
26
|
+
Mongo::Logger.logger.level = Logger::INFO if Mongoid::Compatibility::Version.mongoid5_or_newer?
|
|
27
|
+
|
|
14
28
|
Mongoid.configure do |config|
|
|
15
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
|
|
16
35
|
end
|
|
17
36
|
|
|
18
37
|
RSpec.configure do |c|
|
|
19
38
|
c.before :each do
|
|
20
|
-
|
|
39
|
+
DatabaseCleaner.clean
|
|
21
40
|
end
|
|
22
41
|
c.after :all do
|
|
23
|
-
|
|
24
|
-
end
|
|
25
|
-
c.before :all do
|
|
26
|
-
Mongoid.logger.level = Logger::INFO
|
|
27
|
-
Mongo::Logger.logger.level = Logger::INFO if Mongoid::Compatibility::Version.mongoid5? || Mongoid::Compatibility::Version.mongoid6?
|
|
42
|
+
DatabaseCleaner.clean
|
|
28
43
|
end
|
|
29
44
|
end
|
data/spec/support/mongoid.rb
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
if Mongoid::Compatibility::Version.mongoid5_or_newer?
|
|
2
|
+
module Mongoid
|
|
3
|
+
def self.default_session
|
|
4
|
+
default_client
|
|
5
|
+
end
|
|
4
6
|
end
|
|
5
|
-
end
|
|
7
|
+
end
|
|
@@ -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
|
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.
|
|
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:
|
|
11
|
+
date: 2026-11-24 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: mongoid
|
|
@@ -17,6 +17,9 @@ dependencies:
|
|
|
17
17
|
- - ">="
|
|
18
18
|
- !ruby/object:Gem::Version
|
|
19
19
|
version: '3.0'
|
|
20
|
+
- - "<"
|
|
21
|
+
- !ruby/object:Gem::Version
|
|
22
|
+
version: '10'
|
|
20
23
|
type: :runtime
|
|
21
24
|
prerelease: false
|
|
22
25
|
version_requirements: !ruby/object:Gem::Requirement
|
|
@@ -24,20 +27,23 @@ dependencies:
|
|
|
24
27
|
- - ">="
|
|
25
28
|
- !ruby/object:Gem::Version
|
|
26
29
|
version: '3.0'
|
|
30
|
+
- - "<"
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '10'
|
|
27
33
|
- !ruby/object:Gem::Dependency
|
|
28
34
|
name: mongoid-compatibility
|
|
29
35
|
requirement: !ruby/object:Gem::Requirement
|
|
30
36
|
requirements:
|
|
31
37
|
- - ">="
|
|
32
38
|
- !ruby/object:Gem::Version
|
|
33
|
-
version:
|
|
39
|
+
version: 0.5.1
|
|
34
40
|
type: :runtime
|
|
35
41
|
prerelease: false
|
|
36
42
|
version_requirements: !ruby/object:Gem::Requirement
|
|
37
43
|
requirements:
|
|
38
44
|
- - ">="
|
|
39
45
|
- !ruby/object:Gem::Version
|
|
40
|
-
version:
|
|
46
|
+
version: 0.5.1
|
|
41
47
|
- !ruby/object:Gem::Dependency
|
|
42
48
|
name: unicode_utils
|
|
43
49
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -52,33 +58,60 @@ dependencies:
|
|
|
52
58
|
- - ">="
|
|
53
59
|
- !ruby/object:Gem::Version
|
|
54
60
|
version: '0'
|
|
55
|
-
|
|
61
|
+
- !ruby/object:Gem::Dependency
|
|
62
|
+
name: database_cleaner
|
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
|
64
|
+
requirements:
|
|
65
|
+
- - ">="
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: '0'
|
|
68
|
+
type: :development
|
|
69
|
+
prerelease: false
|
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
71
|
+
requirements:
|
|
72
|
+
- - ">="
|
|
73
|
+
- !ruby/object:Gem::Version
|
|
74
|
+
version: '0'
|
|
75
|
+
description:
|
|
56
76
|
email: aaron.windsor@gmail.com
|
|
57
77
|
executables: []
|
|
58
78
|
extensions: []
|
|
59
79
|
extra_rdoc_files: []
|
|
60
80
|
files:
|
|
61
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"
|
|
62
87
|
- ".gitignore"
|
|
63
88
|
- ".rspec"
|
|
64
89
|
- ".rubocop.yml"
|
|
65
90
|
- ".rubocop_todo.yml"
|
|
66
|
-
- ".
|
|
91
|
+
- ".spellcheck.txt"
|
|
67
92
|
- CHANGELOG.md
|
|
68
93
|
- Dangerfile
|
|
69
94
|
- Gemfile
|
|
95
|
+
- Gemfile.danger
|
|
70
96
|
- LICENSE
|
|
71
97
|
- README.md
|
|
98
|
+
- RELEASING.md
|
|
72
99
|
- Rakefile
|
|
100
|
+
- UPGRADING.md
|
|
73
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
|
|
74
104
|
- lib/mongoid/full_text_search/indexable.rb
|
|
75
105
|
- lib/mongoid/full_text_search/indexes.rb
|
|
106
|
+
- lib/mongoid/full_text_search/ngrams.rb
|
|
76
107
|
- lib/mongoid/full_text_search/version.rb
|
|
77
108
|
- lib/mongoid_fulltext.rb
|
|
78
109
|
- mongoid_fulltext.gemspec
|
|
110
|
+
- script/release.rb
|
|
79
111
|
- spec/models/accentless_artwork.rb
|
|
80
112
|
- spec/models/advanced_artwork.rb
|
|
81
113
|
- spec/models/basic_artwork.rb
|
|
114
|
+
- spec/models/chinese_artwork.rb
|
|
82
115
|
- spec/models/delayed_artwork.rb
|
|
83
116
|
- spec/models/external_artist.rb
|
|
84
117
|
- spec/models/external_artwork.rb
|
|
@@ -88,21 +121,40 @@ files:
|
|
|
88
121
|
- spec/models/filtered_other.rb
|
|
89
122
|
- spec/models/gallery/basic_artwork.rb
|
|
90
123
|
- spec/models/hidden_dragon.rb
|
|
124
|
+
- spec/models/localized_artwork.rb
|
|
91
125
|
- spec/models/multi_external_artwork.rb
|
|
92
126
|
- spec/models/multi_field_artist.rb
|
|
93
127
|
- spec/models/multi_field_artwork.rb
|
|
128
|
+
- spec/models/multilingual_artwork.rb
|
|
94
129
|
- spec/models/partitioned_artist.rb
|
|
95
130
|
- spec/models/russian_artwork.rb
|
|
131
|
+
- spec/models/scoped_artwork.rb
|
|
96
132
|
- spec/models/short_prefixes_artwork.rb
|
|
97
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
|
|
98
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
|
|
99
147
|
- spec/spec_helper.rb
|
|
100
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
|
|
101
153
|
homepage: https://github.com/mongoid/mongoid_fulltext
|
|
102
154
|
licenses:
|
|
103
155
|
- MIT
|
|
104
156
|
metadata: {}
|
|
105
|
-
post_install_message:
|
|
157
|
+
post_install_message:
|
|
106
158
|
rdoc_options: []
|
|
107
159
|
require_paths:
|
|
108
160
|
- lib
|
|
@@ -117,9 +169,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
117
169
|
- !ruby/object:Gem::Version
|
|
118
170
|
version: 1.3.6
|
|
119
171
|
requirements: []
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
signing_key:
|
|
172
|
+
rubygems_version: 3.5.22
|
|
173
|
+
signing_key:
|
|
123
174
|
specification_version: 4
|
|
124
175
|
summary: Full-text search for the Mongoid ORM, using n-grams extracted from text.
|
|
125
176
|
test_files: []
|
data/.travis.yml
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
sudo: false
|
|
2
|
-
|
|
3
|
-
matrix:
|
|
4
|
-
include:
|
|
5
|
-
- rvm: 2.3.1
|
|
6
|
-
env:
|
|
7
|
-
- MONGOID_VERSION=6.0
|
|
8
|
-
before_script:
|
|
9
|
-
- bundle exec danger
|
|
10
|
-
- rvm: 2.3.1
|
|
11
|
-
env:
|
|
12
|
-
- MONGOID_VERSION=5.0
|
|
13
|
-
- rvm: 2.3.1
|
|
14
|
-
env:
|
|
15
|
-
- MONGOID_VERSION=4.0
|
|
16
|
-
- rvm: 2.2
|
|
17
|
-
env:
|
|
18
|
-
- MONGOID_VERSION=3.1.0
|
|
19
|
-
- rvm: 2.1
|
|
20
|
-
env:
|
|
21
|
-
- MONGOID_VERSION=3.0.0
|
|
22
|
-
|
|
23
|
-
services: mongodb
|
|
24
|
-
|
|
25
|
-
addons:
|
|
26
|
-
apt:
|
|
27
|
-
sources:
|
|
28
|
-
- mongodb-3.2-precise
|
|
29
|
-
packages:
|
|
30
|
-
- mongodb-org-server
|