如何仅在删除,添加和更新模型实例时将algolia与数据库同步
How to synchronise algolia with database only when deleting, adding and updating model instance
根据 algoliasearch-django 文档:
AUTO_INDEXING
: automatically synchronize the models with Algolia (default to True
).
据我了解,如果我将 AUTO_INDEXING
设置为 True,每当我更新模型实例或更新模型(例如添加新字段)时,它都会将 Algolia 与我自己的数据库(或模型)同步。但是,我想做的是同步Algolia on demand,例如只在模型实例发生变化、添加或删除时同步它们。有什么办法可以实现吗?谢谢。
启用 AUTO_INDEXING
时,您告诉 Algolia 的 Django 集成连接到 Django 的 pre-save
and post-save
信号。当您的一个实例更新时,这些提供了一个做出反应的机会,Algolia 在这里使用它来同步您索引中的这些更改。
As you can see in django/db/models/base.py
,这些是调用模型的 save()
方法时发送的唯一信号:
if not meta.auto_created:
pre_save.send(
sender=origin, instance=self, raw=raw, using=using,
update_fields=update_fields,
)
#...
# Signal that the save is complete
if not meta.auto_created:
post_save.send(
sender=origin, instance=self, created=(not updated),
update_fields=update_fields, raw=raw, using=using,
)
所以Algolia的AUTO_INDEXING
只能依赖这些作为trigger to update your records:
if (isinstance(auto_indexing, bool) and
auto_indexing) or self.__auto_indexing:
# Connect to the signalling framework.
post_save.connect(self.__post_save_receiver, model)
pre_delete.connect(self.__pre_delete_receiver, model)
def __post_save_receiver(self, instance, **kwargs):
"""Signal handler for when a registered model has been saved."""
logger.debug('RECEIVE post_save FOR %s', instance.__class__)
self.save_record(instance, **kwargs)
def __pre_delete_receiver(self, instance, **kwargs):
"""Signal handler for when a registered model has been deleted."""
logger.debug('RECEIVE pre_delete FOR %s', instance.__class__)
self.delete_record(instance)
考虑到这一点,AUTO_INDEXING
无法猜测您是否调用了 instance.save()
因为您创建了一个新实例 或 因为您的模型已更新,更新了所有现有实例。 AUTO_INDEXING
旨在每次 实例更新时 触发索引(通过添加新字段、更改当前字段、添加实例或删除实例),以确保您的 Algolia索引始终与您的 Django 数据库同步。
如果您想使用 Algolia 实现更自定义的同步处理,您可以:
- 禁用
AUTO_INDEXING
在您的 apps.py
中向 Algolia 注册您的模型后,连接到适当的信号:
algoliasearch.register(Contact, ContactIndex)
post_save.connect(my_post_save_receiver, ContactIndex)
pre_delete.connect(my_pre_delete_receiver, ContactIndex)
并且在您的 my_post_save_receiver
/my_pre_save_receiver
方法中,处理 Signal 参数以决定是否要调用 index.save_record(instance)
.
看看 Signals documentation and the Signals guide. Please note the duplicate signals 部分。
根据 algoliasearch-django 文档:
AUTO_INDEXING
: automatically synchronize the models with Algolia (default toTrue
).
据我了解,如果我将 AUTO_INDEXING
设置为 True,每当我更新模型实例或更新模型(例如添加新字段)时,它都会将 Algolia 与我自己的数据库(或模型)同步。但是,我想做的是同步Algolia on demand,例如只在模型实例发生变化、添加或删除时同步它们。有什么办法可以实现吗?谢谢。
启用 AUTO_INDEXING
时,您告诉 Algolia 的 Django 集成连接到 Django 的 pre-save
and post-save
信号。当您的一个实例更新时,这些提供了一个做出反应的机会,Algolia 在这里使用它来同步您索引中的这些更改。
As you can see in django/db/models/base.py
,这些是调用模型的 save()
方法时发送的唯一信号:
if not meta.auto_created:
pre_save.send(
sender=origin, instance=self, raw=raw, using=using,
update_fields=update_fields,
)
#...
# Signal that the save is complete
if not meta.auto_created:
post_save.send(
sender=origin, instance=self, created=(not updated),
update_fields=update_fields, raw=raw, using=using,
)
所以Algolia的AUTO_INDEXING
只能依赖这些作为trigger to update your records:
if (isinstance(auto_indexing, bool) and
auto_indexing) or self.__auto_indexing:
# Connect to the signalling framework.
post_save.connect(self.__post_save_receiver, model)
pre_delete.connect(self.__pre_delete_receiver, model)
def __post_save_receiver(self, instance, **kwargs):
"""Signal handler for when a registered model has been saved."""
logger.debug('RECEIVE post_save FOR %s', instance.__class__)
self.save_record(instance, **kwargs)
def __pre_delete_receiver(self, instance, **kwargs):
"""Signal handler for when a registered model has been deleted."""
logger.debug('RECEIVE pre_delete FOR %s', instance.__class__)
self.delete_record(instance)
考虑到这一点,AUTO_INDEXING
无法猜测您是否调用了 instance.save()
因为您创建了一个新实例 或 因为您的模型已更新,更新了所有现有实例。 AUTO_INDEXING
旨在每次 实例更新时 触发索引(通过添加新字段、更改当前字段、添加实例或删除实例),以确保您的 Algolia索引始终与您的 Django 数据库同步。
如果您想使用 Algolia 实现更自定义的同步处理,您可以:
- 禁用
AUTO_INDEXING
在您的
apps.py
中向 Algolia 注册您的模型后,连接到适当的信号:algoliasearch.register(Contact, ContactIndex) post_save.connect(my_post_save_receiver, ContactIndex) pre_delete.connect(my_pre_delete_receiver, ContactIndex)
并且在您的
my_post_save_receiver
/my_pre_save_receiver
方法中,处理 Signal 参数以决定是否要调用index.save_record(instance)
.
看看 Signals documentation and the Signals guide. Please note the duplicate signals 部分。