Commit fc9840edfc6f3335bacf5fb5448b29e42941b21b

Authored by Andrew Kane
1 parent 8447f829

Use automatic mapping

lib/searchkick.rb
... ... @@ -6,7 +6,7 @@ require "tire"
6 6  
7 7 module Searchkick
8 8 module Model
9   - def searchkick(field, options = {})
  9 + def searchkick(options = {})
10 10 custom_settings = {
11 11 analysis: {
12 12 analyzer: {
... ... @@ -15,7 +15,7 @@ module Searchkick
15 15 tokenizer: "keyword",
16 16 filter: ["lowercase", "snowball"]
17 17 },
18   - searchkick: {
  18 + default_index: {
19 19 type: "custom",
20 20 tokenizer: "standard",
21 21 # synonym should come last, after stemming and shingle
... ... @@ -55,7 +55,7 @@ module Searchkick
55 55 ignore_case: true,
56 56 synonyms: synonyms
57 57 }
58   - custom_settings[:analysis][:analyzer][:searchkick][:filter] << "searchkick_synonym"
  58 + custom_settings[:analysis][:analyzer][:default_index][:filter] << "searchkick_synonym"
59 59 custom_settings[:analysis][:analyzer][:searchkick_search][:filter].insert(-2, "searchkick_synonym")
60 60 custom_settings[:analysis][:analyzer][:searchkick_search][:filter] << "searchkick_synonym"
61 61 custom_settings[:analysis][:analyzer][:searchkick_search2][:filter] << "searchkick_synonym"
... ... @@ -70,7 +70,7 @@ module Searchkick
70 70 tire do
71 71 settings custom_settings
72 72 mapping do
73   - indexes field, analyzer: "searchkick"
  73 + # indexes field, analyzer: "searchkick"
74 74 if options[:conversions]
75 75 indexes :conversions, type: "nested" do
76 76 indexes :query, analyzer: "searchkick_keyword"
... ...
lib/searchkick/search.rb
1 1 module Searchkick
2 2 # can't check mapping for conversions since the new index may not be built
3 3 module Search
  4 + def index_types
  5 + Hash[ Product.index.mapping["product"]["properties"].map{|k, v| [k, v["type"]] } ].reject{|k, v| k == "conversions" || k[0] == "_" }
  6 + end
  7 +
4 8 def search(term, options = {})
5 9 fields = ["name"]
6 10 tire.search do
... ...
test/searchkick_test.rb
1 1 require "test_helper"
2 2  
3 3 class Product < ActiveRecord::Base
4   - searchkick :name, synonyms: [
5   - "clorox => bleach",
6   - "saranwrap => plastic wrap",
7   - "scallion => green onion",
8   - "qtip => cotton swab",
9   - "burger => hamburger",
10   - "bandaid => bandag"
11   - ], settings: {number_of_shards: 1}, conversions: true
  4 + searchkick \
  5 + synonyms: [
  6 + "clorox => bleach",
  7 + "saranwrap => plastic wrap",
  8 + "scallion => green onion",
  9 + "qtip => cotton swab",
  10 + "burger => hamburger",
  11 + "bandaid => bandag"
  12 + ],
  13 + settings: {
  14 + number_of_shards: 1
  15 + },
  16 + conversions: true
  17 +
  18 + # searchkick do
  19 + # string :name
  20 + # boolean :visible
  21 + # integer :orders_count
  22 + # end
12 23 end
13 24  
  25 +p Product.index_types
  26 +
14 27 class TestSearchkick < Minitest::Unit::TestCase
15 28  
16 29 def setup
... ... @@ -202,7 +215,7 @@ class TestSearchkick &lt; Minitest::Unit::TestCase
202 215  
203 216 def store(documents)
204 217 documents.each do |document|
205   - Product.index.store document.merge(_type: "product")
  218 + Product.index.store ({_type: "product", visible: true}).merge(document)
206 219 end
207 220 Product.index.refresh
208 221 end
... ...