diff --git a/lib/searchkick/reindex.rb b/lib/searchkick/reindex.rb index 980767f..fa2b1d1 100644 --- a/lib/searchkick/reindex.rb +++ b/lib/searchkick/reindex.rb @@ -125,6 +125,11 @@ module Searchkick tokenizer: "keyword", filter: ["lowercase", "asciifolding"] }, + searchkick_word_search: { + type: "custom", + tokenizer: "standard", + filter: ["lowercase", "asciifolding"] + }, searchkick_suggest_index: { type: "custom", tokenizer: "standard", diff --git a/lib/searchkick/search.rb b/lib/searchkick/search.rb index 44c20b2..97f28ce 100644 --- a/lib/searchkick/search.rb +++ b/lib/searchkick/search.rb @@ -92,11 +92,12 @@ module Searchkick ] end else + analyzer = field.match(/\.word_(start|middle|end)\z/) ? "searchkick_word_search" : "searchkick_autocomplete_search" queries << { multi_match: { fields: [field], query: term, - analyzer: "searchkick_autocomplete_search" + analyzer: analyzer } } end diff --git a/test/autocomplete_test.rb b/test/autocomplete_test.rb index a5312f2..6566194 100644 --- a/test/autocomplete_test.rb +++ b/test/autocomplete_test.rb @@ -18,33 +18,38 @@ class TestAutocomplete < Minitest::Unit::TestCase end def test_text_start - store_names ["Where in the World is Carmen San Diego?"] - assert_search "whe", ["Where in the World is Carmen San Diego?"], fields: [{name: :text_start}] + store_names ["Where in the World is Carmen San Diego"] + assert_search "where in the world is", ["Where in the World is Carmen San Diego"], fields: [{name: :text_start}] + assert_search "in the world", [], fields: [{name: :text_start}] end def test_text_middle - store_names ["Where in the World is Carmen San Diego?"] - assert_search "n the wor", ["Where in the World is Carmen San Diego?"], fields: [{name: :text_middle}] + store_names ["Where in the World is Carmen San Diego"] + assert_search "where in the world is", ["Where in the World is Carmen San Diego"], fields: [{name: :text_middle}] + assert_search "n the wor", ["Where in the World is Carmen San Diego"], fields: [{name: :text_middle}] + assert_search "men san diego", ["Where in the World is Carmen San Diego"], fields: [{name: :text_middle}] + assert_search "world carmen", [], fields: [{name: :text_middle}] end def test_text_end - store_names ["Where in the World is Carmen San Diego?"] - assert_search "ego?", ["Where in the World is Carmen San Diego?"], fields: [{name: :text_end}] + store_names ["Where in the World is Carmen San Diego"] + assert_search "men san diego", ["Where in the World is Carmen San Diego"], fields: [{name: :text_end}] + assert_search "carmen san", [], fields: [{name: :text_end}] end def test_word_start - store_names ["Where in the World is Carmen San Diego?"] - assert_search "car", ["Where in the World is Carmen San Diego?"], fields: [{name: :word_start}] + store_names ["Where in the World is Carmen San Diego"] + assert_search "car san wor", ["Where in the World is Carmen San Diego"], fields: [{name: :word_start}] end def test_word_middle - store_names ["Where in the World is Carmen San Diego?"] - assert_search "orl", ["Where in the World is Carmen San Diego?"], fields: [{name: :word_middle}] + store_names ["Where in the World is Carmen San Diego"] + assert_search "orl", ["Where in the World is Carmen San Diego"], fields: [{name: :word_middle}] end def test_word_end - store_names ["Where in the World is Carmen San Diego?"] - assert_search "men", ["Where in the World is Carmen San Diego?"], fields: [{name: :word_end}] + store_names ["Where in the World is Carmen San Diego"] + assert_search "rld men ego", ["Where in the World is Carmen San Diego"], fields: [{name: :word_end}] end end -- libgit2 0.21.0