简体   繁体   English

如何在IntegerField上过滤Haystack SearchQuerySet for None

[英]How can I filter a Haystack SearchQuerySet for None on an IntegerField

This is driving me a bit mad but seems like it should be simple. 这让我有点生气,但看起来应该很简单。

I'm using Django and Haystack and have a search index including an IntegerField which allows null. 我正在使用Django和Haystack并且有一个包含允许null的IntegerField的搜索索引。 This is based on a related model in Django, but I don't think this matters. 这是基于Django的相关模型,但我认为这不重要。 eg: 例如:

class ThingIndex(indexes.ModelSearchIndex, indexes.Indexable):
    group = indexes.IntegerField(model_attr='group__id', null=True)

    class Meta:
        model = Thing

I sometimes want my Haystack query to return items with None/Null for this field, so I'm filtering in the search form's __init__, but I can't get a query to do this. 我有时希望我的Haystack查询返回此字段的None / Null项,所以我在搜索表单的__init__中进行过滤,但我无法获得查询来执行此操作。 The most obvious way I tried was: 我尝试过的最明显的方法是:

self.searchqueryset.filter(group__isnull=True)  # how to do it on a regular Django queryset

But this returns no records. 但这不会返回任何记录。

Right now I'm working around it with this: 现在我正在解决这个问题:

self.searchqueryset.exclude(group__in=range(1,100))

Which works, but obviously isn't the way it should be done :) 哪个有效,但显然不是它应该做的方式:)

Can anyone help? 有人可以帮忙吗?

Thanks! 谢谢!

If you are using ElasticSearch, the solution can be done without patching, just using native ElasticSearch: 如果您正在使用ElasticSearch,则只需使用本机ElasticSearch就可以在不进行修补的情况下完成解决方案:

from haystack.inputs import Raw
self.searchqueryset.exclude(group = Raw("[* TO *]"))

Other way around, filter all documents that have non-emtpy group field: 换句话说,过滤所有具有非emtpy组字段的文档:

from haystack.inputs import Raw
self.searchqueryset.filter(group = Raw("[* TO *]"))

This could work for SOLR too and for other Haystack backends applying the same concept but with specific syntax of the backend search language. 这也适用于SOLR以及其他Haystack后端应用相同的概念,但具有后端搜索语言的特定语法。

References: 参考文献:

I feel this question was not answered. 我觉得这个问题没有得到解答。 It seems the op was asking how to filter for null entries using haystack.query.SearchQuerySet with an ElasticSearch backend. 似乎op正在询问如何使用带有ElasticSearch后端的haystack.query.SearchQuerySet来过滤空条目。

In the example above, replace 在上面的示例中,替换

self.searchqueryset.filter(group__isnull=True)

with

self.searchqueryset.filter(_missing_='group')

Not intuitive, but its the only way I have gotten this to work so far. 不直观,但它是我到目前为止工作的唯一方式。

If you're using SOLR, you'd probably like to have a look at the following links: 如果您正在使用SOLR,您可能希望查看以下链接:

1) https://github.com/toastdriven/django-haystack/commit/9332a91a7f0e4b33d7e20aa892d156305c12dfe3 1) https://github.com/toastdriven/django-haystack/commit/9332a91a7f0e4b33d7e20aa892d156305c12dfe3
2) https://github.com/toastdriven/django-haystack/issues/163 2) https://github.com/toastdriven/django-haystack/issues/163

There's a patch for SOLR, allowing such queries, but for other backends there's probably none. 有一个SOLR的补丁,允许这样的查询,但对于其他后端,可能没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM