2014-09-30 53 views
1

我想創建一個包含來自postgresql中的其他字段的連接結果的字段的索引。postgresql - 創建和索引包含連接的字段

例如我有一個地址:

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

上面是由5個字段,通過在例如逗號sperated的。我想創建一個包含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

順便提一下'CASE WHEN sao_text is not NULL THEN sao_text || ','ELSE''END'給出了和'coalesce(sao_text ||',','')' – 2014-09-30 13:39:19

+0

相同的結果。我不認爲示例搜索是非常棒的。我的互聯網搜索使用TSVECTOR似乎拋出了很多全文搜索的例子,但由於數據提供者的官方示例有不同的說法,我將其拋棄了。這些示例是通用SQL,但並不特定於postgresql。任何幫助不勝感激。 – StevenH 2014-09-30 13:39:25

+0

我認爲你正在尋找'物化視圖'功能,它被添加到PostgreSQL 9.3中https://wiki.postgresql.org/wiki/Materialized_Views http://www.postgresql.org/docs/current/static/sql -creatematerializedview.html – pozs 2014-09-30 13:41:24

回答

1

利用這些信息,你可以找到在Postgres full text search或者Postgres的官方手冊互聯網:http://www.postgresql.org/docs/current/static/textsearch.html

當我看到你將需要:

1)在你的表達的GIN或GIST指數(見全文搜索的例子)。

2)在基表+表達式上創建一個簡單的VIEW(而不是MVIEW)。

3)在VIEW中使用全文搜索查詢。

相關問題