From 9ca1a262af14f34f1eba5bebb5620228aaba32b4 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Wed, 14 Sep 2016 01:27:28 -0700 Subject: [PATCH] Added filterable option --- CHANGELOG.md | 1 + lib/searchkick/index.rb | 10 +++++++--- test/index_test.rb | 5 +++++ test/test_helper.rb | 5 +++-- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 83da88d..b529bff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## 1.3.5 [unreleased] - Added `request_params` option +- Added `filterable` option ## 1.3.4 diff --git a/lib/searchkick/index.rb b/lib/searchkick/index.rb index 3123aa4..0831283 100644 --- a/lib/searchkick/index.rb +++ b/lib/searchkick/index.rb @@ -477,7 +477,7 @@ module Searchkick end mapping_options = Hash[ - [:autocomplete, :suggest, :word, :text_start, :text_middle, :text_end, :word_start, :word_middle, :word_end, :highlight, :searchable, :only_analyzed] + [:autocomplete, :suggest, :word, :text_start, :text_middle, :text_end, :word_start, :word_middle, :word_end, :highlight, :searchable, :filterable, :only_analyzed] .map { |type| [type, (options[type] || []).map(&:to_s)] } ] @@ -486,7 +486,7 @@ module Searchkick mapping_options.values.flatten.uniq.each do |field| fields = {} - if mapping_options[:only_analyzed].include?(field) + if mapping_options[:only_analyzed].include?(field) || (options.key?(:filterable) && !mapping_options[:filterable].include?(field)) fields[field] = {type: default_type, index: "no"} else fields[field] = keyword_mapping @@ -501,7 +501,7 @@ module Searchkick end end - mapping_options.except(:highlight, :searchable, :only_analyzed, :word).each do |type, f| + mapping_options.except(:highlight, :searchable, :filterable, :only_analyzed, :word).each do |type, f| if options[:match] == type || f.include?(field) fields[type] = {type: default_type, index: "analyzed", analyzer: "searchkick_#{type}_index"} end @@ -548,6 +548,10 @@ module Searchkick "{name}" => keyword_mapping.merge(include_in_all: !options[:searchable]) } + if options.key?(:filterable) + dynamic_fields["{name}"] = {type: default_type, index: "no"} + end + dynamic_fields["{name}"][:ignore_above] = 256 unless below22 unless options[:searchable] diff --git a/test/index_test.rb b/test/index_test.rb index c4af943..0556515 100644 --- a/test/index_test.rb +++ b/test/index_test.rb @@ -112,6 +112,11 @@ class IndexTest < Minitest::Test end def test_analyzed_only + store [{name: "Product A", alt_description: "Hello"}] + assert_search "*", [], where: {alt_description: "Hello"} + end + + def test_analyzed_only_large_value skip if nobrainer? large_value = 10000.times.map { "hello" }.join(" ") store [{name: "Product A", alt_description: large_value}] diff --git a/test/test_helper.rb b/test/test_helper.rb index 8b7eb7b..fe3e77e 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -287,9 +287,10 @@ class Product word_middle: [:name], word_end: [:name], highlight: [:name], - # unsearchable: [:description], searchable: [:name, :color], - only_analyzed: [:alt_description], + filterable: [:name, :color, :description], + # unsearchable: [:description], + # only_analyzed: [:alt_description], match: ENV["MATCH"] ? ENV["MATCH"].to_sym : nil attr_accessor :conversions, :user_ids, :aisle, :details -- libgit2 0.21.0