干草堆搜索很多到很多领域都不行 [英] Haystack search on a many to many field is not working

查看:107
本文介绍了干草堆搜索很多到很多领域都不行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在具有多个字段的模型上运行搜索,并且我想使用此字段过滤搜索。

I'm trying to run a search on a model that has a many to many field, and I want to filter the search using this field.

这是我当前的代码:

search_indexes.py

search_indexes.py

class ListingInex(indexes.SearchIndex, indexes.Indexable):
   text = indexes.CharField(document=True, use_template=True)
   business_name = indexes.CharField(model_attr='business_name')
   category = indexes.MultiValueField(indexed=True, stored=True)
   city = indexes.CharField(model_attr='city')
   neighborhood= indexes.CharField(model_attr='neighborhood')
   state = indexes.CharField(model_attr='state')
   address = indexes.CharField(model_attr='address')
   zip_code = indexes.CharField(model_attr='zip_code')
   phone_number = indexes.CharField(model_attr='phone_number')

    def get_model(self):
          return listing

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

    def prepare_category(self, obj):
       return [category.name for category in obj.category_set.all()]

listing_text.txt

listing_text.txt

{{ object.business_name }}
{{ object.state }}
{{object.city}}
{{object.zip_code}}
{{object.phone_number}} 
{{object.neighborhood}}
{% for category in obj.category.all %}
{{ category.name }}
{% endfor %}

尝试做一个简单的搜索,如:

I'm trying to do a simple search like:

search_results = SearchQuerySet().filter(category=query_text).filter

它返回一个空列表。感谢提前。

it returns an empty list. Thanks in advance.

推荐答案

阅读代码我认为问题在这里:

Reading your code I think the problem is here:

{% for category in obj.category.all %}
{{ category.name }}
{% endfor %}

类别不是一个对象,是一个MultiValueField。也许如果你尝试这样的东西:

Category is not an object, is a MultiValueField. Maybe if you try something like:

{% for category in object.category.all %}
{{ category.name }}
{% endfor %}

object.category.all 而不是obj.category.all。我不知道解决方案是这样的,但我确定错误可能在这3行。

object.category.all instead of obj.category.all. I'm not sure the solution is this, but I'm pretty sure the error could be in this 3 lines.

这篇关于干草堆搜索很多到很多领域都不行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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