From fc9840edfc6f3335bacf5fb5448b29e42941b21b Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Tue, 16 Jul 2013 01:11:57 -0700 Subject: [PATCH] Use automatic mapping --- lib/searchkick.rb | 8 ++++---- lib/searchkick/search.rb | 4 ++++ test/searchkick_test.rb | 31 ++++++++++++++++++++++--------- 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/lib/searchkick.rb b/lib/searchkick.rb index ae58b2a..e9f29bc 100644 --- a/lib/searchkick.rb +++ b/lib/searchkick.rb @@ -6,7 +6,7 @@ require "tire" module Searchkick module Model - def searchkick(field, options = {}) + def searchkick(options = {}) custom_settings = { analysis: { analyzer: { @@ -15,7 +15,7 @@ module Searchkick tokenizer: "keyword", filter: ["lowercase", "snowball"] }, - searchkick: { + default_index: { type: "custom", tokenizer: "standard", # synonym should come last, after stemming and shingle @@ -55,7 +55,7 @@ module Searchkick ignore_case: true, synonyms: synonyms } - custom_settings[:analysis][:analyzer][:searchkick][:filter] << "searchkick_synonym" + custom_settings[:analysis][:analyzer][:default_index][:filter] << "searchkick_synonym" custom_settings[:analysis][:analyzer][:searchkick_search][:filter].insert(-2, "searchkick_synonym") custom_settings[:analysis][:analyzer][:searchkick_search][:filter] << "searchkick_synonym" custom_settings[:analysis][:analyzer][:searchkick_search2][:filter] << "searchkick_synonym" @@ -70,7 +70,7 @@ module Searchkick tire do settings custom_settings mapping do - indexes field, analyzer: "searchkick" + # indexes field, analyzer: "searchkick" if options[:conversions] indexes :conversions, type: "nested" do indexes :query, analyzer: "searchkick_keyword" diff --git a/lib/searchkick/search.rb b/lib/searchkick/search.rb index 6e1ab3b..b9110d1 100644 --- a/lib/searchkick/search.rb +++ b/lib/searchkick/search.rb @@ -1,6 +1,10 @@ module Searchkick # can't check mapping for conversions since the new index may not be built module Search + def index_types + Hash[ Product.index.mapping["product"]["properties"].map{|k, v| [k, v["type"]] } ].reject{|k, v| k == "conversions" || k[0] == "_" } + end + def search(term, options = {}) fields = ["name"] tire.search do diff --git a/test/searchkick_test.rb b/test/searchkick_test.rb index 5311bac..5553522 100644 --- a/test/searchkick_test.rb +++ b/test/searchkick_test.rb @@ -1,16 +1,29 @@ require "test_helper" class Product < ActiveRecord::Base - searchkick :name, synonyms: [ - "clorox => bleach", - "saranwrap => plastic wrap", - "scallion => green onion", - "qtip => cotton swab", - "burger => hamburger", - "bandaid => bandag" - ], settings: {number_of_shards: 1}, conversions: true + searchkick \ + synonyms: [ + "clorox => bleach", + "saranwrap => plastic wrap", + "scallion => green onion", + "qtip => cotton swab", + "burger => hamburger", + "bandaid => bandag" + ], + settings: { + number_of_shards: 1 + }, + conversions: true + + # searchkick do + # string :name + # boolean :visible + # integer :orders_count + # end end +p Product.index_types + class TestSearchkick < Minitest::Unit::TestCase def setup @@ -202,7 +215,7 @@ class TestSearchkick < Minitest::Unit::TestCase def store(documents) documents.each do |document| - Product.index.store document.merge(_type: "product") + Product.index.store ({_type: "product", visible: true}).merge(document) end Product.index.refresh end -- libgit2 0.21.0