0

I have a project with Django, DRF and PostgreSQL. And I need to make a search for some data in this project. In this search I need logical operators (AND, OR, NOT), searching by prefix (like manch*) and case insensitivity. So I decided to use PostgreSQL full text search like this

class VariableSearch(generics.ListAPIView):
    serializer_class = VariableSerializer
    
    def get_queryset(self):
        q = self.request.query_params.get('query')
        
        if q:
            vector = SearchVector('name', 'label')
            query = SearchQuery(q, search_type='raw')
            queryset = Variable.objects.annotate(search=vector).filter(search=query)
        else:
            queryset = Variable.objects.none()
        return queryset

So with this type of search I can do queries like this

http://127.0.0.1:8000/search_var/?query=полит:* | инфо:*

And operator OR and searching by prefix working good.

But when I'm trying to use AND (&) on NOT (!) it's giving me results without this logical AND or just syntax error in tsquery: "word !not" for queries like this

http://127.0.0.1:8000/search_var/?query=word !not

So how to make AND and NOT work properly?

  • Are you writing that url in the browser manually? Do note that the ampersand i.e. `&` is used to separate parameters in a query string (example: `?foo=1&bar=2`). You need to url encode the `&` if you want to specify a value having it. – Abdul Aziz Barkat Oct 27 '22 at 15:20
  • Yeah you was right! I just encoded ```& -> %26``` And now it's working fine. Thank you! – Iggy Comdore Oct 27 '22 at 19:51
  • Does this answer your question? [Escaping ampersand in URL](https://stackoverflow.com/questions/16622504/escaping-ampersand-in-url) – Abdul Aziz Barkat Oct 28 '22 at 04:34
  • Partially. For AND operator it's working with encoding. And for NOT operator it's working with AND before so we need to do like this ```operand1 & ! operand2``` – Iggy Comdore Oct 28 '22 at 06:53

1 Answers1

0

add another query to url

example:

?word_status=True

Or you can but ‘or’, ‘and’ and get this value to apply it with your query( depending on it )

Hashem
  • 595
  • 1
  • 3
  • 8