diff --git a/CHANGELOG.md b/CHANGELOG.md index 134d44a..6819450 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ ## 4.4.1 (unreleased) -- Added `stem_exclusion` option +- Added `stem_exclusion` and `stemmer_override` options - Improved error message for `reload_synonyms` with non-OSS version of Elasticsearch - Improved output for reindex rake task diff --git a/lib/searchkick/index_options.rb b/lib/searchkick/index_options.rb index a9ca359..b86dd17 100644 --- a/lib/searchkick/index_options.rb +++ b/lib/searchkick/index_options.rb @@ -296,6 +296,23 @@ module Searchkick end end + if options[:stemmer_override] + stemmer_override = { + type: "stemmer_override" + } + if options[:stemmer_override].is_a?(String) + stemmer_override[:rules_path] = options[:stemmer_override] + else + stemmer_override[:rules] = options[:stemmer_override] + end + settings[:analysis][:filter][:searchkick_stemmer_override] = stemmer_override + + settings[:analysis][:analyzer].each do |_, analyzer| + stemmer_index = analyzer[:filter].index("searchkick_stemmer") if analyzer[:filter] + analyzer[:filter].insert(stemmer_index, "searchkick_stemmer_override") if stemmer_index + end + end + if options[:stem_exclusion] settings[:analysis][:filter][:searchkick_stem_exclusion] = { type: "keyword_marker", diff --git a/lib/searchkick/model.rb b/lib/searchkick/model.rb index 4ab2ece..fbf74a3 100644 --- a/lib/searchkick/model.rb +++ b/lib/searchkick/model.rb @@ -6,7 +6,7 @@ module Searchkick unknown_keywords = options.keys - [:_all, :_type, :batch_size, :callbacks, :case_sensitive, :conversions, :deep_paging, :default_fields, :filterable, :geo_shape, :highlight, :ignore_above, :index_name, :index_prefix, :inheritance, :language, :locations, :mappings, :match, :merge_mappings, :routing, :searchable, :search_synonyms, :settings, :similarity, - :special_characters, :stem, :stem_conversions, :stem_exclusion, :suggest, :synonyms, :text_end, + :special_characters, :stem, :stem_conversions, :stem_exclusion, :stemmer_override, :suggest, :synonyms, :text_end, :text_middle, :text_start, :word, :wordnet, :word_end, :word_middle, :word_start] raise ArgumentError, "unknown keywords: #{unknown_keywords.join(", ")}" if unknown_keywords.any? diff --git a/test/index_options_test.rb b/test/index_options_test.rb index adcc8ad..ba97e97 100644 --- a/test/index_options_test.rb +++ b/test/index_options_test.rb @@ -39,6 +39,26 @@ class IndexOptionsTest < Minitest::Test end end + def test_no_stemmer_override + with_options({}) do + store_names ["animals", "animations"] + assert_search "animals", ["animals", "animations"], {misspellings: false} + assert_search "animations", ["animals", "animations"], {misspellings: false} + assert_equal ["anim"], Song.search_index.tokens("animations", analyzer: "searchkick_index") + assert_equal ["anim"], Song.search_index.tokens("animations", analyzer: "searchkick_search2") + end + end + + def test_stemmer_override + with_options({stemmer_override: ["animations => animat"]}) do + store_names ["animals", "animations"] + assert_search "animals", ["animals"], {misspellings: false} + assert_search "animations", ["animations"], {misspellings: false} + assert_equal ["animat"], Song.search_index.tokens("animations", analyzer: "searchkick_index") + assert_equal ["animat"], Song.search_index.tokens("animations", analyzer: "searchkick_search2") + end + end + def test_special_characters with_options({special_characters: false}) do store_names ["jalapeƱo"] -- libgit2 0.21.0