Open martinstreicher opened 6 months ago
I will note that if you explicitly include Elasticsearch::Model
in both classes, both classes are added to the registry.
I do see this in the README:
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 atype
for a document in Elasticsearch. The Elasticsearch team has deprecated support fortypes
, as is described here. This gem will also remove support for types andSingle 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 artificialtype
field manually in each document and use it in other operations.
Does this apply to subclasses outside of STI? I suppose it might.
A proposed PR is here: https://github.com/elastic/elasticsearch-rails/pull/1073/files -- if this is a valid issue and repair, I will add tests to the PR. I signed the contributor's agreement.
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.