Commit 9507809f4135ca913c6b0a0a8c55316ba8653a51

Authored by Andrew Kane
1 parent d821021f

Better synonyms

README.md
... ... @@ -45,7 +45,7 @@ Product.search "2% Milk", facets: [:store_id, :aisle_id]
45 45  
46 46 ```ruby
47 47 class Product < ActiveRecord::Base
48   - searchkick synonyms: ["scallion => green onion"] # TODO Ruby syntax
  48 + searchkick synonyms: [["scallion", "green onion"], ["qtip", "cotton swab"]]
49 49 end
50 50 ```
51 51  
... ...
lib/searchkick.rb
... ... @@ -53,7 +53,7 @@ module Searchkick
53 53 custom_settings[:analysis][:filter][:searchkick_synonym] = {
54 54 type: "synonym",
55 55 ignore_case: true,
56   - synonyms: synonyms
  56 + synonyms: synonyms.map{|s| s.join(" => ") }
57 57 }
58 58 custom_settings[:analysis][:analyzer][:default_index][:filter] << "searchkick_synonym"
59 59 custom_settings[:analysis][:analyzer][:searchkick_search][:filter].insert(-2, "searchkick_synonym")
... ...
lib/searchkick/search.rb
... ... @@ -2,7 +2,7 @@ module Searchkick
2 2 # can't check mapping for conversions since the new index may not be built
3 3 module Search
4 4 def index_types
5   - Hash[ Product.index.mapping["product"]["properties"].map{|k, v| [k, v["type"]] } ].reject{|k, v| k == "conversions" || k[0] == "_" }
  5 + Hash[ (((Product.index.mapping || {})["product"] || {})["properties"] || {}).map{|k, v| [k, v["type"]] } ].reject{|k, v| k == "conversions" || k[0] == "_" }
6 6 end
7 7  
8 8 def search(term, options = {})
... ...
test/searchkick_test.rb
... ... @@ -3,12 +3,12 @@ require &quot;test_helper&quot;
3 3 class Product < ActiveRecord::Base
4 4 searchkick \
5 5 synonyms: [
6   - "clorox => bleach",
7   - "saranwrap => plastic wrap",
8   - "scallion => green onion",
9   - "qtip => cotton swab",
10   - "burger => hamburger",
11   - "bandaid => bandag"
  6 + ["clorox", "bleach"],
  7 + ["scallion", "greenonion"],
  8 + ["saranwrap", "plasticwrap"],
  9 + ["qtip", "cotton swab"],
  10 + ["burger", "hamburger"],
  11 + ["bandaid", "bandag"]
12 12 ],
13 13 settings: {
14 14 number_of_shards: 1
... ...