Commit ea27d5303f85936deae7779c80f9e0e69e95acb6

Authored by Andrew Kane
1 parent 322c7d86

Handle synonyms gracefully

Showing 2 changed files with 23 additions and 21 deletions   Show diff stats
lib/searchkick.rb
... ... @@ -71,8 +71,9 @@ module Searchkick
71 71  
72 72 # TODO fix this monstrosity
73 73 # TODO add custom synonyms
74   - def self.settings
75   - {
  74 + def self.settings(options = {})
  75 + synonyms = options[:synonyms] || []
  76 + settings = {
76 77 analysis: {
77 78 analyzer: {
78 79 searchkick_keyword: {
... ... @@ -85,17 +86,12 @@ module Searchkick
85 86 tokenizer: "standard",
86 87 # synonym should come last, after stemming and shingle
87 88 # shingle must come before snowball
88   - filter: ["standard", "lowercase", "asciifolding", "stop", "searchkick_index_shingle", "snowball", "searchkick_synonym"]
89   - # filter: ["standard", "lowercase", "asciifolding", "stop", "snowball"]
  89 + filter: ["standard", "lowercase", "asciifolding", "stop", "searchkick_index_shingle", "snowball"]
90 90 },
91   - # lucky find http://web.archiveorange.com/archive/v/AAfXfQ17f57FcRINsof7
92 91 searchkick_search: {
93 92 type: "custom",
94 93 tokenizer: "standard",
95   - # synonym should come last, after stemming and shingle
96   - # shingle must come before snowball
97   - # filter: ["standard", "lowercase", "asciifolding", "stop", "searchkick_shingle", "snowball", "searchkick_synonym"]
98   - filter: ["standard", "lowercase", "asciifolding", "stop", "searchkick_search_shingle", "snowball", "searchkick_synonym"]
  94 + filter: ["standard", "lowercase", "asciifolding", "stop", "searchkick_search_shingle", "snowball"]
99 95 }
100 96 },
101 97 filter: {
... ... @@ -103,26 +99,26 @@ module Searchkick
103 99 type: "shingle",
104 100 token_separator: ""
105 101 },
  102 + # lucky find http://web.archiveorange.com/archive/v/AAfXfQ17f57FcRINsof7
106 103 searchkick_search_shingle: {
107 104 type: "shingle",
108 105 token_separator: "",
109 106 output_unigrams: false,
110 107 output_unigrams_if_no_shingles: true
111   - },
112   - searchkick_synonym: {
113   - type: "synonym",
114   - ignore_case: true,
115   - # tokenizer: "keyword",
116   - synonyms: [
117   - "clorox => bleach",
118   - "saranwrap => plastic wrap",
119   - "scallion => green onion",
120   - "qtip => cotton swab"
121   - ]
122 108 }
123 109 }
124 110 }
125 111 }
  112 + if synonyms.any?
  113 + settings[:analysis][:filter][:searchkick_synonym] = {
  114 + type: "synonym",
  115 + ignore_case: true,
  116 + synonyms: synonyms
  117 + }
  118 + settings[:analysis][:analyzer][:searchkick][:filter] << "searchkick_synonym"
  119 + settings[:analysis][:analyzer][:searchkick_search][:filter] << "searchkick_synonym"
  120 + end
  121 + settings
126 122 end
127 123  
128 124 end
... ...
test/searchkick_test.rb
... ... @@ -5,8 +5,14 @@ class TestSearchkick &lt; Minitest::Unit::TestCase
5 5 def setup
6 6 $index = Tire::Index.new("products")
7 7 $index.delete
  8 + synonyms = [
  9 + "clorox => bleach",
  10 + "saranwrap => plastic wrap",
  11 + "scallion => green onion",
  12 + "qtip => cotton swab"
  13 + ]
8 14 index_options = {
9   - settings: Searchkick.settings.merge(number_of_shards: 1),
  15 + settings: Searchkick.settings(synonyms: synonyms).merge(number_of_shards: 1),
10 16 mappings: {
11 17 document: {
12 18 properties: {
... ...