2017-07-17 56 views
0

有序全文搜索我有一個像通過精確匹配PostgreSQL中

select * from mytable where posttext @@ to_tsquery('Intelence'); 

我有文本與「intelence」字與「英特爾」的查詢。

我想返回關鍵字'Intelence'的精確匹配將在'intel'結果前排序的結果嗎?

回答

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