Postgresql
‘錯誤:在創建索引期間文本搜尋字典“不重音”不存在’?
我在 Mac OS X Yosemite 上執行 PostgreSQL 9.3。
我嘗試創建一個不重音的小寫三元組索引。為了實現它,我這樣做了:
mydb=# CREATE EXTENSION pg_trgm SCHEMA public VERSION "1.1"; CREATE EXTENSION unaccent SCHEMA public; ALTER FUNCTION unaccent(text) IMMUTABLE; CREATE EXTENSION CREATE EXTENSION ALTER FUNCTION
然後我嘗試創建索引:
mydb=# CREATE INDEX author_label_hun_gin_trgm ON address USING gin (public.unaccent(lower(label_hun)) gin_trgm_ops); ERROR: text search dictionary "unaccent" does not exist
…並得到這個錯誤。如果我嘗試列出可用的文本搜尋字典,
unaccent
字典似乎就在那裡:mydb=# \dFd List of text search dictionaries Schema | Name | Description ------------+-----------------+----------------------------------------------------------- pg_catalog | danish_stem | snowball stemmer for danish language pg_catalog | dutch_stem | snowball stemmer for dutch language pg_catalog | english_stem | snowball stemmer for english language pg_catalog | finnish_stem | snowball stemmer for finnish language pg_catalog | french_stem | snowball stemmer for french language pg_catalog | german_stem | snowball stemmer for german language pg_catalog | hungarian_stem | snowball stemmer for hungarian language pg_catalog | italian_stem | snowball stemmer for italian language pg_catalog | norwegian_stem | snowball stemmer for norwegian language pg_catalog | portuguese_stem | snowball stemmer for portuguese language pg_catalog | romanian_stem | snowball stemmer for romanian language pg_catalog | russian_stem | snowball stemmer for russian language pg_catalog | simple | simple dictionary: just lower case and check for stopword pg_catalog | spanish_stem | snowball stemmer for spanish language pg_catalog | swedish_stem | snowball stemmer for swedish language pg_catalog | turkish_stem | snowball stemmer for turkish language public | unaccent |
知道這裡可能有什麼問題嗎?
我推薦這種替代方法來獲取
IMMUTABLE
unaccent()
功能:CREATE OR REPLACE FUNCTION f_unaccent(text) RETURNS text AS $func$ SELECT public.unaccent('public.unaccent', $1) -- schema-qualify function and dictionary $func$ LANGUAGE sql IMMUTABLE;
將該函式用於表達式索引和所有查詢。詳細解釋:
並且不要改變原來的功能:
ALTER FUNCTION unaccent(text) IMMUTABLE;
search_path
..這public.unaccent()
也解決public
了search_path
.然後,您的索引:
CREATE INDEX author_label_hun_gin_trgm ON address USING gin (f_unaccent(label_hun) gin_trgm_ops);
我沒有包括
lower()
. 您可以,但 trigram 索引支持開箱即用的不區分大小寫的模式:故障排除
如果您在同一會話中以相同的方式執行所有這些,
search_path
並且一個呼叫在下一個報告時有效function ... does not exist
,那麼您的數據庫中的某些內容已嚴重損壞。首先確保您使用有效
search_path
設置進行測試:SHOW search_path
此相關答案中的詳細資訊
如果您診斷出損壞,請切換到警報模式並採取所有必要步驟以避免數據失去(例如在執行其他任何操作之前繪製備份)。
系統表的索引損壞
pg_proc
可能是原因。要從shell重新索引所有系統表:reindexdb [connection-option...] --system mydb