加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

postgresql – postgres 9.1全文搜索返回没有结果

发布时间:2020-12-13 15:50:19 所属栏目:百科 来源:网络整理
导读:我在网上搜索了很多天似乎互联网从来没有听说过我的问题: 我有一个邮政地址数据库表,其中包含大约37M的英国记录,其中包含地理空间索引和衍生的全文索引,如下所示: create index on gb_locations using gin(to_tsvector('english',"Postcode" || ' ' || "Po
我在网上搜索了很多天似乎互联网从来没有听说过我的问题:

我有一个邮政地址数据库表,其中包含大约37M的英国记录,其中包含地理空间索引和衍生的全文索引,如下所示:

create index on gb_locations using gin(to_tsvector('english',"Postcode" || ' ' || "Postcode_outcode" || ' ' || "Road" || ' ' || "Neighbourhood" || ' ' || "Admin2" || ' ' || "Admin3");)

我的全文搜索形式如下:

SELECT * FROM gb_locations
WHERE
    to_tsvector('english',"Postcode" || ' ' || "Postcode_outcode" || ' ' || "Road" || ' ' || "Neighbourhood" || ' ' || "Admin2" || ' ' || "Admin3") @@ plainto_tsquery('english','greenham road rg14')

该查询适用于大多数英国地址,特别是在伦敦地区,但对于更远的地方,查询不会返回任何结果.

我已经验证表中存在记录,因为我可以使用地理空间搜索找到它,但对于全文搜索,似乎数据库不知道它.

这是解释:

Bitmap Heap Scan on gb_locations  (cost=52.04..56.10 rows=1 width=521)
  Recheck Cond: (to_tsvector('english'::regconfig,((((((((((("Postcode")::text || ' '::text) || ("Postcode_outcode")::text) || ' '::text) || "Road") || ' '::text) || ("Neighbourhood")::text) || ' '::text) || ("Admin2")::text) || ' '::text) || ("Admin3")::text)) @@ '''greenham'' & ''road'' & ''rg14'''::tsquery)
  ->  Bitmap Index Scan on text_search_index  (cost=0.00..52.04 rows=1 width=0)
        Index Cond: (to_tsvector('english'::regconfig,((((((((((("Postcode")::text || ' '::text) || ("Postcode_outcode")::text) || ' '::text) || "Road") || ' '::text) || ("Neighbourhood")::text) || ' '::text) || ("Admin2")::text) || ' '::text) || ("Admin3")::text)) @@ '''greenham'' & ''road'' & ''rg14'''::tsquery)

任何poiners都会非常感激.

解决方法

如果某些字段可以为NULL,则需要在获取要搜索的字符串的全局串联中对它们应用coalesce(field,”).

否则它似乎与评论中给出的示例值一起使用:

select to_tsvector('english','RG147SW RG14 Greenham Road Newbury  West Berkshire')
  @@ plainto_tsquery('english','greenham road rg14');

 ?column? 
----------
 t
(1 row)

但是这个不匹配(结果为NULL),当Admin2为NULL时更是如此,或者更常见的是将任何其他字段传递给||操作符.

select to_tsvector('english','RG147SW RG14 Greenham Road ' || NULL || ' Newbury  West Berkshire')
      @@ plainto_tsquery('english','greenham road rg14');

?column? 
----------

(1 row)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读