SearchQuery websearch drops parentheses in queries

So, I’m on Django 3.2 and Postgres 12, and trying to use SearchQuery with search_type='websearch'.

According to the documentation, this example shows that parentheses should be relevant in a “websearch” query, and implies that the “raw” and “websearch” queries are identical:

>>> SearchQuery("'tomato' & ('red' | 'green')", search_type='raw')
>>> SearchQuery("'tomato' ('red' OR 'green')", search_type='websearch')

I am interpreting both examples as “tomato AND (red OR green)”, which expands in plain language to “give me results that include both tomato and red, and results that include both tomato and green”.

However, when I attempted this query, I receive results that are different in “websearch” than in “raw”. The “raw” search_type returns the results I expect. However, the “websearch” search_type becomes translated to a raw query of 'tomato' & 'red' | 'green', dropping the parentheses grouping entirely. This means you will receive results that include “green” but not “tomato.”

Am I misunderstanding the documentation or implementing websearch incorrectly? It appears to me that "tomato' ('red' OR 'green')" should be different from "tomato' 'red' OR 'green'" but in practice there is no difference in results at all.

I would appreciate any help on this! Thank you.

The text search documentation on Postgres declares the difference, the main point in websearch search type is:

  • unquoted text : text not inside quote marks will be converted to terms separated by & operators, as if processed by plainto_tsquery .
  • "quoted text" : text inside quote marks will be converted to terms separated by <-> operators, as if processed by phraseto_tsquery .
  • OR : the word “or” will be converted to the | operator.
  • - : a dash will be converted to the ! operator.

Other punctuation is ignored. So like plainto_tsquery and phraseto_tsquery , the websearch_to_tsquery function will not recognize tsquery operators, weight labels, or prefix-match labels in its input.

Thanks @Rigo-Villalta. If that’s the case, shouldn’t this be changed in the documentation? It’s misleading to imply that the parentheses are relevant to creating a websearch type of search query.

I am not sure if in the slang of full text search the term “web search” is a defacto for this kind of search, but yes, IMHO the Django Documentation should at least have the clarification that the Postgres documentation owns. Until I understand you can fork Django and made a pull request.

There are some folks that collaborate with the source code and documentation here in the forum, I hope they read the thread.