algolia / algoliasearch-django

Seamless integration of Algolia into your Django project.
https://www.algolia.com
MIT License
173 stars 65 forks source link

Turn off auto-indexing for certain operations #251

Closed dbinetti closed 6 years ago

dbinetti commented 6 years ago

Hi, is it possible to turn off auto-indexing temporarily? (Like signal disabling?)

I run a nightly operation that runs a denormalization that touches every row, but does not affect fields that are reflected in my search index. However, since I'm touching every row it counts as an operation, and I'm quickly using up my allotment for the month. is there a way to turn off indexing immediately prior to, and re-enable following, my operation?

Thanks.

PLNech commented 6 years ago

Hi @dbinetti, thanks for suggesting this! I definitely see how it could be useful. We just need to consider how we could implement this thoughtfully, I'll tag it as an enhancement and we'll add it to the roadmap for this integration :)

PLNech commented 6 years ago

@julienbourdeau: if that works for you, I propose you give a try to implementing this.

I'll happily review your design/PR :slightly_smiling_face:

clemfromspace commented 6 years ago

A really quick and simple way to do it will be to use a context decorator, like the translation.override one from Django: Doc: https://docs.djangoproject.com/en/2.0/ref/utils/#django.utils.translation.override Code: https://github.com/django/django/blob/master/django/utils/translation/__init__.py

The base class ContextDecorator is really well made in the sense it allows you to use the context decorator also as a function decorator, so it will allow you to do something like this:

with disable_auto_indexing():
     big_operation()

or

@disable_auto_indexing()
def big_operation():
    ...

WDYT?

dbinetti commented 6 years ago

So perhaps this is a dumb question, but shouldn't I be able to do this myself? I know the process to disable signals, but for whatever reason I can't load the receiver def __post_save_receiver(self, instance, **kwargs): in registration.py. Is there some reason for this?

clemfromspace commented 6 years ago

Hello @dbinetti

I managed to disable the signal by doing the following:

from django.db.models.signals import post_save

from algoliasearch_django import algolia_engine

post_save.disconnect(algolia_engine._AlgoliaEngine__post_save_receiver, sender=MyModel)

(Note the _AlgoliaEngine__post_save_receiver name of the method, coming from the python Name mangling )

Let me know if that help :)

clemfromspace commented 6 years ago

Hi again @dbinetti,

I opened #266, I think it should solve the issue your are having, let me know what you think :)

dbinetti commented 6 years ago

Yep, perfect @clemfromspace. This is exactly what I had in mind!

clemfromspace commented 6 years ago

@dbinetti Hey there, we just released the version 1.7.0 containing the context decorator :) I am working on updating the documentation right now, but you can already upgrade and test it like below:

from algoliasearch_django.decorators import disable_auto_indexing

# As a method decorator
@disable_auto_indexing
def my_method():
    pass

# As a context manager (specifying the model)
with disable_auto_indexing(model=MyModel):
    pass
dbinetti commented 6 years ago

Yep, works perfectly. thanks!

On Mon, Aug 27, 2018 at 6:48 AM Clément Denoix notifications@github.com wrote:

@dbinetti https://github.com/dbinetti Hey there, we just released the version 1.7.0 containing the context decorator :) I am working on updating the documentation right now, but you can already upgrade and test it like below:

from algoliasearch_django.decorators import disable_auto_indexing @disable_auto_indexingdef my_method(): pass with disable_auto_indexing(model=MyModel): pass

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/algolia/algoliasearch-django/issues/251#issuecomment-416232585, or mute the thread https://github.com/notifications/unsubscribe-auth/AAJ3ujrMGqgEcPw8BhUZQIANrpnYQMshks5uU_i6gaJpZM4TXSNE .