Commit 98d8fe062631a6779026fa64180b8cd5c762a098

Authored by Andrew Kane
1 parent c9a82022

Improved models option with inheritance in ES 7.5+

lib/searchkick/query.rb
... ... @@ -435,19 +435,20 @@ module Searchkick
435 435  
436 436 models = Array(options[:models])
437 437 if models.any? { |m| m != m.searchkick_klass }
438   - Searchkick.warn("Passing child models to models option throws off hits and pagination - use type option instead")
439   -
440   - # TODO uncomment once aliases are supported with _index
441   - # should be ES 7.5
  438 + # aliases are not supported with _index in ES below 7.5
442 439 # see https://github.com/elastic/elasticsearch/pull/46640
443   - # index_type_or =
444   - # models.map do |m|
445   - # v = {_index: m.searchkick_index.name}
446   - # v[:type] = m.searchkick_index.klass_document_type(m, true) if m != m.searchkick_klass
447   - # v
448   - # end
449   -
450   - # where[:or] = Array(where[:or]) + [index_type_or]
  440 + if below75?
  441 + Searchkick.warn("Passing child models to models option throws off hits and pagination - use type option instead")
  442 + else
  443 + index_type_or =
  444 + models.map do |m|
  445 + v = {_index: m.searchkick_index.name}
  446 + v[:type] = m.searchkick_index.klass_document_type(m, true) if m != m.searchkick_klass
  447 + v
  448 + end
  449 +
  450 + where[:or] = Array(where[:or]) + [index_type_or]
  451 + end
451 452 end
452 453  
453 454 # start everything as efficient filters
... ... @@ -1103,5 +1104,9 @@ module Searchkick
1103 1104 def below70?
1104 1105 Searchkick.server_below?("7.0.0")
1105 1106 end
  1107 +
  1108 + def below75?
  1109 + Searchkick.server_below?("7.5.0")
  1110 + end
1106 1111 end
1107 1112 end
... ...
test/inheritance_test.rb
... ... @@ -94,8 +94,9 @@ class InheritanceTest < Minitest::Test
94 94 # see https://github.com/elastic/elasticsearch/issues/23306
95 95 # show warning for now
96 96 # alternative is disallow inherited models with models option
97   - assert_equal 3, Searchkick.search("bear", models: [Cat, Product]).hits.size
98   - assert_equal 3, Searchkick.search("bear", models: [Cat, Product], per_page: 1).total_pages
  97 + expected = Searchkick.server_below?("7.5.0") ? 3 : 2
  98 + assert_equal expected, Searchkick.search("bear", models: [Cat, Product]).hits.size
  99 + assert_equal expected, Searchkick.search("bear", models: [Cat, Product], per_page: 1).total_pages
99 100 end
100 101  
101 102 # TODO move somewhere better
... ...