Commit bba628b0cb04eab7f55226c396645b2c028859d2
1 parent
9d49fb3f
Exists in
master
and in
5 other branches
Added stemmer_override option - #1429
Showing
4 changed files
with
39 additions
and
2 deletions
Show diff stats
CHANGELOG.md
lib/searchkick/index_options.rb
... | ... | @@ -296,6 +296,23 @@ module Searchkick |
296 | 296 | end |
297 | 297 | end |
298 | 298 | |
299 | + if options[:stemmer_override] | |
300 | + stemmer_override = { | |
301 | + type: "stemmer_override" | |
302 | + } | |
303 | + if options[:stemmer_override].is_a?(String) | |
304 | + stemmer_override[:rules_path] = options[:stemmer_override] | |
305 | + else | |
306 | + stemmer_override[:rules] = options[:stemmer_override] | |
307 | + end | |
308 | + settings[:analysis][:filter][:searchkick_stemmer_override] = stemmer_override | |
309 | + | |
310 | + settings[:analysis][:analyzer].each do |_, analyzer| | |
311 | + stemmer_index = analyzer[:filter].index("searchkick_stemmer") if analyzer[:filter] | |
312 | + analyzer[:filter].insert(stemmer_index, "searchkick_stemmer_override") if stemmer_index | |
313 | + end | |
314 | + end | |
315 | + | |
299 | 316 | if options[:stem_exclusion] |
300 | 317 | settings[:analysis][:filter][:searchkick_stem_exclusion] = { |
301 | 318 | type: "keyword_marker", | ... | ... |
lib/searchkick/model.rb
... | ... | @@ -6,7 +6,7 @@ module Searchkick |
6 | 6 | unknown_keywords = options.keys - [:_all, :_type, :batch_size, :callbacks, :case_sensitive, :conversions, :deep_paging, :default_fields, |
7 | 7 | :filterable, :geo_shape, :highlight, :ignore_above, :index_name, :index_prefix, :inheritance, :language, |
8 | 8 | :locations, :mappings, :match, :merge_mappings, :routing, :searchable, :search_synonyms, :settings, :similarity, |
9 | - :special_characters, :stem, :stem_conversions, :stem_exclusion, :suggest, :synonyms, :text_end, | |
9 | + :special_characters, :stem, :stem_conversions, :stem_exclusion, :stemmer_override, :suggest, :synonyms, :text_end, | |
10 | 10 | :text_middle, :text_start, :word, :wordnet, :word_end, :word_middle, :word_start] |
11 | 11 | raise ArgumentError, "unknown keywords: #{unknown_keywords.join(", ")}" if unknown_keywords.any? |
12 | 12 | ... | ... |
test/index_options_test.rb
... | ... | @@ -39,6 +39,26 @@ class IndexOptionsTest < Minitest::Test |
39 | 39 | end |
40 | 40 | end |
41 | 41 | |
42 | + def test_no_stemmer_override | |
43 | + with_options({}) do | |
44 | + store_names ["animals", "animations"] | |
45 | + assert_search "animals", ["animals", "animations"], {misspellings: false} | |
46 | + assert_search "animations", ["animals", "animations"], {misspellings: false} | |
47 | + assert_equal ["anim"], Song.search_index.tokens("animations", analyzer: "searchkick_index") | |
48 | + assert_equal ["anim"], Song.search_index.tokens("animations", analyzer: "searchkick_search2") | |
49 | + end | |
50 | + end | |
51 | + | |
52 | + def test_stemmer_override | |
53 | + with_options({stemmer_override: ["animations => animat"]}) do | |
54 | + store_names ["animals", "animations"] | |
55 | + assert_search "animals", ["animals"], {misspellings: false} | |
56 | + assert_search "animations", ["animations"], {misspellings: false} | |
57 | + assert_equal ["animat"], Song.search_index.tokens("animations", analyzer: "searchkick_index") | |
58 | + assert_equal ["animat"], Song.search_index.tokens("animations", analyzer: "searchkick_search2") | |
59 | + end | |
60 | + end | |
61 | + | |
42 | 62 | def test_special_characters |
43 | 63 | with_options({special_characters: false}) do |
44 | 64 | store_names ["jalapeño"] | ... | ... |