Commit 9ca1a262af14f34f1eba5bebb5620228aaba32b4

Authored by Andrew Kane
1 parent 841eeb86

Added filterable option

CHANGELOG.md
1 1 ## 1.3.5 [unreleased]
2 2  
3 3 - Added `request_params` option
  4 +- Added `filterable` option
4 5  
5 6 ## 1.3.4
6 7  
... ...
lib/searchkick/index.rb
... ... @@ -477,7 +477,7 @@ module Searchkick
477 477 end
478 478  
479 479 mapping_options = Hash[
480   - [:autocomplete, :suggest, :word, :text_start, :text_middle, :text_end, :word_start, :word_middle, :word_end, :highlight, :searchable, :only_analyzed]
  480 + [:autocomplete, :suggest, :word, :text_start, :text_middle, :text_end, :word_start, :word_middle, :word_end, :highlight, :searchable, :filterable, :only_analyzed]
481 481 .map { |type| [type, (options[type] || []).map(&:to_s)] }
482 482 ]
483 483  
... ... @@ -486,7 +486,7 @@ module Searchkick
486 486 mapping_options.values.flatten.uniq.each do |field|
487 487 fields = {}
488 488  
489   - if mapping_options[:only_analyzed].include?(field)
  489 + if mapping_options[:only_analyzed].include?(field) || (options.key?(:filterable) && !mapping_options[:filterable].include?(field))
490 490 fields[field] = {type: default_type, index: "no"}
491 491 else
492 492 fields[field] = keyword_mapping
... ... @@ -501,7 +501,7 @@ module Searchkick
501 501 end
502 502 end
503 503  
504   - mapping_options.except(:highlight, :searchable, :only_analyzed, :word).each do |type, f|
  504 + mapping_options.except(:highlight, :searchable, :filterable, :only_analyzed, :word).each do |type, f|
505 505 if options[:match] == type || f.include?(field)
506 506 fields[type] = {type: default_type, index: "analyzed", analyzer: "searchkick_#{type}_index"}
507 507 end
... ... @@ -548,6 +548,10 @@ module Searchkick
548 548 "{name}" => keyword_mapping.merge(include_in_all: !options[:searchable])
549 549 }
550 550  
  551 + if options.key?(:filterable)
  552 + dynamic_fields["{name}"] = {type: default_type, index: "no"}
  553 + end
  554 +
551 555 dynamic_fields["{name}"][:ignore_above] = 256 unless below22
552 556  
553 557 unless options[:searchable]
... ...
test/index_test.rb
... ... @@ -112,6 +112,11 @@ class IndexTest < Minitest::Test
112 112 end
113 113  
114 114 def test_analyzed_only
  115 + store [{name: "Product A", alt_description: "Hello"}]
  116 + assert_search "*", [], where: {alt_description: "Hello"}
  117 + end
  118 +
  119 + def test_analyzed_only_large_value
115 120 skip if nobrainer?
116 121 large_value = 10000.times.map { "hello" }.join(" ")
117 122 store [{name: "Product A", alt_description: large_value}]
... ...
test/test_helper.rb
... ... @@ -287,9 +287,10 @@ class Product
287 287 word_middle: [:name],
288 288 word_end: [:name],
289 289 highlight: [:name],
290   - # unsearchable: [:description],
291 290 searchable: [:name, :color],
292   - only_analyzed: [:alt_description],
  291 + filterable: [:name, :color, :description],
  292 + # unsearchable: [:description],
  293 + # only_analyzed: [:alt_description],
293 294 match: ENV["MATCH"] ? ENV["MATCH"].to_sym : nil
294 295  
295 296 attr_accessor :conversions, :user_ids, :aisle, :details
... ...