Elasticsearch-rails: Elasticsearch::Model::Callbacks not working for updates

Created on 23 Nov 2015  路  5Comments  路  Source: elastic/elasticsearch-rails

Hi I have a file called as blog.rb

where I have added these two lines:

include Elasticsearch::Model
include Elasticsearch::Model::Callbacks

Then I have the dsl in my code:

def self.search(query_term)
__elasticsearch__.search(
 {
"query" =>{
    "filtered" => {
        "query" => {
            "multi_match" => {
                "query" => query_term,
                "fields" => ["name", "blog_entry"]
            }
        },
        "filter" => {
            "term" => {"public_flag"=> true}
        }
    }
}
}).records
end

I have set up elasticsearch service in my local environment, am able to access the same via postman. Search works if there is a brand new entry, even deletion works fine. But if I update the blog_entry, the search doesnt work on updated blog entry. I wait for few minutes, still the search doesnt bring back the updated entry. Should I be adding this statement in my code?

 Blog.__elasticsearch__.refresh_index! 
stale

Most helpful comment

I get the same issue and I've tryed with custom callback from the readme. But when I replace __elasticsearch__.update_document by __elasticsearch__.index_document refresh: true it works as expected.

All 5 comments

Have you tried setting up the callback yourself like shown in the readme?

+1

I get the same issue and I've tryed with custom callback from the readme. But when I replace __elasticsearch__.update_document by __elasticsearch__.index_document refresh: true it works as expected.

The elasticsearch-model gem basically adds a before_save callback setting an instance variable with the attributes that changed (@__changed_attributes). Then in the update_document method, it's using this variable to know what to update.

You should open the gem manually (bundle open elasticsearch-model) and make sure that the right attributes are present when updating your model (write a test for it, it makes it easier).

A simple solution, that would make your updates slower, would be to use after_commit lambda { __elasticsearch__.index_document }, on: :update instead of after_commit lambda { elasticsearch.update_document }, on: :update` on update... Not pretty, but I think that might fix it if you're desperate.

Personally, the issue I was having (and still have, but I'll probably be able to fix it easily now that I understood it), is that it doesn't work well with this: store :extra, accessors: [:notes, :drops, :mods], coder: JSON, the store feature of rails. The reason is that model.changes will return something like { extra: { notes: [], etc } }, and that doesnt "match" with what as_indexed_json returns :(

edit: what I ended up doing is adding another before_save callback that modifies the instance variable and merge the extra hash into it... A better solution I think would be to get rid of the store, and use serialize :notes (same for each accessor).

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings