From ea27d5303f85936deae7779c80f9e0e69e95acb6 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Sun, 14 Jul 2013 22:58:22 -0700 Subject: [PATCH] Handle synonyms gracefully --- lib/searchkick.rb | 36 ++++++++++++++++-------------------- test/searchkick_test.rb | 8 +++++++- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/lib/searchkick.rb b/lib/searchkick.rb index 94ca762..a782edd 100644 --- a/lib/searchkick.rb +++ b/lib/searchkick.rb @@ -71,8 +71,9 @@ module Searchkick # TODO fix this monstrosity # TODO add custom synonyms - def self.settings - { + def self.settings(options = {}) + synonyms = options[:synonyms] || [] + settings = { analysis: { analyzer: { searchkick_keyword: { @@ -85,17 +86,12 @@ module Searchkick tokenizer: "standard", # synonym should come last, after stemming and shingle # shingle must come before snowball - filter: ["standard", "lowercase", "asciifolding", "stop", "searchkick_index_shingle", "snowball", "searchkick_synonym"] - # filter: ["standard", "lowercase", "asciifolding", "stop", "snowball"] + filter: ["standard", "lowercase", "asciifolding", "stop", "searchkick_index_shingle", "snowball"] }, - # lucky find http://web.archiveorange.com/archive/v/AAfXfQ17f57FcRINsof7 searchkick_search: { type: "custom", tokenizer: "standard", - # synonym should come last, after stemming and shingle - # shingle must come before snowball - # filter: ["standard", "lowercase", "asciifolding", "stop", "searchkick_shingle", "snowball", "searchkick_synonym"] - filter: ["standard", "lowercase", "asciifolding", "stop", "searchkick_search_shingle", "snowball", "searchkick_synonym"] + filter: ["standard", "lowercase", "asciifolding", "stop", "searchkick_search_shingle", "snowball"] } }, filter: { @@ -103,26 +99,26 @@ module Searchkick type: "shingle", token_separator: "" }, + # lucky find http://web.archiveorange.com/archive/v/AAfXfQ17f57FcRINsof7 searchkick_search_shingle: { type: "shingle", token_separator: "", output_unigrams: false, output_unigrams_if_no_shingles: true - }, - searchkick_synonym: { - type: "synonym", - ignore_case: true, - # tokenizer: "keyword", - synonyms: [ - "clorox => bleach", - "saranwrap => plastic wrap", - "scallion => green onion", - "qtip => cotton swab" - ] } } } } + if synonyms.any? + settings[:analysis][:filter][:searchkick_synonym] = { + type: "synonym", + ignore_case: true, + synonyms: synonyms + } + settings[:analysis][:analyzer][:searchkick][:filter] << "searchkick_synonym" + settings[:analysis][:analyzer][:searchkick_search][:filter] << "searchkick_synonym" + end + settings end end diff --git a/test/searchkick_test.rb b/test/searchkick_test.rb index d2f2140..e7d61e0 100644 --- a/test/searchkick_test.rb +++ b/test/searchkick_test.rb @@ -5,8 +5,14 @@ class TestSearchkick < Minitest::Unit::TestCase def setup $index = Tire::Index.new("products") $index.delete + synonyms = [ + "clorox => bleach", + "saranwrap => plastic wrap", + "scallion => green onion", + "qtip => cotton swab" + ] index_options = { - settings: Searchkick.settings.merge(number_of_shards: 1), + settings: Searchkick.settings(synonyms: synonyms).merge(number_of_shards: 1), mappings: { document: { properties: { -- libgit2 0.21.0