Commit 656cc5ddfb8e82f8f16eed3aaa488b813cf2785f

Authored by Andrew Kane
1 parent 4180044a

Added searchable option

CHANGELOG.md
... ... @@ -2,6 +2,7 @@
2 2  
3 3 - Added `below` option to misspellings to improve performance
4 4 - Fixed synonyms for `word_*` partial matches
  5 +- Added `searchable` option
5 6 - Added `similarity` option
6 7 - Added `match` option
7 8 - Added `word` option
... ...
lib/searchkick/index.rb
... ... @@ -421,7 +421,7 @@ module Searchkick
421 421 end
422 422  
423 423 mapping_options = Hash[
424   - [:autocomplete, :suggest, :word, :text_start, :text_middle, :text_end, :word_start, :word_middle, :word_end, :highlight]
  424 + [:autocomplete, :suggest, :word, :text_start, :text_middle, :text_end, :word_start, :word_middle, :word_end, :highlight, :searchable]
425 425 .map { |type| [type, (options[type] || []).map(&:to_s)] }
426 426 ]
427 427  
... ... @@ -435,18 +435,20 @@ module Searchkick
435 435 }
436 436 }
437 437  
438   - if word
439   - field_mapping[:fields]["analyzed"] = {type: "string", index: "analyzed"}
440   - end
  438 + if !options[:searchable] || mapping_options[:searchable].include?(field)
  439 + if word
  440 + field_mapping[:fields]["analyzed"] = {type: "string", index: "analyzed"}
441 441  
442   - mapping_options.except(:highlight).each do |type, fields|
443   - if options[:match] == type || fields.include?(field)
444   - field_mapping[:fields][type] = {type: "string", index: "analyzed", analyzer: "searchkick_#{type}_index"}
  442 + if mapping_options[:highlight].include?(field)
  443 + field_mapping[:fields]["analyzed"][:term_vector] = "with_positions_offsets"
  444 + end
445 445 end
446   - end
447 446  
448   - if word && mapping_options[:highlight].include?(field)
449   - field_mapping[:fields]["analyzed"][:term_vector] = "with_positions_offsets"
  447 + mapping_options.except(:highlight, :searchable).each do |type, fields|
  448 + if options[:match] == type || fields.include?(field)
  449 + field_mapping[:fields][type] = {type: "string", index: "analyzed", analyzer: "searchkick_#{type}_index"}
  450 + end
  451 + end
450 452 end
451 453  
452 454 mapping[field] = field_mapping
... ... @@ -475,15 +477,17 @@ module Searchkick
475 477 # http://www.elasticsearch.org/guide/reference/mapping/multi-field-type/
476 478 # however, we can include the not_analyzed field in _all
477 479 # and the _all index analyzer will take care of it
478   - "{name}" => {type: "string", index: "not_analyzed"}
  480 + "{name}" => {type: "string", index: "not_analyzed", include_in_all: !options[:searchable]}
479 481 }
480 482  
481   - if options[:match] && options[:match] != :word
482   - dynamic_fields[options[:match]] = {type: "string", index: "analyzed", analyzer: "searchkick_#{options[:match]}_index"}
483   - end
  483 + unless options[:searchable]
  484 + if options[:match] && options[:match] != :word
  485 + dynamic_fields[options[:match]] = {type: "string", index: "analyzed", analyzer: "searchkick_#{options[:match]}_index"}
  486 + end
484 487  
485   - if word
486   - dynamic_fields["analyzed"] = {type: "string", index: "analyzed"}
  488 + if word
  489 + dynamic_fields["analyzed"] = {type: "string", index: "analyzed"}
  490 + end
487 491 end
488 492  
489 493 mappings = {
... ...
test/match_test.rb
... ... @@ -198,6 +198,13 @@ class MatchTest < Minitest::Test
198 198 assert_search "almond", []
199 199 end
200 200  
  201 + def test_unsearchable_where
  202 + store [
  203 + {name: "Unsearchable", description: "Almond"}
  204 + ]
  205 + assert_search "*", ["Unsearchable"], where: {description: "Almond"}
  206 + end
  207 +
201 208 def test_emoji
202 209 skip unless defined?(EmojiParser)
203 210 store_names ["Banana"]
... ...
test/test_helper.rb
... ... @@ -216,7 +216,8 @@ class Product
216 216 word_middle: [:name],
217 217 word_end: [:name],
218 218 highlight: [:name],
219   - unsearchable: [:description],
  219 + # unsearchable: [:description],
  220 + searchable: [:name, :color],
220 221 match: ENV["MATCH"] ? ENV["MATCH"].to_sym : nil
221 222  
222 223 attr_accessor :conversions, :user_ids, :aisle
... ...