PostgreSQL 全量文本搜索与数据库测试指南
1. PostgreSQL 全量文本搜索
PostgreSQL 具备全量文本搜索功能,能够克服 SQL 模式匹配操作符(如 LIKE 和 ILIKE)的局限性,提升文本搜索性能。
1.1 传统匹配操作符的局限性
- 索引使用限制 :即便支持使用
text_pattern_ops
类对文本创建索引,但该索引无法用于非锚定文本搜索。例如:
CREATE TABLE document(
document_id serial primary key,
document_body text
);
CREATE INDEX on document (document_body text_pattern_ops);
INSERT INTO document VALUES (default, 'Can not use text_pattern_op class to search for non-anchored text');
-- 锚定文本搜索
car_portal=# EXPLAIN SELECT * FROM document WHERE document_body like 'Can%text_pattern';
QUERY PLAN
-----------------------------------------------------------------------