Commit 5d8cdd0d1a46c89a8359616aa8a183244f4659aa
1 parent
33ee919b
Exists in
master
and in
17 other branches
Made type optional for custom mapping for Elasticsearch 6 - #1260
Showing
4 changed files
with
19 additions
and
11 deletions
Show diff stats
CHANGELOG.md
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 | { | ... | ... |