Home Django SearchVector using icontains
Reply: 0

Django SearchVector using icontains

user1176
1#
user1176 Published in April 22, 2018, 10:10 pm

I am trying to search for a list of values in multiple columns in postgres (via django). I was able to use SearchQuery and SearchVector and this works great if one of the search values matches a full word. I was hoping to use icontains so that partial strings could also be used in the search. Is this possible and if so could someone point me in the right direction. Here is an example of my approach below.

Example Data:

Superhero.objects.create(superhero='Batman', publisher='DC Comics', alter_ego="Bruce Wayne")
Superhero.objects.create(superhero='Hulk', publisher='Marvel Comics', alter_ego="Bruce Banner")

Django filter:

from django.contrib.postgres.search import SearchQuery, SearchVector

    query = SearchQuery('man') | SearchQuery('Bruce') 
    vector = SearchVector('superhero', 'alter_ego', 'publisher')
    self.queryset = self.queryset.annotate(search=vector).filter(search=query)

This would return the Hulk record but I am hoping I can somehow use like 'icontains' so that when searching for 'man' the Batman record would also be returned. Any help is appreciated!

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.308862 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO