coreseek安裝過程

來源:互聯網
上載者:User

標籤:本地   get   速度   --   意思   sql   select   run   ram   

一、sphinx 全文檢索索引
通過sphinx檢索到id,然後到mysql裡面拿到記錄

什麼是勸我呢檢索?
結構化資料: 具有固定格式或者長度的資料
非結構化資料: 標題 內容 等不定長的資料
非機構化資料還叫做"全文資料"

檢索分為兩個過程:
索引建立 和 搜尋索引

檢索結構
php -> sphinx -> mysql
非結構化資料又叫全文資料,非固定長度欄位例如文章標題搜尋這類適用sphinx

全文資料搜尋:
順序掃描 : 如like尋找
索引掃描 : 把非結構化資料中的內容提取出來一部分重新組織,這部分資料就叫做索引

全文檢索索引分為兩個過程
1.建立索引
2.搜尋索引

建立索引:
sphinx通過語言處理組件會把搜尋語句中的標點符號,語氣助詞給過濾掉,然後處理完畢後會
分析出一些詞語,傳給索引組件,然後排序去除重複詞語.

搜尋索引:
搜尋索引鍵 提交 -> sphinx 分析出文檔ID -> mysql通過ID查詢資料並返回


中文分詞需要coreseek
coreseek 支援中文的全文檢索索引引擎

sphinx的安裝
下載源碼包 進行源碼安裝
./configure --prefix=/usr/local/sphinx --with-mysql=/usr/local/mysql
make && make install

sphinx 中重要的三個命令(bin目錄下)
Indexer 建立索引命令
Searchd 啟動進程命令
Search 命令列搜尋命令

配置sphinx
cd /usr/local/sphinx/etc
cp sphinx.conf.dist sphinx.conf
vi sphinx.conf

設定檔
名為main的主要資料源:source main{}
繼承名為main的增量資料來源:source delta:main{}
名為main的主索引:index main{}
繼承名為main的增量索引:index delta:main{}
分布式索引:index dist1{}
索引器:indexer{}
服務進程:searchd{}

修改設定檔中主要資料源的mysql配置,測試環境的資料來源名為main,假設mysql中test有張表叫post
source main
{
# data source type. mandatory, no default value
# known types are mysql, pgsql, mssql, xmlpipe, xmlpipe2, odbc
type = mysql

#####################################################################
## SQL settings (for ‘mysql‘ and ‘pgsql‘ types)
#####################################################################

# some straightforward parameters for SQL source types
sql_host = localhost
sql_user = root
sql_pass = admin
sql_db = test
sql_port = 3306 # optional, default is 3306


sql_sock = /tmp/mysqld.sock #如果是linux下需要開啟,指定sock檔案

sql_query_pre = SET NAMES utf8
sql_query_pre = SET SESSION query_cache_type=OFF #關閉緩衝


# main document fetch query
# mandatory, integer document ID field MUST be the first selected column
sql_query = \ #擷取資料的sql語句
SELECT id,title,content FROM post


#sql_attr_uint = group_id #對排序欄位進行注釋


#sql_attr_timestamp = date_added #對排序欄位進行注釋


sql_query_info = SELECT * FROM post WHERE id=$id


}
注釋掉所有增量資料來源,修改主索引

index main
{
source = main

# index files path and file name, without extension
# mandatory, path must be writable, extensions will be auto-appended
path = /usr/local/sphinx/var/data/main

docinfo = extern
mlock = 0
morphology = none
# default is 1 (index everything)
min_word_len = 1

# charset encoding type
# optional, default is ‘sbcs‘
# known types are ‘sbcs‘ (Single Byte CharSet) and ‘utf-8‘
charset_type = utf-8
# ‘utf-8‘ default value is
# charset_table = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F

}

注釋掉所有的增量索引和分布式索引

 

配置完成後,建立索引
命令 indexer
-c 指定設定檔
--all 對所有索引重新編製索引
--rotate 用於輪換索引,主要是在不停止服務的時候,增加索引
--merge 合并索引
/usr/local/sphinx/sbin/indexer -c
/usr/local/sphinx/etc/sphinx --all
如果報錯./indexer: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file: No such file or directory
解決方式:cp /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib/libmysqlclient.so.18

測試
建立索引
[[email protected] bin]# ./indexer --all
Sphinx 0.9.9-release (r2117)
Copyright (c) 2001-2009, Andrew Aksyonoff

using config file ‘/usr/local/sphinx/etc/sphinx.conf‘...
indexing index ‘main‘...
collected 2 docs, 0.0 MB
sorted 0.0 Mhits, 100.0% done
total 2 docs, 47 bytes
total 0.033 sec, 1414 bytes/sec, 60.21 docs/sec
total 1 reads, 0.000 sec, 0.0 kb/call avg, 0.0 msec/call avg
total 5 writes, 0.000 sec, 0.0 kb/call avg, 0.0 msec/call avg

搜尋
[[email protected] bin]# ./search linux1
Sphinx 0.9.9-release (r2117)
Copyright (c) 2001-2009, Andrew Aksyonoff

using config file ‘/usr/local/sphinx/etc/sphinx.conf‘...
index ‘main‘: query ‘linux1 ‘: returned 1 matches of 1 total in 0.000 sec

displaying matches:
1. document=1, weight=1
id=1
title=linux1
content=salflsdkjsdglds

words:
1. ‘linux1‘: 1 documents, 1 hits

