From 98d8fe062631a6779026fa64180b8cd5c762a098 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Tue, 3 Dec 2019 16:55:54 -0800 Subject: [PATCH] Improved models option with inheritance in ES 7.5+ --- lib/searchkick/query.rb | 29 +++++++++++++++++------------ test/inheritance_test.rb | 5 +++-- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/lib/searchkick/query.rb b/lib/searchkick/query.rb index b80c932..9fd5d86 100644 --- a/lib/searchkick/query.rb +++ b/lib/searchkick/query.rb @@ -435,19 +435,20 @@ module Searchkick models = Array(options[:models]) if models.any? { |m| m != m.searchkick_klass } - Searchkick.warn("Passing child models to models option throws off hits and pagination - use type option instead") - - # TODO uncomment once aliases are supported with _index - # should be ES 7.5 + # aliases are not supported with _index in ES below 7.5 # see https://github.com/elastic/elasticsearch/pull/46640 - # index_type_or = - # models.map do |m| - # v = {_index: m.searchkick_index.name} - # v[:type] = m.searchkick_index.klass_document_type(m, true) if m != m.searchkick_klass - # v - # end - - # where[:or] = Array(where[:or]) + [index_type_or] + if below75? + Searchkick.warn("Passing child models to models option throws off hits and pagination - use type option instead") + else + index_type_or = + models.map do |m| + v = {_index: m.searchkick_index.name} + v[:type] = m.searchkick_index.klass_document_type(m, true) if m != m.searchkick_klass + v + end + + where[:or] = Array(where[:or]) + [index_type_or] + end end # start everything as efficient filters @@ -1103,5 +1104,9 @@ module Searchkick def below70? Searchkick.server_below?("7.0.0") end + + def below75? + Searchkick.server_below?("7.5.0") + end end end diff --git a/test/inheritance_test.rb b/test/inheritance_test.rb index 5d0a1fb..9bcc78c 100644 --- a/test/inheritance_test.rb +++ b/test/inheritance_test.rb @@ -94,8 +94,9 @@ class InheritanceTest < Minitest::Test # see https://github.com/elastic/elasticsearch/issues/23306 # show warning for now # alternative is disallow inherited models with models option - assert_equal 3, Searchkick.search("bear", models: [Cat, Product]).hits.size - assert_equal 3, Searchkick.search("bear", models: [Cat, Product], per_page: 1).total_pages + expected = Searchkick.server_below?("7.5.0") ? 3 : 2 + assert_equal expected, Searchkick.search("bear", models: [Cat, Product]).hits.size + assert_equal expected, Searchkick.search("bear", models: [Cat, Product], per_page: 1).total_pages end # TODO move somewhere better -- libgit2 0.21.0