Coder Social home page Coder Social logo

as_json only: [...attributs...], methods: [...methods list...] => callback update_document does not update "methods values" in ES about elasticsearch-rails HOT 9 CLOSED

elastic avatar elastic commented on June 22, 2024
as_json only: [...attributs...], methods: [...methods list...] => callback update_document does not update "methods values" in ES

from elasticsearch-rails.

Comments (9)

karmi avatar karmi commented on June 22, 2024

Implement you own callback for save or commit lifecycle events.

from elasticsearch-rails.

alotela avatar alotela commented on June 22, 2024

ok. It would be nice if we could say 'if this attribute changes, then save this method at the same time'

from elasticsearch-rails.

karmi avatar karmi commented on June 22, 2024

There are couple of issues opened for that, lately: #5 and #37. We need to better document it, or improve the behaviour.

from elasticsearch-rails.

alotela avatar alotela commented on June 22, 2024

ok. thank you. We'l try to help with a solution if I find something interesting.

from elasticsearch-rails.

karmi avatar karmi commented on June 22, 2024

@alotela The linked commit should fix your issue.

from elasticsearch-rails.

alotela avatar alotela commented on June 22, 2024

If I'm right, it just checks for attributes I really want to put into ES. My problem is that when I use methods array in as_indexed_json, they are not updated. It's not about unwanted attributes.
I solved the problem with this:
1/ I Changed the method:

    module Elasticsearch
      module Model
        module Indexing
          module InstanceMethods
            def update_document(options={})
              changed_attributes = self.instance_variable_get(:@__changed_attributes)
              changed_attributes.merge!(es_changed_methods_values(changed_attributes.keys)) if changed_attributes
              if changed_attributes
                client.update(
                  { 
                     index: index_name, type:  document_type, id:    self.id, body:  { doc: changed_attributes }}.merge(options)
                )
                else
                  index_document(options)
                end
              end

               def es_changed_methods_values changed_attributs
                  changed_methods = {}
                  return changed_methods unless respond_to?(:es_changed_methods)
              es_changed_methods.each do |attribut, methods|
                    methods.each { |method| changed_methods[method] = send(method) } if changed_attributs.include?(attribut)
                  end
                  changed_methods
                end
              end
            end
          end
        end

2/ In my concern or model, I had this to know the methods I need to "refresh" depending on changed attributes:

     def es_changed_methods
      {
        "last_connection_at" => [:es_connected_until, :es_visible_until],
        "lat" => [:es_location],
        "lng" => [:es_location]
      }
    end

last_connection_at, lat, png are my model's attributes
es_connected_until, es_visible_until, es_location are methods for as_indexed_json:

    def as_indexed_json options={}
      as_json only: ['id',
        'banned',
        'birthday',
        'certified',
        'confirmed_at',
        'name',
        'gender',
        'ghost_mode',
        'hide_age',
        'is_banned_by_admin', 
        'last_connection_at',
        'city'],
        methods: ['es_connected_until', 'es_friend_ids', 'es_location', 'es_profil_photo_url', 'es_visible_until']
    end 

Works perfectly!

from elasticsearch-rails.

karmi avatar karmi commented on June 22, 2024

@alotela You shouldn't override update_document in your code. Period. (At least override it for the proxy object in the specific model, not for all.)

My problem is that when I use methods array in as_indexed_json, they are not updated.

Yes, see #5 for a discussion about that. Just implement your own hooks for indexing, that's it, really.

from elasticsearch-rails.

alotela avatar alotela commented on June 22, 2024

I totally agree that overriding update_document is not the right way to do.
But:
1/ It is easy to use in all models... Just need to define the "es_changed_methods"
2/ maybe it would be a good solution to put directly into elastic search-rails so no more "override"
3/ I think solutions in #5 are more complicated and not really an easy way to do things, even if I saw your comment
"On the other hand, I wouldn't like making the automatic callbacks so magical and smart, they handle every use case... I've seen much of these issues and participated in many discussions like this in Tire, and unless there's a clear way, it's usually best leaving this to the user?"

I personally think this problem should be managed by the gem easily... And defining a method to match changed_attributes and "changed_methods" looks good for me. It allows the gem to update only changed data.

What do you think ? ;)

from elasticsearch-rails.

karmi avatar karmi commented on June 22, 2024

I still think the cleanest way is to simply define your own indexing routines, in concert with your own serialization routines. The default callbacks are just that -- defaults for convenience.

from elasticsearch-rails.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.