Django干草堆索引在模型中的许多领域不起作用 [英] Django Haystack indexing is not working for many to many field in model

查看:217
本文介绍了Django干草堆索引在模型中的许多领域不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我们的django应用程序中使用haystack进行搜索和搜索工作非常好。但是我有一个reamtime搜索的问题。对于实时搜索,我使用的是haystack的默认RealTimeSignalProcessor(haystack.signals.RealtimeSignalProcessor)。我的模型包含一个很多的领域。当数据更改为这个多个到多个字段时,似乎realtimesignal处理器没有正确更新索引数据。更新了很多数据后,我的搜索结果错误。

I am using haystack in our django application for search and search is working very fine. But I am having an issue with reamtime search. For realtime search I am using haystack's default RealTimeSignalProcessor(haystack.signals.RealtimeSignalProcessor). My model contains one many to many field in it. When data is changed for this many to many field only, it seems the realtimesignal processor is not updating indexing data properly. After updating the many to many data, I am getting wrong search result.

它在手动运行rebuild_index命令后工作。我认为rebuild_index正在工作,因为它首先进行清理,然后再次建立索引数据。

Its working after manually running rebuild_index command. I think rebuild_index is working because its doing cleaning first and then again building indexing data.

有人可以提出解决问题的方法吗?

Can someone suggest some solution to the problem ?

以下是代码。

型号:

class Message_forum(models.Model):
      message = models.ForeignKey(Message)
      tags = models.ManyToManyField(Tag, blank=True, null=True) #this is many to many field

search_index.py:

class Message_forumIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.EdgeNgramField(document=True, use_template=True)
    message = indexes.CharField(model_attr='message', null=True)
    tags = indexes.CharField(model_attr='tags', null=True)

    def get_model(self):
        return Message_forum

    def index_queryset(self, using=None):
        return self.get_model().objects.all()

    def prepare_tags(self, obj):
        return [tag.tag for tag in obj.tags.all()]

索引模板:

{{ object.tags.tag }}

settings.py:

HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'

我拥有最新版本的haystack和whoosh作为后端。

I am having latest version of haystack and whoosh as back-end.

推荐答案

在挖掘干草堆代码后,我已经弄清楚了。

I have figured it out after digging into code of haystack.

在haystack默认的RealTimeSignalProcessor中,其连接post_save和post_delete信号应用模式。现在在handle_save方法中正在调用post_save和post_delete信号。在这种方法中,haystack正在验证发件人,在我的情况下,对于标签(多对多)字段,Message_forum_tag模型正在作为发件人传递。现在,该模型的索引不存在于我的search_index中,因为它不是我的应用程序模型,而是django生成的。因此,在handle_save方法中,它绕过了此模型的任何更改,因此它不会更新已更改对象的索引数据。

In haystack default RealTimeSignalProcessor, its connecting post_save and post_delete signals of each model of application. Now in handle_save method is being called in post_save and post_delete signal. In this method haystack is validating the sender and in my case for tags(many-to-many) field, Message_forum_tag model is being passed as sender. Now index for this model is not present into my search_index since its not my application model but instead django's generated one. And so in handle_save method it was bypassing any changes on this model and hence it wasn't updating indexed data for changed object.

所以我已经找出了两个不同的解决方案这个问题。

So I have figured out two different solution for this problem.


  1. 我可以创建特定于我的模型Message_forum的自定义实时信号处理器,在这种安装方法中我可以连接m2mchanged使用handle_save在Message_forum中的多对多字段上发出信号。同时,我可以传递Message_forum作为发件人,以便haystack将通过验证(不完全验证,但试图获取其索引obj),并将更新已更改对象的索引数据。

  1. I can create custom realtime signal processor specific to my model Message_forum, in this in setup method I can connect m2mchanged signal on each many-to-many fields in Message_forum with handle_save. At the same time I can pass Message_forum as sender so that haystack will pass the validation(not exactly validation but its trying to get its index obj) around it and will update the index data of changed object.

另一种方法是确保每当任何多对多字段被更改时,其父级(这里Message_forum.save())的保存方法正在被调用。所以它会始终调用post_save信号,之后haidstack将更新索引对象数据。

The other way is to ensure that whenever any many-to-many field is being changed, save method of its parent(here Message_forum.save()) is being called. And so it will always invoke post_save signal and after that haystack will update the index object data.

花费大约3几个小时来弄清楚。希望这可以帮助有同样问题的人。

Have spend around 3 hours to figure it out. Hope this will help someone having same problem.

这篇关于Django干草堆索引在模型中的许多领域不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