Commit b8fe2f0a9b43f24b197dc788cd341ff0163c8eb2

Authored by Andrew
1 parent dee94765

Added type option

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