[英]Django Haystack - Filter by substring of a field using SearchQuerySet ()
I have a Django project that uses SOLR for indexing. 我有一个使用SOLR进行索引的Django项目。
I'm trying to do a substring search using Haystack's SearchQuerySet class. 我正在尝试使用Haystack的SearchQuerySet类进行子字符串搜索 。
For example, when a user searches for the term "ear" , it should return the entry that has a field with the value: "Search" . 例如,当用户搜索术语“ ear”时 ,它应返回具有值为“ Search”的字段的条目。 As you can see, "ear" is a SUBSTRING of "Search" .
如您所见, “ ear”是“ Search”的SUBSTRING。 (obviously :))
(显然:))
In other words, in a perfect Django world I would like something like: 换句话说,在一个完美的Django世界中,我想要这样的东西:
SearchQuerySet().all().filter(some_field__contains_substring='ear')
In the haystack documentation for SearchQuerySet ( https://django-haystack.readthedocs.org/en/latest/searchqueryset_api.html#field-lookups ), it says that only the following FIELD LOOKUP types are supported: 在SearchQuerySet的干草堆文档( https://django-haystack.readthedocs.org/en/latest/searchqueryset_api.html#field-lookups )中,它说仅支持以下FIELD LOOKUP类型:
I tried using __ contains , but it behaves exactly like 我尝试使用__ contains ,但其行为与 __exact , which looks up the exact word (the whole word) in a sentence, not a substring of a word.
__exact ,它查找句子中的确切单词(整个单词),而不是单词的子串。
I am confused, because such a functionality is pretty basic, and I'm not sure if I'm missing something, or there is another way to approach this problem (using Regex or something?). 我很困惑,因为这样的功能是非常基本的,而且我不确定是否丢失了某些东西,或者有另一种方法来解决此问题(使用Regex或其他东西?)。
Thanks 谢谢
That could be done using EdgeNgramField field: 可以使用EdgeNgramField字段完成此操作:
some_field = indexes.EdgeNgramField() # also prepare value for this field or use model_attr
Then for partial match: 然后进行部分匹配:
SearchQuerySet().all().filter(some_field='ear')
It's a bug in haystack. 这是大海捞针中的错误。
As you said, __exact
is implemented exactly like __contains
and therefore this functionality does not exists out of the box in haystack. 正如您所说,
__exact
的实现与__contains
完全相同,因此,此功能在干草堆中并不存在。
The fix is awaiting merge here: https://github.com/django-haystack/django-haystack/issues/1041 该修复程序正在这里等待合并: https : //github.com/django-haystack/django-haystack/issues/1041
You can bridge the waiting time for a fixed release like this: 您可以像这样固定发布的等待时间:
from haystack.inputs import BaseInput, Clean
class CustomContain(BaseInput):
"""
An input type for making wildcard matches.
"""
input_type_name = 'custom_contain'
def prepare(self, query_obj):
query_string = super(CustomContain, self).prepare(query_obj)
query_string = query_obj.clean(query_string)
exact_bits = [Clean(bit).prepare(query_obj) for bit in query_string.split(' ') if bit]
query_string = u' '.join(exact_bits)
return u'*{}*'.format(query_string)
# Usage:
SearchQuerySet().filter(content=CustomContain('searchcontentgoeshere'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.