postgresql - 包含连接字段的create和index

时间:2014-09-30 13:23:57

标签: postgresql

我正在尝试创建一个索引,其中包含一个字段,其中包含postgresql中其他几个字段的连接结果。

例如我有一个地址:

BLACKPOOL FYLDE & WYRE SOCIETY FOR THE BLIND, PRINCESS ALEXANDRA HOME FOR THE BLIND, BOSWORTH PLACE, BLACKPOOL, FY4 1SH

以上由5个字段组成,在示例中由逗号分配。我想创建一个包含5个字段数据的索引字段。

我希望我的索引包含3个字段:uprn,AddressText,wkb_geometry AddressText将包含上面的示例,其中5个字段连接到其中,其他2个字段只是常规字段。

我知道如何在查询“||”中连接并且工作正常但无法找到解释如何执行上述操作的任何地方。我所描述的方式是地址数据提供者(Ordnance Survey)如何解释它应该完成,但它们没有提供示例代码来创建索引。

我可以使用上面的内容创建一个新表,但是在更新地址数据时必须重新创建它。

我将用于构建地址的实际查询是这样的,因为您可以看到它比仅仅5个字段更复杂:

SELECT uprn,
(
CASE WHEN organisation_name IS NOT NULL THEN organisation_name || ', ' ELSE '' END
|| CASE WHEN sao_text IS NOT NULL THEN sao_text || ', ' ELSE '' END
|| CASE WHEN sao_start_number IS NOT NULL THEN sao_start_number::text ELSE '' END
|| CASE WHEN sao_start_suffix IS NOT NULL THEN  sao_start_suffix ELSE '' END
|| CASE WHEN sao_end_number IS NOT NULL THEN '-' || sao_end_number ELSE '' END
|| CASE WHEN sao_end_suffix IS NOT NULL THEN sao_end_suffix ELSE '' END

|| CASE WHEN sao_start_number IS NOT NULL THEN ' ' ELSE '' END

|| CASE WHEN pao_text IS NOT NULL THEN pao_text || ', ' ELSE '' END
|| CASE WHEN pao_start_number IS NOT NULL THEN pao_start_number::text ELSE '' END
|| CASE WHEN pao_start_suffix IS NOT NULL THEN  pao_start_suffix ELSE '' END
|| CASE WHEN pao_end_number IS NOT NULL THEN '-' || pao_end_number ELSE '' END
|| CASE WHEN pao_end_suffix IS NOT NULL THEN pao_end_suffix ELSE '' END

|| CASE WHEN pao_start_number IS NOT NULL THEN ' ' ELSE '' END

|| CASE WHEN street_description IS NOT NULL THEN street_description || ', ' ELSE '' END
|| CASE WHEN locality IS NOT NULL THEN locality || ', ' ELSE '' END
|| CASE WHEN town_name IS NOT NULL THEN town_name ELSE '' END
|| CASE WHEN administrative_area <> town_name THEN ', ' || administrative_area ELSE '' END
|| CASE WHEN postcode_locator IS NOT NULL THEN ', ' || postcode_locator ELSE '' END
) AS AddressText, wkb_geometry
FROM addressbase.address
LIMIT 1000

不幸的是我不能这样做:

CREATE INDEX AddressSearchIndex ON addressbase.address (uprn,
(
CASE WHEN organisation_name IS NOT NULL THEN organisation_name || ', ' ELSE '' END
|| CASE WHEN sao_text IS NOT NULL THEN sao_text || ', ' ELSE '' END
|| CASE WHEN sao_start_number IS NOT NULL THEN sao_start_number::text ELSE '' END
|| CASE WHEN sao_start_suffix IS NOT NULL THEN  sao_start_suffix ELSE '' END
|| CASE WHEN sao_end_number IS NOT NULL THEN '-' || sao_end_number ELSE '' END
|| CASE WHEN sao_end_suffix IS NOT NULL THEN sao_end_suffix ELSE '' END

|| CASE WHEN sao_start_number IS NOT NULL THEN ' ' ELSE '' END

|| CASE WHEN pao_text IS NOT NULL THEN pao_text || ', ' ELSE '' END
|| CASE WHEN pao_start_number IS NOT NULL THEN pao_start_number::text ELSE '' END
|| CASE WHEN pao_start_suffix IS NOT NULL THEN  pao_start_suffix ELSE '' END
|| CASE WHEN pao_end_number IS NOT NULL THEN '-' || pao_end_number ELSE '' END
|| CASE WHEN pao_end_suffix IS NOT NULL THEN pao_end_suffix ELSE '' END

|| CASE WHEN pao_start_number IS NOT NULL THEN ' ' ELSE '' END

|| CASE WHEN street_description IS NOT NULL THEN street_description || ', ' ELSE '' END
|| CASE WHEN locality IS NOT NULL THEN locality || ', ' ELSE '' END
|| CASE WHEN town_name IS NOT NULL THEN town_name ELSE '' END
|| CASE WHEN administrative_area <> town_name THEN ', ' || administrative_area ELSE '' END
|| CASE WHEN postcode_locator IS NOT NULL THEN ', ' || postcode_locator ELSE '' END
) AS AddressText, wkb_geometry);

这个例子允许我像这样进行搜索:

SELECT UPRN, AddressText, wkb_geometry FROM AddressSearchIndex
WHERE
AddressText LIKE ‘%4%’ AND
AddressText LIKE ‘%HIGH%’ AND
AddressText LIKE ‘%STREET%’ AND
AddressText LIKE ‘%WESTVILLE%’ AND
AddressText LIKE ‘%WV17%’;

1 个答案:

答案 0 :(得分:1)

使用您可以在Postgres full text search或Postgres官方手册上找到的信息:http://www.postgresql.org/docs/current/static/textsearch.html

我认为你需要:

1)表达式上的GIN或GIST索引(请参阅全文搜索示例)。

2)在基表+表达式上创建一个简单的VIEW(而不是MVIEW)。

3)对您的VIEW使用全文搜索查询。