Comments (9)
Implement you own callback for save
or commit
lifecycle events.
from elasticsearch-rails.
ok. It would be nice if we could say 'if this attribute changes, then save this method at the same time'
from elasticsearch-rails.
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.
ok. thank you. We'l try to help with a solution if I find something interesting.
from elasticsearch-rails.
@alotela The linked commit should fix your issue.
from elasticsearch-rails.
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.
@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.
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.
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)
- Batch size is ignored when fetching documents from MongoDB
- NPE happens on short prefix query on a field with index_prefixes
- Indexes are not refreshing, even with refresh_interval being set
- Disable automatic callbacks in rspec
- Not getting any records from elasticsearch HOT 1
- FYI gem version 6.1.1 was pushed without any changes HOT 1
- elasticsearch-model 7.2.1 introduces circular dependency with ruby 2.7 and rubygems 3.2.5 HOT 1
- specify an alias near the index_name in an AR model HOT 1
- Illegal argument "type" sent in all requests upgrading to 8.x HOT 1
- Not able to update single record too please help me to here HOT 1
- How to import into two clusters
- Mapping Two Ruby on Rails Models with Different Attribute Names to a Shared Elasticsearch Index using 'elasticsearch-rails'
- update_document not selective enough
- Backward compatibility for older Elasticsearch version HOT 1
- Faraday::ConnectionFailed (end of file reached) HOT 1
- Elasticsearch - Unexpected Character Error (Minus Sign) HOT 1
- Unexpected behavior when searching from a scope
- Feature Suggestion - Get Indexed Document for Instance
- Deprecation warning in Rails 7.1 (support Rails 7.2)
- ElasticSearch::Model does not register a subclass HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from elasticsearch-rails.