我有一个name
字段,我正试图用SearchVector对其进行注释。若我不在搜索字符串中添加空白,它可以很好地工作,但若我添加空白,则返回空列表。相同的字符串适用于常规filter
查询集。
>>> r = Resource.objects.filter(name__icontains='LAKSHMI NURSING')
>>> r
<QuerySet [<Resource: LAKSHMI NURSING HOME>]>
>>>
使用没有空白字符串的搜索矢量
>>> r = Resource.objects.annotate(
... search=SearchVector('name', 'type')
... ).filter(search__icontains='LAKSHMI')
>>> r
<QuerySet [<Resource: LAKSHMI NURSING HOME>]>
>>>
带空白:
>>> r = Resource.objects.annotate(
... search=SearchVector('name', 'type')
... ).filter(search__icontains='LAKSHMI NURSING')
>>> r
<QuerySet []>
>>>
如果您尝试会得到什么结果
r = Resource.objects.annotate(
... search=SearchVector('name', 'type')
... ).filter(search='LAKSHMI NURSING')
没有图标?在文档中,我没有看到search__icontains
的示例。
另一种选择是使用SearchQuery
:
from django.contrib.postgres.search import SearchVector, SearchQuery
queryset = queryset.annotate(
search=SearchVector(*args)
).filter(search=SearchQuery(search_text))