Commit 02c9c51baf3923c0e320f7978a9f6793b2fcbf5f

Authored by Andrew Kane
1 parent bb24e095

Added wordnet - #250

CHANGELOG.md
1 1 ## 0.8.2 [unreleased]
2 2  
3 3 - Added `async` to `callbacks` option
  4 +- Added `wordnet` option
4 5  
5 6 ## 0.8.1
6 7  
... ...
README.md
... ... @@ -243,6 +243,27 @@ end
243 243  
244 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 267 ### Misspellings
247 268  
248 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 19  
20 20 class << self
21 21 attr_accessor :search_method_name
  22 + attr_accessor :wordnet_path
22 23 end
23 24 self.search_method_name = :search
  25 + self.wordnet_path = "/var/lib/wn_s.pl"
24 26  
25 27 def self.client
26 28 @client ||= Elasticsearch::Client.new(url: ENV["ELASTICSEARCH_URL"])
... ...
lib/searchkick/reindex.rb
... ... @@ -230,6 +230,17 @@ module Searchkick
230 230 settings[:analysis][:analyzer][:default_index][:filter] << "searchkick_synonym"
231 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 244 if options[:special_characters] == false
234 245 settings[:analysis][:analyzer].each do |analyzer, analyzer_settings|
235 246 analyzer_settings[:filter].reject!{|f| f == "asciifolding" }
... ...
test/synonyms_test.rb
... ... @@ -42,4 +42,9 @@ class TestSynonyms &lt; Minitest::Test
42 42 assert_search "scallions", ["Green Onions"]
43 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 50 end
... ...
test/test_helper.rb
... ... @@ -171,7 +171,11 @@ class Store
171 171 end
172 172  
173 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 179 end
176 180  
177 181 Product.searchkick_index.delete if Product.searchkick_index.exists?
... ...