Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Callbacks when updating associated record? #57

Closed
bcackerman opened this issue Mar 25, 2014 · 12 comments
Closed

Callbacks when updating associated record? #57

bcackerman opened this issue Mar 25, 2014 · 12 comments

Comments

@bcackerman
Copy link

So in your example https://github.com/elasticsearch/elasticsearch-rails/blob/templates/elasticsearch-rails/lib/rails/templates/searchable.rb#L60 if Authors is updated, how would the original model's index get updated? Running into this issue locally.

@bcackerman
Copy link
Author

To clarify, is it possible to hit the callback method using include Elasticsearch::Model::Callbacks when a touched record was updated?

@bcackerman
Copy link
Author

To further clarify, I have a model Orders which belongs to a Customer. I've added after_touch() { __elasticsearch__.index_document } to my Searchable concern as well. This is my mapping

    mapping do
      indexes :production_notes, type: 'string', analyzer: 'snowball'
      indexes :order_nickname, type: 'string', analyzer: 'keyword'
      indexes :visual_id, type: 'integer'
      indexes :paid, type: 'boolean'
      indexes :orderstatus_id, type: 'integer'
      indexes :customer,
        type: 'object',
        properties: {
            first_name: { type: 'string' },
            last_name: { type: 'string' },
            company: { type: 'string' }
        }
    end

Is there another method to reindex after touch that I'm not seeing?

@karmi
Copy link
Contributor

karmi commented Mar 25, 2014

Depends on how you define the associations -- have a look e.g. here: https://github.com/elasticsearch/elasticsearch-rails/blob/master/elasticsearch-model/examples/activerecord_associations.rb

@karmi
Copy link
Contributor

karmi commented Mar 25, 2014

Aye, this was missing, see this: e8a7f49

@bcackerman
Copy link
Author

Ah ok so include Elasticsearch::Model::Callbacks will know if an associated record is changed when you use touch: true then I'm assuming? Just having trouble getting that working, will have to play with it more.

@bcackerman
Copy link
Author

Ok, what if an Article belongs_to :author?

In that case if you had belongs_to :author, touch: true in Article, would the Article index get updated if Author changed?

@karmi
Copy link
Contributor

karmi commented Mar 25, 2014

@bcackerman So what happens when you try it out?

@bcackerman
Copy link
Author

Specifically I have an index like this:

def as_indexed_json(options={})
      hash = self.as_json(
        include: { 
          orderstatus: { only: [:color, :name] },
          customer: { methods: [:full_name], only: [:full_name, :company] }
        }
      )
      hash
end

Where an Order belongs to a Customer and an Orderstatus. If I update the order's customer, the index's include isn't updated.

@karmi
Copy link
Contributor

karmi commented Mar 25, 2014

@bcackerman Have you actually tried your suggestion? Why are you asking me what happens?

@bcackerman
Copy link
Author

Oh, I am trying it :) I'm just letting you know it doesn't seem that the Callbacks class works to reindex the record when there's an include in my as_indexed_json method.

@bcackerman
Copy link
Author

Figured it out, thanks!

karmi added a commit that referenced this issue Apr 15, 2014
…serialized during update

Previously, the `update_document` method simply intercepted the changes to the model, via the
`@__changed_attributes` variable, and used these directly.

This caused models with a custom serialization method to be incorrectly serialized, namely
unwanted attributes were added.

This patch looks for `as_indexed_json` defined on the model, and when it finds it,
filters the changed attributes through the keys.

Closes #75

Related:

* #59
* #57
* #52
* #40
* #37
* #5
@bcackerman
Copy link
Author

To follow up on this, associated records that the updated record belongs_to, are not reindexed using just belongs_to :model, touch: true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants