【发布时间】:2020-11-01 22:16:58
【问题描述】:
我有下表:
postgres=# \d so_rum;
Table "public.so_rum"
Column | Type | Collation | Nullable | Default
-----------+-------------------------+-----------+----------+---------
id | integer | | |
title | character varying(1000) | | |
posts | text | | |
body | tsvector | | |
parent_id | integer | | |
Indexes:
"so_rum_body_idx" rum (body)
我想做短语搜索查询,所以我想出了以下查询,例如:
select id from so_rum
where body @@ phraseto_tsquery('english','Is it possible to toggle the visibility');
这给了我结果,它只匹配整个文本。但是,有一些文档,其中词法之间的距离更大,而上述查询并没有给我返回这些数据。例如:'it is something possible to do toggle between the. . . visibility' 不会被返回。我知道我可以通过手动输入to_tsquery 来使用<2>(例如)距离运算符返回它。
但我想了解,如何在我的 sql 语句本身中执行此操作,以便我首先获得距离为 1 的结果,然后是 2 等等(可能直到 6-7)。最后将搜索词的实际计数附加到结果中,如下查询:
select count(id) from so_rum
where body @@ to_tsquery('english','string & string . . . ')
是否有可能在单个查询中实现良好的性能?
【问题讨论】:
-
听起来你想要全文搜索:postgresql.org/docs/current/textsearch.html。
-
是的。上面的查询实际上是在做全文搜索。
标签: sql postgresql search indexing full-text-search