Commit 19e35fdab2e7acdf46b59418d2ec6a85c4042f16

Authored by Andrew Kane
1 parent aff7c385

Added only_analyzed option - #429

lib/searchkick/index.rb
... ... @@ -408,7 +408,7 @@ module Searchkick
408 408 end
409 409  
410 410 mapping_options = Hash[
411   - [:autocomplete, :suggest, :word, :text_start, :text_middle, :text_end, :word_start, :word_middle, :word_end, :highlight, :searchable]
  411 + [:autocomplete, :suggest, :word, :text_start, :text_middle, :text_end, :word_start, :word_middle, :word_end, :highlight, :searchable, :only_analyzed]
412 412 .map { |type| [type, (options[type] || []).map(&:to_s)] }
413 413 ]
414 414  
... ... @@ -417,11 +417,13 @@ module Searchkick
417 417 mapping_options.values.flatten.uniq.each do |field|
418 418 field_mapping = {
419 419 type: "multi_field",
420   - fields: {
421   - field => {type: "string", index: "not_analyzed"}
422   - }
  420 + fields: {}
423 421 }
424 422  
  423 + unless mapping_options[:only_analyzed].include?(field)
  424 + field_mapping[:fields][field] = {type: "string", index: "not_analyzed"}
  425 + end
  426 +
425 427 if !options[:searchable] || mapping_options[:searchable].include?(field)
426 428 if word
427 429 field_mapping[:fields]["analyzed"] = {type: "string", index: "analyzed"}
... ... @@ -431,7 +433,7 @@ module Searchkick
431 433 end
432 434 end
433 435  
434   - mapping_options.except(:highlight, :searchable).each do |type, fields|
  436 + mapping_options.except(:highlight, :searchable, :only_analyzed).each do |type, fields|
435 437 if options[:match] == type || fields.include?(field)
436 438 field_mapping[:fields][type] = {type: "string", index: "analyzed", analyzer: "searchkick_#{type}_index"}
437 439 end
... ...
test/index_test.rb
... ... @@ -110,4 +110,15 @@ class IndexTest < Minitest::Test
110 110 end
111 111 assert_search "product", []
112 112 end
  113 +
  114 + def test_large_text
  115 + large_value = 10000.times.map { "hello" }.join(" ")
  116 + assert_raises { store_names[large_value] }
  117 + end
  118 +
  119 + def test_analyzed_only
  120 + large_value = 10000.times.map { "hello" }.join(" ")
  121 + store [{name: "Product A", alt_description: large_value}]
  122 + assert_search "product", ["Product A"]
  123 + end
113 124 end
... ...
test/test_helper.rb
... ... @@ -73,6 +73,7 @@ if defined?(Mongoid)
73 73 field :latitude, type: BigDecimal
74 74 field :longitude, type: BigDecimal
75 75 field :description
  76 + field :alt_description
76 77 end
77 78  
78 79 class Store
... ... @@ -114,6 +115,7 @@ elsif defined?(NoBrainer)
114 115 field :latitude
115 116 field :longitude
116 117 field :description, type: String
  118 + field :alt_description, type: String
117 119  
118 120 belongs_to :store, validates: false
119 121 end
... ... @@ -164,6 +166,7 @@ else
164 166 t.decimal :latitude, precision: 10, scale: 7
165 167 t.decimal :longitude, precision: 10, scale: 7
166 168 t.text :description
  169 + t.text :alt_description
167 170 t.timestamps null: true
168 171 end
169 172  
... ... @@ -219,6 +222,7 @@ class Product
219 222 highlight: [:name],
220 223 # unsearchable: [:description],
221 224 searchable: [:name, :color],
  225 + only_analyzed: [:alt_description],
222 226 match: ENV["MATCH"] ? ENV["MATCH"].to_sym : nil
223 227  
224 228 attr_accessor :conversions, :user_ids, :aisle
... ...