Phrase Search Followed By Operator
Require two words adjacent and in order with to_tsquery's <-> (FOLLOWED BY) operator, so 'btree <-> index' misses documents where the pair is swapped.
Code
CREATE TABLE docs (
id int PRIMARY KEY,
body text NOT NULL
);
INSERT INTO docs VALUES
(1, 'Btree index tuning for large tables'),
(2, 'Index btree internals explained'),
(3, 'Tuning guide for planners');
-- 短语搜索:to_tsquery 的 <->(FOLLOWED BY)要求两词相邻且按序
SELECT id
FROM docs
WHERE to_tsvector('english', body) @@ to_tsquery('english', 'btree <-> index');
-- 换序不命中:短语有方向;n 个词用 n-1 个 <-> 串联
SELECT id
FROM docs
WHERE to_tsvector('english', body) @@ to_tsquery('english', 'index <-> btree');How it works
What runs here is SELECT / INSERT / CREATE — docs are the objects involved.
Breaking the statements into their working parts: a WHERE clause constraining which rows the statement operates on; NOT NULL qualifiers on the required columns; a PRIMARY KEY that enforces row identity.
Putting it to use
The docs table is the working surface here.
Core lines
SELECT id
FROM docs
WHERE to_tsvector('english', body) @@ to_tsquery('english', 'btree <-> index');
SELECT id
FROM docs
WHERE to_tsvector('english', body) @@ to_tsquery('english', 'index <-> btree');
INSERT INTO docs VALUES
(1, 'Btree index tuning for large tables'),
(2, 'Index btree internals explained'),
(3, 'Tuning guide for planners');
Interactive Tools for This Topic
PostgreSQL EXPLAIN AnalyzerAnalyze PostgreSQL EXPLAIN ANALYZE text or FORMAT JSON plans locally to flag Seq Scans, slow nodes, row misestimates, disk sorts, and index hints.SQL DDL to TypeScript ConverterConvert SQL CREATE TABLE DDL statements into strict TypeScript interfaces with nullable, array, enum, JSONB, and Date or ISO string column mappings.Multi-Mode Escape & Unescape CodecEscape and unescape strings safely across JSON, JavaScript, SQL single-quoted literals, POSIX shell single quotes, and RFC 4180 CSV cells in your browser.Fuzzy Match TesterTest subsequence fuzzy matching: check whether pattern characters appear in order inside a text, with positions and highlighting.
FAQ
- Which tables or CTEs does it reference?
- `docs`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.