You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A subclass of a class that includes Elasticsearch::Model is not registered. This yields a crash when searching on the subclass.
Specifically, this method in lib/elasticsearch/model/adapters/multiple.rb returns nil because detect cannot find the subclass in the registry.
# Returns the class of the model corresponding to a specific `hit` in Elasticsearch results
#
# @see Elasticsearch::Model::Registry
#
# @api private
#
def __type_for_hit(hit)
@@__types ||= {}
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] && __no_type?(hit)) ||
(model.index_name == hit[:_index] && model.document_type == hit[:_type])
end
end
end
This is not working code, but it is illustrative of the problem.
This is the code in lib/elasticsearch/model.rb that fails to add a class to the registry when it is a subclass...
def self.included(base)
base.class_eval do
include Elasticsearch::Model::Proxy
# Delegate common methods to the `__elasticsearch__` ClassMethodsProxy, unless they are defined already
class << self
METHODS.each do |method|
delegate method, to: :__elasticsearch__ unless self.public_instance_methods.include?(method)
end
end
end
# Add to the model to the registry if it's a class (and not in intermediate module)
Registry.add(base) if base.is_a?(Class)
end
The line Registry.add(base) if base.is_a?(Class) should likely be copied to a def inherited(subclass) method like so...
def inherited(subclass)
Registry.add(subclass) if subclass.is_a?(Class)
end
... to account for the subclassing. As is, only the first class to include Elasticsearch::Model is instrumented.
The content you are editing has changed. Please copy your edits and refresh the page.
Versions < 7.0.0 of this gem supported inheritance-- more specifically, Single Table Inheritance. With this feature,
elasticsearch settings (index mappings, etc) on a parent model could be inherited by a child model leading to different
model documents being indexed into the same Elasticsearch index. This feature depended on the ability to set a type
for a document in Elasticsearch. The Elasticsearch team has deprecated support for types, as is described here.
This gem will also remove support for types and Single Table Inheritance in version 7.0 as it enables an anti-pattern.
Please save different model documents in separate indices. If you want to use STI, you can include an artificial type field manually in each document and use it in other operations.
Does this apply to subclasses outside of STI? I suppose it might.
A subclass of a class that includes
Elasticsearch::Model
is not registered. This yields a crash when searching on the subclass.Specifically, this method in lib/elasticsearch/model/adapters/multiple.rb returns
nil
becausedetect
cannot find the subclass in the registry.This is not working code, but it is illustrative of the problem.
This is the code in lib/elasticsearch/model.rb that fails to add a class to the registry when it is a subclass...
The line
Registry.add(base) if base.is_a?(Class)
should likely be copied to adef inherited(subclass)
method like so...... to account for the subclassing. As is, only the first class to include
Elasticsearch::Model
is instrumented.Tasks
The text was updated successfully, but these errors were encountered: