![](/img/trans.png)
[英]Postgres - Create index for full text search using 3 + columns with to_tsvector
[英]Postgres Full Text Search using TSVECTOR and special UTF8 characters
我有一個需要可搜索的 instagram 帖子數據庫。 當前實現使用TSVECTOR
列和@@
運算符對其進行搜索:
CREATE TABLE ig_posts (
/* ... */
caption_tsv TSVECTOR
);
帖子插入 SQL:
INSERT INTO ig_posts(caption_tsv)
VALUES (to_tsvector('simple', ?)) /* simple, because post can be in any language */
搜索 SQL:
SELECT *
FROM ig_posts
WHERE ig_posts.caption_tsv @@ to_tsquery('simple', ?)
一切都很好,除了一些帖子可能包含 utf8 編碼的粗體/斜體文本,例如:
'rosegold':26 'sunshine':23 '𝑨𝒍𝒘𝒂𝒚𝒔':1 '𝑯𝒆𝒂𝒓𝒕':4 '𝒊𝒏':2 '𝒎𝒚':3 '𝗗𝗢𝗖𝗘𝗡𝗔𝟭𝟱':21 '𝗗𝗮𝘆':17 '𝗠𝗼𝘁𝗵𝗲𝗿':15
這會導致@@
運算符失敗,因為“Always”和“”被認為是不同的詞。 是否可以在 Postgres 中將粗體/斜體 UTF8 文本恢復正常?
編輯:接受的答案完美,這是我使用的規則文件: https://gist.github.com/dsenkus/9250134b338b9862585963dfdf450e13
And the script that was used to generate it: https://gist.github.com/dsenkus/17ddbd29d53bd6dd2cf4051bd05ad44d (generated results requires some cleanup, since some special letters do not exist in UTF8, and 3rd h letter is not recognized, ℎ should用作值)
您需要unaccent contrib 模塊:
CREATE EXTENSION unaccent;
然后您需要在 PostgreSQL 共享目錄的tsearch_data
子目錄中創建一個文件my_unaccent.rules
,其中包含您需要的字符映射。 這是你必須自己建立的東西。
然后你可以使用它創建一個字典:
CREATE TEXT SEARCH DICTIONARY my_unaccent (TEMPLATE = unaccent, RULES = 'my_unaccent');
然后,您可以基於使用該字典的simple
創建一個新的文本搜索配置:
CREATE TEXT SEARCH CONFIGURATION my_unaccent (COPY = simple);
ALTER TEXT SEARCH CONFIGURATION my_unaccent
ALTER MAPPING FOR asciihword, asciiword, hword, hword_asciipart, hword_part, word
WITH my_unaccent, simple;
這個全文搜索配置應該是你所需要的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.