skip to Main Content

I have a project with Django, Django REST Framework and PostgreSQL.
And my goal to make a search with certain conditions:

  • logical operators (AND, OR, NOT)
  • case insensitive
  • operator like * To search by prefixes. som* -> search for some, somali and so on

My first attempt was to use Postgres full search with this type
search_type='websearch'

It’s all good but don’t have operator *
So I switched to raw type search and my class for search looks like it now

class DataSearch(generics.ListAPIView):
    serializer_class = DataSerializer
    
    def get_queryset(self):
        q = self.request.query_params.get('query')
        
        if q:
            vector = SearchVector('research', 'data', 'research__name')
            query = SearchQuery(q, search_type='raw')
            queryset = Data.objects.annotate(search=vector).filter(search=query)
        else:
            queryset = Data.objects.none()
        return queryset

Logical operator works, search by prefixes works with :* but I don’t know how to make it case insensitive.

Is it possible to make this type of search case insensitive? Or maybe there are another option for it?

2

Answers


  1. Please try:

    SearchQuery(q, search_type=’raw’, config=’english’)

    OR

    SearchQuery(q, config=’pg_catalog.simple’, search_type=’raw’)

    Login or Signup to reply.
  2. This article contains more details about search in Django https://docs.djangoproject.com/en/4.0/ref/contrib/postgres/search/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search