elastic / elasticsearch-rails

Elasticsearch integrations for ActiveModel/Record and Ruby on Rails
Apache License 2.0
3.07k stars 793 forks source link

Feature Suggestion - Get Indexed Document for Instance #1069

Open vanboom opened 7 months ago

vanboom commented 7 months ago

Suggestion: provide an instance method to allow fetching the indexed document if it exists.

Benefits:

  1. This would allow comparing the indexed instance with the Rails model attributes to allow for better index updating control. i.e. do not update the index if no attributes have changed. The update_document feature does not work for async indexing as the record is reloaded from the database unless the changes hash is passed to the async worker.
  2. This would allow integrity checking of the Rails SQL vs. the Elasticsearch for self-healing indexes if anything gets out of sync, e.g. if an async worker fails to update the index for whatever reason.

Thanks for your consideration. Is this a good idea? I will try to contribute a PR.

Maybe a simple get method on the Elasticsearch::Model::InstanceMethods?

client = YourModel.__elasticsearch__.client
document = client.get({ index: YourModel.index_name, id: id_to_find })