Commit ea27d5303f85936deae7779c80f9e0e69e95acb6
1 parent
322c7d86
Exists in
master
and in
21 other branches
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,8 +71,9 @@ module Searchkick | ||
71 | 71 | ||
72 | # TODO fix this monstrosity | 72 | # TODO fix this monstrosity |
73 | # TODO add custom synonyms | 73 | # TODO add custom synonyms |
74 | - def self.settings | ||
75 | - { | 74 | + def self.settings(options = {}) |
75 | + synonyms = options[:synonyms] || [] | ||
76 | + settings = { | ||
76 | analysis: { | 77 | analysis: { |
77 | analyzer: { | 78 | analyzer: { |
78 | searchkick_keyword: { | 79 | searchkick_keyword: { |
@@ -85,17 +86,12 @@ module Searchkick | @@ -85,17 +86,12 @@ module Searchkick | ||
85 | tokenizer: "standard", | 86 | tokenizer: "standard", |
86 | # synonym should come last, after stemming and shingle | 87 | # synonym should come last, after stemming and shingle |
87 | # shingle must come before snowball | 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 | searchkick_search: { | 91 | searchkick_search: { |
93 | type: "custom", | 92 | type: "custom", |
94 | tokenizer: "standard", | 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 | filter: { | 97 | filter: { |
@@ -103,26 +99,26 @@ module Searchkick | @@ -103,26 +99,26 @@ module Searchkick | ||
103 | type: "shingle", | 99 | type: "shingle", |
104 | token_separator: "" | 100 | token_separator: "" |
105 | }, | 101 | }, |
102 | + # lucky find http://web.archiveorange.com/archive/v/AAfXfQ17f57FcRINsof7 | ||
106 | searchkick_search_shingle: { | 103 | searchkick_search_shingle: { |
107 | type: "shingle", | 104 | type: "shingle", |
108 | token_separator: "", | 105 | token_separator: "", |
109 | output_unigrams: false, | 106 | output_unigrams: false, |
110 | output_unigrams_if_no_shingles: true | 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 | end | 122 | end |
127 | 123 | ||
128 | end | 124 | end |
test/searchkick_test.rb
@@ -5,8 +5,14 @@ class TestSearchkick < Minitest::Unit::TestCase | @@ -5,8 +5,14 @@ class TestSearchkick < Minitest::Unit::TestCase | ||
5 | def setup | 5 | def setup |
6 | $index = Tire::Index.new("products") | 6 | $index = Tire::Index.new("products") |
7 | $index.delete | 7 | $index.delete |
8 | + synonyms = [ | ||
9 | + "clorox => bleach", | ||
10 | + "saranwrap => plastic wrap", | ||
11 | + "scallion => green onion", | ||
12 | + "qtip => cotton swab" | ||
13 | + ] | ||
8 | index_options = { | 14 | index_options = { |
9 | - settings: Searchkick.settings.merge(number_of_shards: 1), | 15 | + settings: Searchkick.settings(synonyms: synonyms).merge(number_of_shards: 1), |
10 | mappings: { | 16 | mappings: { |
11 | document: { | 17 | document: { |
12 | properties: { | 18 | properties: { |