rs-pro / mongoid-elasticsearch

DEPRECATED - Consider using SearchKick https://github.com/ankane/searchkick
MIT License
61 stars 23 forks source link

results wrapper model initializing model twice #25

Open gthorsen opened 7 years ago

gthorsen commented 7 years ago

When using the wrapper :model option for ealsticsearch results, the response object trys to assign elastics '_source' node as attributes/fields of the new model instance base on the _type field, but because the model does not respond to _type, _source, _score, or _highlight fields it rescues the error and then assigns those fields to model and then returns the model. This causes the object to initialize twice.

lib/mongoid/elasticsearch/response.rb:175:in 'new' lib/mongoid/elasticsearch/response.rb:185:in 'new'

Would it be better to check if the model responds to the elasticsearch fields, and if not assign them to the model if their values are not nil? It seems like this would be a better instantiating the model twice.

If it makes sense I'll fork and fix. :)