Commit fc9840edfc6f3335bacf5fb5448b29e42941b21b

Authored by Andrew Kane
1 parent 8447f829

Use automatic mapping

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