sphinx相當於把資料庫資料索引靜態化到本地 來加快查詢速度 避免消耗資料庫,給資料庫給降壓,當資料更新的時候,需要重新indexer --all 或者用 indexer main重建所有索引 才會搜出新的資料

 

安裝CORESEEK
coreseek裡有2個檔案夾 一個是mmseg中文分詞包 還有一個是csft(其實就是sphinx)包 都要安裝

首先安裝mmseg中文分詞
./configure --prefix=/usr/local/mmseg
編譯時間可能會報錯config.status: error: cannot find input file: src/Makefile.in
通過automake來解決
首先檢查是否安裝了libtool如果沒有
yum -y install libtool
automake
如果automake報錯 原因可能是下列
Libtool library used but `LIBTOOL‘ is undefined
The usual way to define `LIBTOOL‘ is to add `AC_PROG_LIBTOOL‘
to `configure.ac‘ and run `aclocal‘ and `autoconf‘ again.
If `AC_PROG_LIBTOOL‘ is in `configure.ac‘, make sure
its definition is in aclocal‘s search path.

原因分析
aclocal是個掃描程式, 負責掃描configure.ac中所有的宏定義並展開,
上面產生的原因就是找不到LIBTOOL宏的定義之處造成的.原因就是aclocal與libtool沒有安裝在一個相同目錄下面aclocal是去預設 安裝目錄 /usr/share/aclocal下面搜尋所有的.m4檔案找所定義的宏,但是由於安裝了多個aclocal,可能aclocal目錄不存在,實際目錄為/usr/share/aclocal1.10等,
這就造成了aclocal找不到m4檔案的情況, 解決辦法就是將檔案夾aclocal1.10重新命名為aclocal.或者顯示指定路徑 aclocal -I /usr/share/aclocal1.10 -I /usr/share/libtool/m4 --install 或者把/usr/share/libtool/m4下面的檔案都copy至/usr/share/aclocal1.10中.
本人的機器上是/usr/share/下有2個檔案夾 一個是aclocal,一個是aclocal-1.11,首先將aclocal的所有檔案拷貝到aclocal-1.11下
cp -R /user/share/aclocal/* /usr/share/aclocal-1.11/

然後重新運行aclocal和autoconf
aclocal && autoconf
最後再次運行automake
automake

然後繼續mmseg的安裝
./configure --prefix=/usr/local/mmseg
make && make install

如果make 報錯
There is an easy fix when you get such messages as "X--tag=CXX: command not found".
Just type:
export echo=echo
And try again.

所以輸入
export echo=echo
然後再次運行安裝
make && make install


安裝csft
./configure --prefix=/usr/local/coreseek --with-mysql=/usr/local/mysql --with-mmseg=/usr/local/mmseg --with-mmseg-includes=/usr/local/mmseg/include/mmseg/ --with-mmseg-libs=/usr/local/mmseg/lib/
make && make install
安裝完畢後 注意 coreseek 中的設定檔也是csft.conf 而不是 sphinx.conf
cd /usr/local/coreseek/etc
cp sphinx.conf.dist csft.conf
vim csft.conf

有些配置改動如下其他配置內容如上文的sphinx.conf
在索引源中注釋掉txt
index main{
#stopwords = G:\data\stopwords.txt
#wordforms =G:\data\wordforms.txt
#exceptions =/data/exceptions.txt
#charset_type = sbcs
#添加下面2行 意思是把中文分詞加入到設定檔中
char_type = zh_cn.utf-8
charset_dictpath =/usr/local/mmseg/etc/ #你安裝mmseg的目錄
}

儲存配置
建立索引
cd /usr/local/coreseek/bin
./indexer --all
./search 中文詞綴


如何用php去使用sphinx
Sphinx整合到php程式中有兩種方式
1.Sphinx php 模組(這次我們選擇使用的方式)
2.Sphinx api 類(位於coreseek源碼包裡的csft裡的api檔案夾裡有一個sphinxapi.php,使用的時候包含這個php檔案即可)

我們要使用sphinx需要做以下幾件事:
1.首先要有資料
2.建立sphinx設定檔
3.產生索引
4,啟動searchd 服務進程,並開啟連接埠9312
5.用php客戶程式去連結sphinx服務


/usr/local/coreseek/bin/searchd
啟動進程命令 searchd
-c 指定設定檔
--stop 停止服務
--pidfile 顯示指定pid檔案
-p 指定連接埠(預設9312)
注意:這裡啟動的服務是searchd

使用php sphinx的模組
下載 sphinx-1.1.0.tgz

tar zvxf sphinx-1.1.0.tgz
cd sphinx-1.1.0
/usr/local/php/bin/phpize #用於產生一個configure的指令碼
進入coreseek源碼包的csft/api/libsphinxclent 目錄下執行configure
./configure
make && make install
進入sphinx-1.1.0.gzt的源碼包目錄下 執行configure用於產生so 的shpinx模組
./configure --with-php-config=/usr/local/webserver/php/bin/php-config --with-sphinx
make && make install
安裝成功後會有提示
Build complete.
Don‘t forget to run ‘make test‘.

Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/ (這個目錄是sphinx.so所在的目錄)

然後我們編輯php的ini檔案 在extension中加入
extension=sphinx.so

然後重啟nginx和php的服務
最後列印一下phpinfo 查看sphinx是否開啟成功
如果成功開啟 就可以使用php手冊裡的sphinx的方法和代碼了

 

coreseek安裝過程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.