elasticsearch,django-haystack,Python,Django,elasticsearch,Django Haystack" /> elasticsearch,django-haystack,Python,Django,elasticsearch,Django Haystack" />

Python Django Haystack:应该更新吗;是否使用RealtimeSignalProcessor触发?

Python Django Haystack:应该更新吗;是否使用RealtimeSignalProcessor触发?,python,django,elasticsearch,django-haystack,Python,Django,elasticsearch,Django Haystack,我有一个模型,它有一个布尔字段,用于说明模型对象是否处于活动状态。我基本上希望Haystack忽略任何active=False的对象。这适用于使用以下各项进行完整的重新索引: def index_queryset(self, using=None): return ExampleModel.objects.filter(active=True) 但是,当对象被更新且索引被实时更新且未完全重新编制索引时,例如当将对象更改为非活动时,以下操作不起作用,并且似乎没有调用: def shoul

我有一个模型,它有一个布尔字段,用于说明模型对象是否处于活动状态。我基本上希望Haystack忽略任何active=False的对象。这适用于使用以下各项进行完整的重新索引:

def index_queryset(self, using=None):
    return ExampleModel.objects.filter(active=True)
但是,当对象被更新且索引被实时更新且未完全重新编制索引时,例如当将对象更改为非活动时,以下操作不起作用,并且似乎没有调用:

def should_update(self, instance):
    if instance.active:
        return True
    self.remove_object(instance)
    return False
当对象标记为非活动时,我希望将其从索引中删除,但是在将对象更新为非活动时,它仍保留在索引中,影响面计数等。我使用manage.py update\u index进行了检查,并且应该更新代码似乎没有运行

有关信息,我正在使用haystack dev和elasticsearch最新版本


有什么想法吗?

查看源代码,默认情况下返回True,这意味着重新索引。此外,它附加到类的post delete钩子,这可能是由于您没有删除记录而没有调用它的原因

您应该能够通过如下方式稍微更改代码来触发索引删除:

def should_update(self, instance, **kwargs):
    if instance.active:
        return True
    else:
        self.remove_object(instance, **kwargs)
        return False
或者反过来说:

def should_update(self, instance, **kwargs):
    if not instance.active:
        self.remove_object(instance, **kwargs)
    return instance.active
另一个选项是创建一个CRON脚本,该脚本执行以下操作:

import haystack
from yourapp.models import ExampleModel

for obj in ExampleModel.objects.filter(active=False):
    haystack.site.remove_object(obj)
这也可以被Django中的信号使用

详细说明:我没有测试任何这段代码。这是基于问题中提供的信息的理论

  • 在应用程序目录中创建signals.py文件
  • 编辑signals.py:

    from haystack import signals
    from .models import Product
    from haystack.exceptions import NotHandled
    
    class ProductUpdateSignalProcessor(signals.RealtimeSignalProcessor):
    
        def handle_save(self, sender, instance, **kwargs):
            if isinstance(instance, Product):
                using_backends = self.connection_router.for_write(instance=instance)
                for using in using_backends:
                    try:
                        index = self.connections[using].get_unified_index().get_index(sender)
                        if instance.active:
                            index.update_object(instance, using=using)
                        else:
                            index.remove_object(instance, using=using)
                    except NotHandled:
                        print(NotHandled.message)
            else:
                super(ProductUpdateSignalProcessor, self).handle_save(sender, instance, **kwargs)
    
  • 编辑settings.py:

    HAYSTACK_SIGNAL_PROCESSOR = 'products.signals.ProductUpdateSignalProcessor'
    

  • 您是否尝试运行了
    python manage.py update\u index--remove
    ?drowness,是的,这是一种享受-但是我希望使用RealTimeSignalProcessor,这意味着除非运行它,否则索引将不同步?-此外,它还可以删除一个对象,但当没有可索引的对象时,它仍然会在索引中保留项目?嗨,我已经尝试过了,像以前一样,它似乎没有被调用。我在CMS项目中看到了一些与我的代码完全相同的代码,因此我无法理解为什么它会被忽略。我更新到了最新的开发版本,它仍然是一样的-应该更新代码根本没有运行吗?