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.

SqlData Fetching

Code

snippet
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

FAQ

Which tables or CTEs does it reference?
`docs`.