-
Notifications
You must be signed in to change notification settings - Fork 808
Fix changed attributes matching #140
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
Conversation
If I define `as_indexed_json` function to return symbol-keyed hash then the changes will not be detected because:
```
def as_indexed_json
{name: name}
end
as_indexed_json.keys.include? 'name' #=> false
```
Thus I recommend to compare string variants.
|
Hey, I think this might depend on the Ruby version? Also, I have trouble understanding the issue from your description, and there's sadly no test to help me there .) |
|
(Or, instead of test, a better code example of what you're doing.) |
|
Sorry for bad description :). Here is a better one I hope. First of all my searchable module definition: The main point here is I expect corresponding index item to be also updated but it will not because of Here are the values for LOOK HERE line: Thus we will see an update request to elasticsearch even with changed attributes: Please let me know if you need more information. I am ashamed to say that but I failed to write test in 10 minutes... Thus detailed description is here :) |
|
Right, got you. So, the problem is that when I run the example from I haven't got time right now to peek how Notice also this integration test: https://github.com/elasticsearch/elasticsearch-rails/blob/master/elasticsearch-model/test/integration/active_record_basic_test.rb#L117-L129 Can you play a bit with these examples? Finally, I'm not opposed to being so defensive here, in this code, but I'd like to first understand what's going on, and then, of course, we need to make sure we have tests for this. And when no tests are failing, we first need to come up with a failing test.... |
|
@FUT Any news here? |
|
Just ran into the same problem. Very annoying and hard to debug. |
|
Btw, the Rails API demo code for |
|
Are the specs on Travis for this PR only red b/c of a Travis issue? maybe try restarting them. |
|
@paulwittmann You shouldn't use the automatic callbacks when you define your own @FUT I'll try to write a test for this and verify it... |
|
The problem is if we have this: class User < ActiveRecord::Base
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
settings do
mappings dynamic: 'false' do
indexes :description, type: 'string'
end
end
def as_indexed_json(options = {})
{
description: self.description
}
end
endThen the following won't write any changes to ElasticSearch: user.update_attributes(description: 'new description')my current workaround: def as_indexed_json(options = {})
{
'description' => self.description
}
endWhy are the built-in callbacks not meant to be used when I have a custom as_indexed_json and where do the docs warn against combining the two? |
|
@FUT I've updated some tests to catch the behaviour you and @paulwittmann are describing, and added the defensive @paulwittmann Your issue should be solved by the closing patch. Thanks for adding the example. |
|
thanks so much for the quick fix! |
Should be working all right now for the case you have. But there are other problems, like eg. #166. In general, I would recommend to take indexing into your own hands, except for trivial cases, where the automatic callbacks work. They're more a convenience, so people don't have to write silly code just to update Elasticsearch when they save a model. But in complex situations, indexing should be custom, asynchronous, etc. |
…_indexed_json`
Example of the problem:
book = Book.create(title: "My book")
book.title = "My book 2"
book.save
# Elasticsearch::Transport::Transport::Errors::BadRequest:
# [400] {"error":"MapperParsingException[failed to parse [title]]; nested: ElasticsearchIllegalArgumentException[unknown property [ru]]; ","status":400}
book.instance_variable_get(:@__changed_attributes) # => {title: {ru: "My book 2"}}
book.as_indexed_json # => {title: "My book 2}
Related: #166, #140
|
Thank you very much for the fix guys! Sorry that I did not helped a lot.. |
|
@FUT On the contrary, thanks for reporting it and posting the example, unfortunately it clicked for me only after @paulwittmann added his :) |
If I define
as_indexed_jsonfunction to return symbol-keyed hash then the changes will not be detected because if I changed name:Thus I recommend to compare string variants.