Commit b8fe2f0a9b43f24b197dc788cd341ff0163c8eb2

Authored by Andrew
1 parent dee94765

Added type option

1 ## 2.3.3 [unreleased] 1 ## 2.3.3 [unreleased]
2 2
3 - Fixed `similar` for Elasticsearch 6 3 - Fixed `similar` for Elasticsearch 6
  4 +- Added `type` option
4 5
5 ## 2.3.2 6 ## 2.3.2
6 7
lib/searchkick/index.rb
@@ -322,6 +322,8 @@ module Searchkick @@ -322,6 +322,8 @@ module Searchkick
322 @klass_document_type[klass] ||= begin 322 @klass_document_type[klass] ||= begin
323 if klass.respond_to?(:document_type) 323 if klass.respond_to?(:document_type)
324 klass.document_type 324 klass.document_type
  325 + elsif klass.searchkick_klass.searchkick_options[:type]
  326 + klass.searchkick_klass.searchkick_options[:type]
325 else 327 else
326 klass.model_name.to_s.underscore 328 klass.model_name.to_s.underscore
327 end 329 end
lib/searchkick/index_options.rb
@@ -4,6 +4,8 @@ module Searchkick @@ -4,6 +4,8 @@ module Searchkick
4 options = @options 4 options = @options
5 language = options[:language] 5 language = options[:language]
6 language = language.call if language.respond_to?(:call) 6 language = language.call if language.respond_to?(:call)
  7 + type = options[:type] || :_default_
  8 + type = type.call if type.respond_to?(:call)
