简体   繁体   中英

Django SearchVector doesn't work with search query containing whitespace

I have a name field on which I am trying to annotate with a SearchVector. It works fine if I don't add a whitespace in search string but returns empty list if I add a whitespace. Same string works just fine with regular filter queryset.

>>> r = Resource.objects.filter(name__icontains='LAKSHMI NURSING')
>>> r
<QuerySet [<Resource: LAKSHMI NURSING HOME>]>
>>>

Using Search vector without a white-space string

>>> r = Resource.objects.annotate(
...             search=SearchVector('name', 'type')
...             ).filter(search__icontains='LAKSHMI')
>>> r
<QuerySet [<Resource: LAKSHMI NURSING HOME>]>
>>>

With White-space:

>>> r = Resource.objects.annotate(
...             search=SearchVector('name', 'type')
...             ).filter(search__icontains='LAKSHMI NURSING')
>>> r
<QuerySet []>
>>>

What are the results if you try:

r = Resource.objects.annotate(
...             search=SearchVector('name', 'type')
...             ).filter(search='LAKSHMI NURSING')

without icontains? In the doc , I do not see example with search__icontains .

Another option could be to use SearchQuery :

    from django.contrib.postgres.search import SearchVector, SearchQuery
    queryset = queryset.annotate(
        search=SearchVector(*args)
    ).filter(search=SearchQuery(search_text))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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