在我投资使用 solr 或 lucene 或 sphinx 之前,我想尝试使用 postgresql 全文搜索在我的系统上实现搜索功能。
我的表格中有一个全国性的企业列表,我想搜索。我创建了一个结合了企业名称和城市的 ts 向量,这样我就可以进行像“outback atlanta”这样的搜索。
我还通过使用搜索的通配符功能来实现自动完成功能,方法是将“:”附加到搜索模式并在关键字之间插入“&”,因此搜索模式“outback atl”变成了“outback & atl: " 在使用 to_tsquery() 转换为查询之前。
这是我目前遇到的问题。如果搜索模式输入为“ou”,则返回许多“Outback Steakhouse”记录。如果搜索模式输入为“out”,则不返回任何结果。如果搜索模式输入为“outb”,则返回许多“Outback Steakhouse”记录。
做了一点调试,我想出了这个:
select ts_rank(to_tsvector('Outback Steakhouse'),to_tsquery('ou:*')) as "ou",
ts_rank(to_tsvector('Outback Steakhouse'),to_tsquery('out:*')) as "out",
ts_rank(to_tsvector('Outback Steakhouse'),to_tsquery('outb:*')) as "outb"
结果是:
ou out outb
0.0607927 0 0.0607927
我究竟做错了什么?
这是 pg 全文搜索的限制吗?
我可以用我的字典或配置做些什么来解决这个异常吗?
更新:我认为“out”可能是一个停用词。
当我运行这个调试查询时,我没有得到任何“out”的词位
SELECT * FROM ts_debug('english','out back outback');
alias description token dictionaries dictionary lexemes
asciiword Word all ASCII out {english_stem} english_stem {}
blank Space symbols {}
asciiword Word all ASCII back {english_stem} english_stem {back}
blank Space symbols {}
asciiword Word all ASCII outback {english_stem} english_stem {outback}
所以现在我问我如何修改停用词列表来删除一个词?
更新:这是我目前使用的查询:
select id,name,address,city,state,likes
from view_business_favorite_count
where textsearchable_index_col @@ to_tsquery('simple',$1)
ORDER BY ts_rank(textsearchable_index_col, to_tsquery('simple',$1)) DESC
当我执行查询时(我使用的是 Strongloop Loopback + Express + Node),我将模式传入以替换 $1 参数。模式(如上所述)看起来像“keyword:”或“keyword1 & keyword2 & ... & keywordN: ”
谢谢