The phrase search capability is integrated into the text search data type tsquery
. The text search operator @@
you display takes a tsvector
to the left and a tsquery
to the right. And a tsvector
can be built from any character type as well as from a JSON document.
Related:
You can convert your json
or jsonb
document to a text search vector with one of the dedicated functions:
to_tsvector()
json(b)_to_tsvector()
Note that these only include values from the JSON document, not keys. Typically, that's what you want.
Basic example:
SELECT to_tsvector(jsonb '{"foo":"jump quickly"}')
@@ to_tsquery('jump <-> quick:*');
Demonstrating prefix matching on top of phrase search while being at it. See:
Alternatively, you can simply create the tsvector
from the text
representation of your JSON document to also include key names:
SELECT to_tsvector((jsonb '{"foo-fighter":"jump quickly"}')::text)
@@ to_tsquery('foo <-> fight:*');
Produces a bigger tsvector
, obviously.
Both can be indexed (which is the main point of text search). Only indexes are bound to relational tables. (And you can index the expression!)
The expression itself can be applied to any value, not bound to tables like you seem to imply.