Postgresql增加中文全文搜尋支援

2021-08-21 23:35:55 字數 1978 閱讀 6528

zhparser是乙個支援中文全文檢索的 extension,基於****** chinese word segmentation(scws) 上開發的

解壓安裝:

tar xvf scws-1.2.2.tar.bz2 

cd scws-1.2.2

./configure 

make install

準備:設定環境變數

pg_home=/opt/postgresql/9.6

pg_bin=/opt/postgresql/9.6/bin

path=$path:$pg_bin

解壓並進入解壓目錄,執行命令:

scws_home=/usr/local make && make install

檢查安裝結果:

進入目錄/opt/postgresql/9.6/share/postgresql/extension,檢視檔案列表

1、建立extension:create extension zhparser;

2、create text search configuration testzhcfg (parser = zhparser);

4、select to_tsvector('testzhcfg','南京市長江大橋');

結果:

create index idx_gin_tbl_store_name on poi using gin(to_tsvector('testzhcfg',name_zh));

執行查詢:

select * from poi where to_tsvector('testzhcfg',name_zh) @@ to_tsquery('testzhcfg','中華人民共和國')

查詢結果:

注意點:不要隨意加limit,不加limit的執行計畫為:

加上limit的執行計畫為:

也就是沒有走索引

除了zhparser自帶的詞典,使用者可以增加自定義詞典,自定義詞典的優先順序高於自帶的詞典。自定義詞典的檔案必須放在share/postgresql/tsearch_data目錄中,zhparser根據副檔名確定詞典的格式型別,.txt副檔名表示詞典是文字格式,.xdb副檔名表示這個詞典是xdb格式,多個檔案使用逗號分隔,詞典的分詞優先順序由低到高,如:

zhparser.extra_dicts = 'dict_extra.txt,mydict.xdb'

txt 詞庫的寫法 (txt詞庫目前已相容 cli/scws_gen_dict 所用的文字詞庫) **

每行一條記錄,以 # 或 分號開頭的相當於注釋,忽略跳過

每行由4個字段組成,依次為「詞語"(由中文字或3個以下的字母合成), "tf", "idf", "詞性",字段使用空格或製表符分開,數量不限,可自行對齊以美化

除「詞語」外,其它欄位可忽略不寫。若忽略,tf和idf預設值為 1.0 而 詞性為 "@"

由於 txt 庫動態載入(內部監測檔案修改時間自動轉換成 xdb 存於系統臨時目錄),故建議txt詞庫不要過大

刪除詞做法,請將詞性設為「!「,則表示該詞設為無效,即使在其它核心庫中存在該詞也視為無效

更改之後,需要重新啟動資料庫: bin/pg_ctl -d /data/lilei/postgresql/data/ restart

R連線postgresql中文亂碼

library rpostgresql pgdriver dbdriver postgresql key dbconnect pgdriver,host 192.168.1.121 port 5432 dbname test project user postgres password 123456...

九,給spyder增加中文注釋

sudo cp usr lib x86 64 linux gnu qt5 plugins platforminputcontexts libfcitxplatforminputcontextplugin.so anaconda3 envs tf gpu py3 plugins platforminp...

discuz增加中文驗證碼

方法 1.從windows 系統下獲得字型檔案 c盤下的windows fonts目錄 如 simhei.ttf simsun.ttc 2.linux centos6.5 usr share fonts 下 mkdir chinese 目錄 3.賦予操作許可權 chmod r 755 usr sha...