Commit 5d8cdd0d1a46c89a8359616aa8a183244f4659aa

Authored by Andrew Kane
1 parent 33ee919b

Made type optional for custom mapping for Elasticsearch 6 - #1260

CHANGELOG.md
1 1 ## 4.0.1 [unreleased]
2 2  
  3 +- Made type optional for custom mapping for Elasticsearch 6
3 4 - Fixed error when suggestions empty
4 5 - Fixed `models` option with inheritance
5 6  
... ...
README.md
... ... @@ -1396,10 +1396,8 @@ Create a custom mapping:
1396 1396 ```ruby
1397 1397 class Product < ApplicationRecord
1398 1398 searchkick mappings: {
1399   - product: {
1400   - properties: {
1401   - name: {type: "keyword"}
1402   - }
  1399 + properties: {
  1400 + name: {type: "keyword"}
1403 1401 }
1404 1402 }
1405 1403 end
... ...
lib/searchkick/index_options.rb
... ... @@ -5,12 +5,24 @@ module Searchkick
5 5 language = options[:language]
6 6 language = language.call if language.respond_to?(:call)
7 7  
  8 + below62 = Searchkick.server_below?("6.2.0")
  9 + below70 = Searchkick.server_below?("7.0.0")
  10 +
  11 + if below70
  12 + index_type = options[:_type]
  13 + index_type = index_type.call if index_type.respond_to?(:call)
  14 + end
  15 +
  16 + custom_mapping = (options[:mapping] || {}).symbolize_keys
  17 + if below70 && custom_mapping.any? && custom_mapping.key?(:properties)
  18 + # add type
  19 + custom_mapping = {index_type.to_sym => custom_mapping}
  20 + end
  21 +
8 22 if options[:mappings] && !options[:merge_mappings]
9 23 settings = options[:settings] || {}
10   - mappings = options[:mappings]
  24 + mappings = custom_mapping
11 25 else
12   - below62 = Searchkick.server_below?("6.2.0")
13   - below70 = Searchkick.server_below?("7.0.0")
14 26  
15 27 default_type = "text"
16 28 default_analyzer = :searchkick_index
... ... @@ -411,12 +423,10 @@ module Searchkick
411 423 }
412 424  
413 425 if below70
414   - index_type = options[:_type]
415   - index_type = index_type.call if index_type.respond_to?(:call)
416 426 mappings = {index_type => mappings}
417 427 end
418 428  
419   - mappings = mappings.symbolize_keys.deep_merge((options[:mappings] || {}).symbolize_keys)
  429 + mappings = mappings.symbolize_keys.deep_merge(custom_mapping)
420 430 end
421 431  
422 432 {
... ...
test/models/store.rb
... ... @@ -4,7 +4,6 @@ class Store
4 4 name: {type: "keyword"}
5 5 }
6 6 }
7   - mappings = {store: mappings} if Searchkick.server_below?("7.0.0")
8 7  
9 8 searchkick \
10 9 routing: true,
... ...