全文搜索按PostgreSQL中的完全匹配排序

时间:2017-07-17 07:17:29

标签: postgresql full-text-search exact-match

我有一个像

这样的查询
select * from mytable where posttext @@ to_tsquery('Intelence');

我的文字有'intelence'字样和'intel'字样。

我想返回结果,其中关键字'Intelence'的完全匹配将在'intel'结果之前订购?

1 个答案:

答案 0 :(得分:1)

也许不是最优化和最佳的方式,但在order by中使用正则表达式匹配应该做你想要的

with mytable( posttext ) as(
select 'a Intelence' union all
select 'intel someIntelence' union all
select 'a intel' union all
select 'a Intelence b' 
)

select * 
from mytable 
where posttext @@ to_tsquery('Intelence')  
order by posttext ~* '(^|\s)intelence(\s|$)' desc
相关问题