itstommymorgan / asari

a Ruby wrapper for AWS CloudSearch.
52 stars 51 forks source link

ActiveRecord indexed objects are removed when updating them if they don't match the asari_should_index? method #18

Open mmeys opened 11 years ago

mmeys commented 11 years ago

Hi,

I was wondering why an ActiveRecord object indexed using the asari_index method is removed from cloud search after and update if it doesn't match the asari_should_index? method?

The behavior I want is to update on cloud search the indexed object only when some attributes changed. Indeed I index users of my app with their name and their avatar. So I don't want to update them on cloud search if theses attributes doesn't change...and I don't want them to be deleted from cloud search neither.

That's why I suggest to remove the self.asari_remove_item(obj)line from the asari_update_item method.

def asari_update_item(obj)
  if self.asari_when
    unless asari_should_index?(obj)
      self.asari_remove_item(obj) # I suggest to remove this line
      return
    end
  end
  data = {}
  self.asari_fields.each do |field|
    data[field] = obj.send(field)
  end
  self.asari_instance.update_item(obj.send(:id), data)
rescue Asari::DocumentUpdateException => e
  self.asari_on_error(e)
end
zmoshansky commented 10 years ago

1.) Updating the index only when the indexed fields changed would be duplicate of Issue #11 2.) Removing this line would prevent objects from being deleted from the index when they are updated. Which is likely a use case.

For your problem, perhaps look into using a when block that returns false on update if the fields haven't changed. Perhaps using http://stackoverflow.com/questions/1586536/how-to-detect-attribute-changes-from-model

mmeys commented 10 years ago

1.) I agree. 2.) Indeed. I don't understand why this has never been corrected.

Thanks for the suggestion, that's what I did!