lib/elasticsearch/model/adapters/multiple.rb in elasticsearch-model-6.1.2 vs lib/elasticsearch/model/adapters/multiple.rb in elasticsearch-model-7.0.0.pre

- old
+ new

@@ -1,5 +1,22 @@ +# Licensed to Elasticsearch B.V. under one or more contributor +# license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright +# ownership. Elasticsearch B.V. licenses this file to you under +# the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + module Elasticsearch module Model module Adapter # An adapter to be used for deserializing results from multiple models, @@ -56,12 +73,12 @@ case when Elasticsearch::Model::Adapter::ActiveRecord.equal?(adapter) klass.where(klass.primary_key => ids) when Elasticsearch::Model::Adapter::Mongoid.equal?(adapter) klass.where(:id.in => ids) - else - klass.find(ids) + else + klass.find(ids) end end # Returns the record IDs grouped by class based on type `_type` # @@ -89,14 +106,22 @@ # @api private # def __type_for_hit(hit) @@__types ||= {} - @@__types[ "#{hit[:_index]}::#{hit[:_type]}" ] ||= begin + key = "#{hit[:_index]}::#{hit[:_type]}" if hit[:_type] && hit[:_type] != '_doc' + key = hit[:_index] unless key + + @@__types[key] ||= begin Registry.all.detect do |model| - model.index_name == hit[:_index] && model.document_type == hit[:_type] + (model.index_name == hit[:_index] && __no_type?(hit)) || + (model.index_name == hit[:_index] && model.document_type == hit[:_type]) end end + end + + def __no_type?(hit) + hit[:_type].nil? || hit[:_type] == '_doc' end # Returns the adapter registered for a particular `klass` or `nil` if not available # # @api private