使用postgreSQL+bamboo搭建比lucene方便N倍的全文搜索

全部用到到包有:web

cmake-2.6.4.tar.gz (編nlpbamboo用)sql

CRF++-0.53.tar.gz(同上)vim

nlpbamboo-1.1.1.tar.bz2(分詞用)bash

postgreSQL-8.3.3.tar.gz(索引用)網絡

安裝pgsql函數

tar -zxvf postgreSQL-8.3.3.tar.gzpost

cd postgre-8.3.3學習

./configure –prefix=/opt/pgsqlui

make
make install搜索引擎

useradd postgre

chown -R postgre.postgre /opt/pgsql
su – postgre
vi ~postgre/.bash_profile
添加
export PATH
PGLIB=/opt/pgsql/lib
PGDATA=/data/PGSearch
PATH=$PATH:/opt/pgsql/bin
MANPATH=$MANPATH:/opt/pgsql/man
export PGLIB PGDATA PATH MANPATH

# mkdir -p /data/PGSearch

# chown -R postgre.postgre /data/PGSearch

# chown -R postgre.postgre /opt/pgsql

#sudo -u postgre /opt/pgsql/bin/initdb –locale=zh_CN.UTF-8 –encoding=utf8 -D /data/PGSearch

#sudo -u postgre /opt/pgsql/bin/postmaster -i -D /data/PGSearch & //容許網絡訪問

#sudo -u postgre /opt/pgsql/bin/createdb kxgroup
# vim /data/PGSearch/pg_hba.conf 以下增長可訪問的機器:

host all all 10.2.19.178 255.255.255.0 trust

#su – postgre

$pg_ctl stop

$postmaster -i -D /data/PGSearch &
安裝中文分詞(Cmake CRF++ bamboo)
Cmake是爲了編譯bamboo,CRF++是bamboo依賴的。

tar -zxvf cmake-2.6.4.tar.gz

cd cmake-2.6.4
./configure
gmake
make install

tar -zxvf CRF++-0.53.tar.gz
cd CRF++-0.53
./configure
make
make install

tar -jxvf nlpbamboo-1.1.1.tar.bz2
cd nlpbamboo-1.1.1
mkdir build
cd build/
cmake .. -DCMAKE_BUILD_TYPE=release
make all
make install

cp index.tar.bz2 /opt/bamboo/
cd /opt/bamboo/
tar -jxvf index.tar.bz2

#/opt/bamboo/bin/bamboo

若是出現:

ERROR: libcrfpp.so.0: cannot open shared object file: No such file or directory

就執行:

ln -s /usr/local/lib/libcrfpp.so.* /usr/lib/
ldconfig

增長上中文分詞擴展到pgsql

#vim /root/.bash_profile 也增長:

PGLIB=/opt/pgsql/lib
PGDATA=/data/PGSearch
PATH=$PATH:/opt/pgsql/bin
MANPATH=$MANPATH:/opt/pgsql/man
export PGLIB PGDATA PATH MANPATH

#source ~/.bash_profile

cd /opt/bamboo/exts/postgres/chinese_parser/
make
make install

su – postgre
cd /opt/pgsql/share/contrib/
touch /opt/pgsql/share/tsearch_data/chinese_utf8.stop
psql kxgroup
\i chinese_parser.sql 導入

再執行下面的sql,已經能夠將一段話分詞了:

SELECT to_tsvector(’chinesecfg’, ‘結果在命令行下執行bamboo才知道’);

先到這裏,下一部分講述對TEXT字段進行索引和查詢,完整構造一整個搜索引擎。

1、基礎篇

本回從一條sql開始:

select * from dbname where field_name @@ ‘aa|bb’ order by rank(field_name, ‘aa|bb’);

從這個sql字面意思講解:從 dbname這個表中查field_name匹配aa或者是bb的詞,而且按照他們的匹配的RANK排序。

基本上明白上面這段話後,來學習四個概念:tsvector、 tsquery、 @@ 、gin。

1.tsvector:

在postgreSQL 8.3自帶支持全文檢索功能,在以前的版本中須要安裝配置tsearch2才能使用。它提供兩個數據類型(tsvector,tsquery),而且經過 動態檢索天然語言文檔的集合,定位到最匹配的查詢結果,tsvector正是其中之一。

一個tsvector的值是惟一分詞的分類列表,把一話一句詞格式化爲不一樣的詞條,在進行分詞處理的時候,tsvector會自動去掉分詞中重複的詞條,按照必定的順序裝入。例如

