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

我在网上搜索了很多天似乎互联网从来没有听说过我的问题:

我有一个邮政地址数据库表,其中包含大约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)

相关文章

文章浏览阅读601次。Oracle的数据导入导出是一项基本的技能,...
文章浏览阅读553次。开头还是介绍一下群,如果感兴趣polardb...
文章浏览阅读3.5k次,点赞3次,收藏7次。折腾了两个小时多才...
文章浏览阅读2.7k次。JSON 代表 JavaScript Object Notation...
文章浏览阅读2.9k次,点赞2次,收藏6次。navicat 连接postgr...
文章浏览阅读1.4k次。postgre进阶sql,包含分组排序、JSON解...