Django haystack,如何搜索ManyToMany相关字段?

发布于 2024-09-16 08:49:33 字数 1355 浏览 9 评论 0原文

我已将 MultivaluedField 添加到我的索引 (haystack),我需要搜索 ManyToMany 相关字段,但它不起作用。

引擎发出“嗖”的一声。

我的索引如下所示:

 class PostIndex(SearchIndex):
     text = CharField(document=True, use_template=True)
     author = CharField(model_attr='author') 
     body = CharField(model_attr='body') 
     pub_date = DateTimeField(model_attr='publish') 
     regions = MultiValueField() 
 
 def prepare_regions(self, obj):
     return [region.name for region in obj.regions.all()]

我的模型如下所示:

 class Post(models.Model):

     title           = models.CharField(_('title'), max_length=200)
     author          = models.ForeignKey(User, blank=True, null=True)
     body            = models.TextField(_('body'), )
     allow_comments  = models.BooleanField(_('allow comments'), default=True)
     publish         = models.DateTimeField(_('publish'), default=datetime.datetime.now)
     categories      = models.ManyToManyField(Category, blank=True)
     tags            = TagField()
     objects         = PublicManager()

     regions         = models.ManyToManyField(Region, blank=True)

如果我使用 SearchQuerySet().filter(region__in=words_list) 它就可以工作。问题是我不知道用户何时搜索某个区域或另一个字段,因此我必须使用 SearchQuerySet().filter(content__icontains=words_list)。这样就什么也找不到了。

谢谢

谢谢!!

I've added a MultivaluedField to my index (haystack), I need to search for a ManyToMany related field, but it doesn't work.

The engine is WHOOSH.

This how my index looks like:

 class PostIndex(SearchIndex):
     text = CharField(document=True, use_template=True)
     author = CharField(model_attr='author') 
     body = CharField(model_attr='body') 
     pub_date = DateTimeField(model_attr='publish') 
     regions = MultiValueField() 
 
 def prepare_regions(self, obj):
     return [region.name for region in obj.regions.all()]

And this how my model looks like:

 class Post(models.Model):

     title           = models.CharField(_('title'), max_length=200)
     author          = models.ForeignKey(User, blank=True, null=True)
     body            = models.TextField(_('body'), )
     allow_comments  = models.BooleanField(_('allow comments'), default=True)
     publish         = models.DateTimeField(_('publish'), default=datetime.datetime.now)
     categories      = models.ManyToManyField(Category, blank=True)
     tags            = TagField()
     objects         = PublicManager()

     regions         = models.ManyToManyField(Region, blank=True)

If I use SearchQuerySet().filter(region__in=words_list) it works. The problem is that I don't know when the user is searching for a region or another field, so I have to use SearchQuerySet().filter(content__icontains=words_list). And in this way nothing is found.

Thanks

Thanks!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七色彩虹 2024-09-23 08:49:33

尝试 :

class PostIndex(SearchIndex):
 text = CharField(document=True, use_template=True)
 author = CharField(model_attr='author') 
 body = CharField(model_attr='body') 
 pub_date = DateTimeField(model_attr='publish') 

 regions = CharField(model_attr='regions')

Try :

class PostIndex(SearchIndex):
 text = CharField(document=True, use_template=True)
 author = CharField(model_attr='author') 
 body = CharField(model_attr='body') 
 pub_date = DateTimeField(model_attr='publish') 

 regions = CharField(model_attr='regions')
去了角落 2024-09-23 08:49:33

您只需将区域 ID 添加到区域的索引中。

尝试

    def prepare_regions(self, obj):
        return [region.pk for region in obj.regions.all()]

You only add region id to the index for Region.

Try

    def prepare_regions(self, obj):
        return [region.pk for region in obj.regions.all()]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文