1

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 []>
>>>
MohitC
  • 4,541
  • 2
  • 34
  • 55

1 Answers1

2

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))
Henri
  • 780
  • 4
  • 10