PostgreSQL hỗ trợ FULL TEXT SEARCH như thế nào?
PostgreSQL hỗ trợ FULL TEXT SEARCH như thế nào?
PostgreSQL có full-text search (FTS) tích hợp dựa trên tsvector và tsquery.
SELECT to_tsvector('english', 'The quick brown foxes jumped') @@ to_tsquery('english', 'fox & jump'); -- true
-- Cột tìm kiếm tính sẵn + index GIN
ALTER TABLE articles ADD COLUMN tsv tsvector
GENERATED ALWAYS AS (setweight(to_tsvector('simple', coalesce(title,'')), 'A') ||
setweight(to_tsvector('simple', coalesce(body,'')), 'B')) STORED;
CREATE INDEX idx_articles_tsv ON articles USING GIN (tsv);
SELECT id, title, ts_rank(tsv, q) AS rank
FROM articles, websearch_to_tsquery('simple', 'kiến trúc "microservice" -monolith') q
WHERE tsv @@ q
ORDER BY rank DESC LIMIT 20;
Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.