7 9
8 if options[:mappings] && !options[:merge_mappings] 10 if options[:mappings] && !options[:merge_mappings]
9 settings = options[:settings] || {} 11 settings = options[:settings] || {}
@@ -313,7 +315,7 @@ module Searchkick @@ -313,7 +315,7 @@ module Searchkick
313 multi_field = dynamic_fields["{name}"].merge(fields: dynamic_fields.except("{name}")) 315 multi_field = dynamic_fields["{name}"].merge(fields: dynamic_fields.except("{name}"))
314 316
315 mappings = { 317 mappings = {
316 - _default_: { 318 + type => {
317 properties: mapping, 319 properties: mapping,
318 _routing: routing, 320 _routing: routing,
319 # https://gist.github.com/kimchy/2898285 321 # https://gist.github.com/kimchy/2898285
@@ -331,7 +333,7 @@ module Searchkick @@ -331,7 +333,7 @@ module Searchkick
331 333
332 if below60 334 if below60
333 all_enabled = all && (!options[:searchable] || options[:searchable].to_a.map(&:to_s).include?("_all")) 335 all_enabled = all && (!options[:searchable] || options[:searchable].to_a.map(&:to_s).include?("_all"))
334 - mappings[:_default_][:_all] = all_enabled ? analyzed_field_options : {enabled: false} 336 + mappings[type][:_all] = all_enabled ? analyzed_field_options : {enabled: false}
335 end 337 end
336 338
337 mappings = mappings.deep_merge(options[:mappings] || {}) 339 mappings = mappings.deep_merge(options[:mappings] || {})
lib/searchkick/model.rb
@@ -5,7 +5,7 @@ module Searchkick @@ -5,7 +5,7 @@ module Searchkick
5 :filterable, :geo_shape, :highlight, :ignore_above, :index_name, :index_prefix, :language, 5 :filterable, :geo_shape, :highlight, :ignore_above, :index_name, :index_prefix, :language,
6 :locations, :mappings, :match, :merge_mappings, :routing, :searchable, :settings, :similarity, 6 :locations, :mappings, :match, :merge_mappings, :routing, :searchable, :settings, :similarity,
7 :special_characters, :stem_conversions, :suggest, :synonyms, :text_end, 7 :special_characters, :stem_conversions, :suggest, :synonyms, :text_end,
8 - :text_middle, :text_start, :word, :wordnet, :word_end, :word_middle, :word_start] 8 + :text_middle, :text_start, :type, :word, :wordnet, :word_end, :word_middle, :word_start]
9 raise ArgumentError, "unknown keywords: #{unknown_keywords.join(", ")}" if unknown_keywords.any? 9 raise ArgumentError, "unknown keywords: #{unknown_keywords.join(", ")}" if unknown_keywords.any?
10 10
11 raise "Only call searchkick once per model" if respond_to?(:searchkick_index) 11 raise "Only call searchkick once per model" if respond_to?(:searchkick_index)
test/inheritance_test.rb
@@ -2,14 +2,13 @@ require_relative "test_helper" @@ -2,14 +2,13 @@ require_relative "test_helper"
2 2
3 class InheritanceTest < Minitest::Test 3 class InheritanceTest < Minitest::Test
4 def setup 4 def setup
5 - skip if defined?(Cequel) || !elasticsearch_below60? 5 + skip if defined?(Cequel)
6 super 6 super
7 end 7 end
8 8
9 def test_child_reindex 9 def test_child_reindex
10 store_names ["Max"], Cat 10 store_names ["Max"], Cat
11 assert Dog.reindex 11 assert Dog.reindex
12 - Animal.searchkick_index.refresh  
13 assert_equal 1, Animal.search("*").size 12 assert_equal 1, Animal.search("*").size
14 end 13 end
15 14
@@ -18,6 +17,8 @@ class InheritanceTest &lt; Minitest::Test @@ -18,6 +17,8 @@ class InheritanceTest &lt; Minitest::Test
18 end 17 end
19 18
20 def test_child_search 19 def test_child_search
  20 + skip unless elasticsearch_below60?
  21 +
21 store_names ["Bear"], Dog 22 store_names ["Bear"], Dog
22 store_names ["Bear"], Cat 23 store_names ["Bear"], Cat
23 assert_equal 1, Dog.search("bear").size 24 assert_equal 1, Dog.search("bear").size
@@ -30,12 +31,16 @@ class InheritanceTest &lt; Minitest::Test @@ -30,12 +31,16 @@ class InheritanceTest &lt; Minitest::Test
30 end 31 end
31 32
32 def test_force_one_type 33 def test_force_one_type
  34 + skip unless elasticsearch_below60?
  35 +
33 store_names ["Green Bear"], Dog 36 store_names ["Green Bear"], Dog
34 store_names ["Blue Bear"], Cat 37 store_names ["Blue Bear"], Cat
35 assert_equal ["Blue Bear"], Animal.search("bear", type: [Cat]).map(&:name) 38 assert_equal ["Blue Bear"], Animal.search("bear", type: [Cat]).map(&:name)
36 end 39 end
37 40
38 def test_force_multiple_types 41 def test_force_multiple_types
  42 + skip unless elasticsearch_below60?
  43 +
39 store_names ["Green Bear"], Dog 44 store_names ["Green Bear"], Dog
40 store_names ["Blue Bear"], Cat 45 store_names ["Blue Bear"], Cat
41 store_names ["Red Bear"], Animal 46 store_names ["Red Bear"], Animal
@@ -43,6 +48,8 @@ class InheritanceTest &lt; Minitest::Test @@ -43,6 +48,8 @@ class InheritanceTest &lt; Minitest::Test
43 end 48 end
44 49
45 def test_child_autocomplete 50 def test_child_autocomplete
  51 + skip unless elasticsearch_below60?
  52 +
46 store_names ["Max"], Cat 53 store_names ["Max"], Cat
47 store_names ["Mark"], Dog 54 store_names ["Mark"], Dog
48 assert_equal ["Max"], Cat.search("ma", fields: [:name], match: :text_start).map(&:name) 55 assert_equal ["Max"], Cat.search("ma", fields: [:name], match: :text_start).map(&:name)
@@ -70,7 +77,7 @@ class InheritanceTest &lt; Minitest::Test @@ -70,7 +77,7 @@ class InheritanceTest &lt; Minitest::Test
70 store_names ["Bear A"], Cat 77 store_names ["Bear A"], Cat
71 store_names ["Bear B"], Dog 78 store_names ["Bear B"], Dog
72 Animal.reindex 79 Animal.reindex
73 - assert_equal 1, Dog.search("bear").size 80 + assert_equal 2, Animal.search("bear").size
74 end 81 end
75 82
76 # TODO move somewhere better 83 # TODO move somewhere better
test/test_helper.rb
@@ -505,6 +505,7 @@ end @@ -505,6 +505,7 @@ end
505 class Animal 505 class Animal
506 searchkick \ 506 searchkick \
507 default_fields: elasticsearch_below60? ? nil : [:name], 507 default_fields: elasticsearch_below60? ? nil : [:name],
  508 + type: elasticsearch_below60? ? nil : :animal,
508 text_start: [:name], 509 text_start: [:name],
509 suggest: [:name], 510 suggest: [:name],
510 index_name: -> { "#{name.tableize}-#{Date.today.year}#{Searchkick.index_suffix}" }, 511 index_name: -> { "#{name.tableize}-#{Date.today.year}#{Searchkick.index_suffix}" },