SELECT ‘a fat cat sat on a mat and ate a fat rat’::tsvector;
tsvector
—————————————————-
‘a’ ‘on’ ‘and’ ‘ate’ ‘cat’ ‘fat’ ‘mat’ ‘rat’ ’sat’

經過tsvector把一個字符串按照空格進行分詞,這能夠把分詞後的詞按照出現的次數排成一排(還會按詞長度)。

對於英文和中文的全文檢索咱們還要看下面這條sql:

SELECT to_tsvector(’english’, ‘The Fat Rats’);
to_tsvector
—————–
‘fat’:2 ‘rat’:3

to_tsvector函數來是tsvector規格化的,在其中可指定所使用的分詞。

2.tsquery:

顧名思義,tsquery,表示的應該是查詢相關的.tsquery是存儲用於檢索的詞條.而且能夠聯合使用boolean 操做符來鏈接, & (AND), | (OR), and ! (NOT). 使用括號(),能夠強制分爲一組.

同時,tsquery 在作搜索的時候,也可使用權重,而且每一個詞均可以使用一個或者多個權重標記,這樣在檢索的時候,會匹配相同權重的信息.跟上面的tsvector相同,tsquery也有一個to_tsquery函數.

3.@@:

在postgresql中全文檢索匹配操做使用@@ 操做符,若是一個
tsvector(document) 匹配到 tsquery(query)則返回true.

看一個簡單的例子:

SELECT ‘a fat cat sat on a mat and ate a fat rat’::tsvector @@ ‘cat & rat’::tsquery;
?column?
———-
t
咱們在處理索引的時候仍是要使用他們的函數以下:
SELECT to_tsvector(’fat cats ate fat rats’) @@ to_tsquery(’fat & rat’);
?column?
———-
t
而且操做符 @@ 可使用text做爲tsvector和tsquery.以下操做符可使使用的方法

tsvector @@ tsquery
tsquery  @@ tsvector
text @@ tsquery
text @@ text
上面的前兩種咱們已經使用過了,可是後兩種,
text @@ tsquery 等同於 to_tsvector(x) @@ y.
text @@ text 等同於 to_tsvector(x) @@ plainto_tsquery(y).(~)plainto_tsquery在後面講。。。

4.gin:

gin是一種索引的名稱,全文索引用的。

咱們能夠經過建立gin索引來加速檢索速度.例如

CREATE INDEX pgweb_idx ON pgweb USING gin(to_tsvector(’english’, body));

建立索引能夠有多種方式.索引的建立甚至能夠鏈接兩個列:
CREATE INDEX pgweb_idx ON pgweb USING gin(to_tsvector(’english’, title || body));

2、提升篇

基礎知識學完了,應該上陣了,爲了實現全文檢索,咱們須要把一個文檔建立一個tsvector 格式,而且經過tsquery實現用戶的查詢,在查詢中咱們返回一個按照重要性排序的查詢結果。

先看一個to_tsquery的sql:

SELECT to_tsquery(’english’, ‘Fat | Rats:AB’);
to_tsquery
——————
‘fat’ | ‘rat’:AB

能夠看出,to_tsquery函數在處理查詢文本的時候,查詢文本的單個詞之間要使用邏輯操做符(& (AND), | (OR) and ! (NOT))鏈接(或者使用括號)。

若是執行下面這條sql就會出錯:

SELECT to_tsquery(’english’, ‘Fat  Rats’);

plainto_tsquery函數卻能夠提供一個標準的tsquery,如上面的例子,plainto_tsquery會自動加上邏輯&操做符。
SELECT plainto_tsquery(’english’, ‘Fat  Rats’);

plainto_tsquery
—————–
‘fat’ & ‘rat’
可是plainto_tsquery函數不可以識別邏輯操做符和權重標記。
SELECT plainto_tsquery(’english’, ‘The Fat & Rats:C’);
plainto_tsquery
———————
‘fat’ & ‘rat’ & ‘c’

3、終結篇

看完上面的一堆後,千言萬語匯成一句話,本文主要講的是一條sql,在加了第一部分裏所講述的擴展後,使用下面的sql,從一個字段中搜一句話,還要排序出來:

select * from tabname where to_tsvector(’chinesecfg’,textname) @@ plainto_tsquery(’搜點啥’) order by ts_rank(to_tsvector(’chinesecfg’,textname),plainto_tsquery(’搜點啥’)) limit 10;

以前的create table create index就不寫了。授人以漁纔是關鍵。

相關文章
相關標籤/搜索