问题
Does the phrase search operator <->
work with JSONB documents or only relational tables in PostgreSQL?
I haven't experimented with this yet, as I haven't yet set up Postgres hosting. The answer to this question will help determine what database and what tools I will be using.
I found this sample code at: https://compose.com/articles/mastering-postgresql-tools-full-text-search-and-phrase-search/:
SELECT document_id, document_text FROM documents
WHERE document_tokens @@ to_tsquery('jump <-> quick');
I just need to know if this operator is supported by JSONB documents.
回答1:
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:
- Match a phrase ending in a prefix with full text search
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:
- Get partial match from GIN indexed TSVECTOR column
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.
来源:https://stackoverflow.com/questions/57763662/does-the-phrase-search-operator-work-with-jsonb-documents-or-only-relational