Commit 02c9c51baf3923c0e320f7978a9f6793b2fcbf5f

Authored by Andrew Kane
1 parent bb24e095

Added wordnet - #250

1 ## 0.8.2 [unreleased] 1 ## 0.8.2 [unreleased]
2 2
3 - Added `async` to `callbacks` option 3 - Added `async` to `callbacks` option
  4 +- Added `wordnet` option
4 5
5 ## 0.8.1 6 ## 0.8.1
6 7
@@ -243,6 +243,27 @@ end @@ -243,6 +243,27 @@ end
243 243
244 Call `Product.reindex` after changing synonyms. 244 Call `Product.reindex` after changing synonyms.
245 245
  246 +### WordNet [master]
  247 +
  248 +Prepopulate English synonyms with the [WordNet database](http://en.wikipedia.org/wiki/WordNet).
  249 +
  250 +Download [WordNet 3.0](http://wordnetcode.princeton.edu/3.0/WNprolog-3.0.tar.gz) to each Elasticsearch server and move `wn_s.pl` to the `/var/lib` directory.
  251 +
  252 +```sh
  253 +cd /tmp
  254 +curl -o wordnet.tar.gz http://wordnetcode.princeton.edu/3.0/WNprolog-3.0.tar.gz
  255 +tar -zxvf wordnet.tar.gz
  256 +mv prolog/wn_s.pl /var/lib
  257 +```
  258 +
  259 +Tell each model to use it:
  260 +
  261 +```ruby
  262 +class Product < ActiveRecord::Base
  263 + searchkick wordnet: true
  264 +end
  265 +```
  266 +
246 ### Misspellings 267 ### Misspellings
247 268
248 By default, Searchkick handles misspelled queries by returning results with an [edit distance](http://en.wikipedia.org/wiki/Levenshtein_distance) of one. To turn off this feature, use: 269 By default, Searchkick handles misspelled queries by returning results with an [edit distance](http://en.wikipedia.org/wiki/Levenshtein_distance) of one. To turn off this feature, use:
lib/searchkick.rb
@@ -19,8 +19,10 @@ module Searchkick @@ -19,8 +19,10 @@ module Searchkick
19 19
20 class << self 20 class << self
21 attr_accessor :search_method_name 21 attr_accessor :search_method_name
  22 + attr_accessor :wordnet_path
22 end 23 end
23 self.search_method_name = :search 24 self.search_method_name = :search
  25 + self.wordnet_path = "/var/lib/wn_s.pl"
24 26
25 def self.client 27 def self.client
26 @client ||= Elasticsearch::Client.new(url: ENV["ELASTICSEARCH_URL"]) 28 @client ||= Elasticsearch::Client.new(url: ENV["ELASTICSEARCH_URL"])
lib/searchkick/reindex.rb
@@ -230,6 +230,17 @@ module Searchkick @@ -230,6 +230,17 @@ module Searchkick
230 settings[:analysis][:analyzer][:default_index][:filter] << "searchkick_synonym" 230 settings[:analysis][:analyzer][:default_index][:filter] << "searchkick_synonym"
231 end 231 end
232 232
  233 + if options[:wordnet]
  234 + settings[:analysis][:filter][:searchkick_wordnet] = {
  235 + type: "synonym",
  236 + format: "wordnet",
  237 + synonyms_path: Searchkick.wordnet_path
  238 + }
  239 +
  240 + settings[:analysis][:analyzer][:default_index][:filter].insert(4, "searchkick_wordnet")
  241 + settings[:analysis][:analyzer][:default_index][:filter] << "searchkick_wordnet"
  242 + end
  243 +
233 if options[:special_characters] == false 244 if options[:special_characters] == false
234 settings[:analysis][:analyzer].each do |analyzer, analyzer_settings| 245 settings[:analysis][:analyzer].each do |analyzer, analyzer_settings|
235 analyzer_settings[:filter].reject!{|f| f == "asciifolding" } 246 analyzer_settings[:filter].reject!{|f| f == "asciifolding" }
test/synonyms_test.rb
@@ -42,4 +42,9 @@ class TestSynonyms &lt; Minitest::Test @@ -42,4 +42,9 @@ class TestSynonyms &lt; Minitest::Test
42 assert_search "scallions", ["Green Onions"] 42 assert_search "scallions", ["Green Onions"]
43 end 43 end
44 44
  45 + def test_wordnet
  46 + store_names ["Creature", "Beast", "Dragon"], Animal
  47 + assert_search "animal", ["Creature", "Beast"], {}, Animal
  48 + end
  49 +
45 end 50 end
test/test_helper.rb
@@ -171,7 +171,11 @@ class Store @@ -171,7 +171,11 @@ class Store
171 end 171 end
172 172
173 class Animal 173 class Animal
174 - searchkick autocomplete: [:name], suggest: [:name], index_name: -> { "#{self.name.tableize}-#{Date.today.year}" } 174 + searchkick \
  175 + autocomplete: [:name],
  176 + suggest: [:name],
  177 + index_name: -> { "#{self.name.tableize}-#{Date.today.year}" },
  178 + wordnet: true
175 end 179 end
176 180
177 Product.searchkick_index.delete if Product.searchkick_index.exists? 181 Product.searchkick_index.delete if Product.searchkick_index.exists?