---
title: PostgreSQL GIN 索引与全文搜索(tsvector)实践
keywords: tsvector, to_tsvector, to_tsquery, GIN, plainto_tsquery
description: 使用 tsvector 与 GIN 索引实现高性能全文搜索,示例包含索引与查询语法。
tags:
- GIN
- PostgreSQL
- plainto_tsquery
- to_tsquery
- to_tsvector
- tsvector
- 全文搜索
- 数据库
categories:
- 文章资讯
- 技术教程
---
初始化与索引:
CREATE TABLE docs (
id bigserial PRIMARY KEY,
title text NOT NULL,
body text NOT NULL
);
CREATE INDEX idx_docs_search ON docs USING GIN (to_tsvector('english', title || ' ' || body));
查询(标准与简单词法):
SET default_text_search_config = 'english';
SELECT id, title FROM docs
WHERE to_tsvector(title || ' ' || body) @@ to_tsquery('cloud & native');
SELECT id, title FROM docs
WHERE to_tsvector(title || ' ' || body) @@ plainto_tsquery('cloud native');

发表评论 取消回复