A. mysql怎麼與sphinx建立索引
安裝sphinx
yum install sphinx libsphinxclient-devel
配置sphinx代替mysql全文索引(/etc/sphinx/sphinx.conf)
source account_src
{
type = mysql
sql_host = localhost
sql_user = root
sql_pass =
sql_db = test
sql_port = 3306
sql_query_pre = SET NAMES utf8
sql_query = \
SELECT * \
FROM tb_account
#sql_attr_uint = idx_id
sql_field_string = name
sql_attr_uint = age
}
index account_idx
{
source = account_src
path = /var/lib/sphinx/account_src
docinfo = extern
charset_type = utf-8
}
indexer
{
mem_limit = 32M
}
searchd
{
listen = 127.0.0.1:9312
listen = 9306:mysql41
log = /var/log/sphinx/searchd.log
query_log = /var/log/sphinx/query.log
read_timeout = 5
max_children = 30
pid_file = /var/run/sphinx/searchd.pid
max_matches = 1000
seamless_rotate = 1
preopen_indexes = 1
unlink_old = 1
workers = threads # for RT to work
binlog_path = /var/lib/sphinx
}
創建全量索引
indexer --config /etc/sphinx/sphinx.conf --all
啟動後台搜索服務
searchd --config /etc/sphinx/sphinx.conf
可設置crontab定期構建索引
indexer --config /etc/sphinx/sphinx.conf --rotate account_idx
測試(自帶search工具已不可用,可參照這里),所以需要源碼安裝sphinx的php擴展
B. php windows下怎麼安裝sphinx,安裝後又怎麼使用
1、下載,我這里下的是「Win64 binaries w/MySQL+PgSQL+libstemmer+id64 support」,下載後文件名:sphinx-2.0.6-release-win64-id64-full.zip;
2、將其解壓到D: \ sphinx,並在D:\sphinx下新建目錄data(用來存放索引文件)與log(用來存放日誌文件);
3、將D:\sphinx\sphinx.conf.in復制到D:\sphinx\bin\sphinx.conf.in,並重命名為sphinx.conf;
4、修改 D:\sphinx\bin\sphinx.conf 如下:
4.1、搜索source src1修改{...}中的內容
# 使用的資料庫類型
type = mysql
# 伺服器
sql_host = localhost
# 資料庫登錄名
sql_user = root
# 資料庫登錄密碼
sql_pass = root
# 操作的資料庫名稱
sql_db = test
# 資料庫伺服器埠
sql_port = 3306
# 設置編碼,如果用的是utf-8編碼
sql_query_pre = SET NAMES utf-8
(以上7條前如有#將其刪除)
4.2、搜索index test1修改{...}中的內容
# 放索引的目錄
path = D:/sphinx/data/
# 編碼
charset_type = utf-8
# 指定utf-8編碼表
charset_table = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F
# 簡單分詞,只有0和1,需要搜索中文必須置1
ngram_len = 1
# 需要分詞的字元,搜索中文時必須
ngram_chars = U+3000..U+2FA1F
(以上5條前如有#將其刪除)
5、導入測試數據將D:\sphinx\example.sql中語句執行到test資料庫中,注意:test資料庫創建時需要指定為utf-8格式;
6、打開cmd窗口,進入目錄D:\sphinx\bin;
7、建立索引,執行indexer.exe test1,test1即為sphinx.conf中index test1
Sphinx 2.0.6-id64-release (r3473)
Copyright (c) 2001-2012, Andrew Aksyonoff
Copyright (c) 2008-2012, Sphinx Technologies Inc (http://sphin
using config file './sphinx.conf'...
indexing index 'test1'...
collected 4 docs, 0.0 MB
sorted 0.0 Mhits, 100.0% done
total 4 docs, 222 bytes
total 0.019 sec, 11252 bytes/sec, 202.74 docs/sec
total 2 reads, 0.000 sec, 0.2 kb/call avg, 0.0 msec/call avg
total 9 writes, 0.000 sec, 0.1 kb/call avg, 0.0 msec/call avg
成功...
8、搜索'test',執行search.exe test
Sphinx 2.0.6-id64-release (r3473)
Copyright (c) 2001-2012, Andrew Aksyonoff
Copyright (c) 2008-2012, Sphinx Technologies Inc (http://sphinxsearch.com)
using config file './sphinx.conf'...
index 'test1': query 'test ': returned 3 matches of 3 total in 0.000 sec
displaying matches:
1. document=1, weight=2421, group_id=1, date_added=Tue Feb 26 10:25:31 2013
id=1
group_id=1
group_id2=5
date_added=2013-02-26 10:25:31
title=test one
content=this is my test document number one. also checking search within
phrases.
2. document=2, weight=1442, group_id=3, date_added=Tue Feb 26 10:25:31 2013
id=2
group_id=3
group_id2=6
date_added=2013-02-26 10:25:31
title= ????
content=this is my test document number two ???????
3. document=4, weight=1442, group_id=2, date_added=Tue Feb 26 10:25:31 2013
id=4
group_id=2
group_id2=8
date_added=2013-02-26 10:25:31
title=doc number four
content=this is to test groups
words:
1. 'test': 3 documents, 4 hits
index 'test1stemmed': search error: failed to open D:/sphinx/data/test1stemmed.s
ph: No such file or directory.
最後面的一句error可忽略;
9、搜索中文,首先將資料庫中的內容update含有中文,執行sql語句:
UPDATE documents SET title='中文', content='this is my test document number one. also checking search within phrases.含有中文。' WHERE id=1;
UPDATE documents SET title='中文標題', content='this is my test document number one. also checking search within phrases.含有中文內容。' WHERE id=2;
UPDATE documents SET title='中文標題測試', content='this is my test document number one. also checking search within phrases.含有中文內容。' WHERE id=3;
重新建立索引(執行第7步);
然後執行search.exe 中文
Sphinx 2.0.6-id64-release (r3473)
Copyright (c) 2001-2012, Andrew Aksyonoff
Copyright (c) 2008-2012, Sphinx Technologies Inc (http://sphinxsearch.com)
using config file './sphinx.conf'...
indexing index 'test1'...
collected 4 docs, 0.0 MB
sorted 0.0 Mhits, 100.0% done
total 4 docs, 349 bytes
total 0.025 sec, 13808 bytes/sec, 158.26 docs/sec
total 2 reads, 0.000 sec, 0.3 kb/call avg, 0.0 msec/call avg
total 9 writes, 0.000 sec, 0.2 kb/call avg, 0.0 msec/call avg
D:\sphinx\bin>search.exe 中文
Sphinx 2.0.6-id64-release (r3473)
Copyright (c) 2001-2012, Andrew Aksyonoff
Copyright (c) 2008-2012, Sphinx Technologies Inc (http://sphinxsearch.com)
using config file './sphinx.conf'...
index 'test1': query '中文 ': returned 0 matches of 0 total in 0.000 sec
words:
index 'test1stemmed': search error: failed to open D:/sphinx/data/test1stemmed.s
ph: No such file or directory.
沒有搜索到,因為windows命令行中的中文時GBK編碼格式,所以沒有匹配內容。我們可以使用PHP程序來試試;
10、進入D:\sphinx\api\目錄,可以發現sphinx支持php、java、ruby調用,並提供對應的test例子,這里我們使用php來操作,首先將api復制到D:\www\下並重命名為sphinxapi,因為我本機上apache的web目錄為D:\www,在D:\www\sphinxapi\下新建search.php,內容為:
<?php
require 'sphinxapi.php';
$s = new SphinxClient();
$s->SetServer('localhost', 9312);
$result = $s->Query('中國');
print_r($result);
echo '<br /><br />';
$result = $s->Query('中文');
print_r($result);
?>
然後回到cmd命令行中,開啟sphinx服務,執行searchd.exe(這個必須要執行的)
Sphinx 2.0.6-id64-release (r3473)
Copyright (c) 2001-2012, Andrew Aksyonoff
Copyright (c) 2008-2012, Sphinx Technologies Inc (http://sphinxsearch.com)
using config file './sphinx.conf'...
WARNING: compat_sphinxql_magics=1 is deprecated; please update your application
and config
WARNING: preopen_indexes=1 has no effect with seamless_rotate=0
listening on all interfaces, port=9312
listening on all interfaces, port=9306
precaching index 'test1'
precaching index 'test1stemmed'
WARNING: index 'test1stemmed': preload: failed to open D:/sphinx/data/test1stemm
ed.sph: No such file or directory; NOT SERVING
precaching index 'rt'
WARNING: index 'rt': preload: failed to open @CONFDIR@/data/rt.lock: No such fil
e or directory; NOT SERVING
precached 3 indexes in 0.018 sec
成功...
然後在瀏覽器中執行http://localhost/sphinxapi/search.php,列印出來的數組結果可以很清晰的看見搜索的結果比對。。。
11、至此sphinx在windows下的簡單安裝與使用就完成了。
C. 使用了 sphinxapi.php 是不是不需要安裝sphinx擴展了
類似的這種第三方功模塊功能,不外乎就是要麼安裝php的擴展功能,然後直接調用其功能包含的內置方法,要麼就是直接include其api文件,都是一樣的道理,最後實例化,調用。
D. 如何調用Sphinx 和api介面
如何調用Sphinx
按上面配置,第5節點對資料庫進行了索引,通過Sphinx自帶的search(在bin/release目錄)就可以在命令行進行搜索:
(搜索CGArt)
windows上:
search -c d:/sphinx/sphinx.conf CGArt
Linux上:
cd /usr/local/sphinx
./bin/search -c sphinx.conf CGArt
運行後,系統提示一堆信息:
....
....
words:
1. 'cgart': 36 documents, 189 hits
這個表示庫中有36條記錄符合要求,出現CGArt的有189處。應用程序如果想調用Sphinx,可以從兩個方面:
一是通過Sphinx官方提供的API介面(介面有python,Java,Php三種版本)
二是通過安裝SphinxSE(具體見1.2部分),然後創建一個中介sphinxSE類型的表,再通過執行特定的SQL語句實現。
通過官方API調用Sphinx(以PHP為例)
在sphinx安裝目錄有一個API目錄,裡面有三個PHP文件:test.php,test2.php和sphinxapi.php。 sphinxapi.php是sphinx調用介面封裝文件,test.php是一個在命令行下執行的查詢例子文件,test2.php是一個生成摘要的 例子文件。
在命令下行運行test.php(Linux上沒有API目錄,需要從源程序包中復制api目錄至/usr/local/sphinx)
Windows上:
D:\sphinx\bin\release>c:\php5.2\php.exe -c c:\php5.2\php.ini ..\..\api\test.php -i cgfinal CGartLinux上(php在/usr/local/php目錄,sphinx.conf在/usr/local/sphinx目錄):
cd /usr/local/sphinx
/usr/local/php/bin/php api/test.php -i cgfinal CGArtSphinx的API查詢介面主要有這些內容(其實對照 一下sphinxapi.php就清楚了):
//創建Sphinx的客戶端介面對象
$cl = new SphinxClient ();
//設置連接Sphinx主機名與埠
$cl->SetServer('localhost',3312);
//可選,為每一個全文檢索欄位設置權重,主要根據你在sql_query中定義的欄位的順序,Sphinx系統以後會調整,可以按欄位名稱來設定權重
$cl->SetWeights ( array ( 100, 1 ) );
//設定搜索模式,SPH_MATCH_ALL,SPH_MATCH_ANY,SPH_MATCH_BOOLEAN,SPH_MATCH_EXTENDED,SPH_MATCH_PHRASE
$cl->SetMatchMode(SPH_MATCH_ALL);
//設定過濾條件$attribute是屬性名,相當於欄位名(用SPH_MATCH_EXTENDED時),$value是值,$exclude是布爾型,
當為true時,相當於$attribute!=$value,默認值是false
$cl->SetFilter($attribute, $values, $exclude);
//設定group by
//根據分組方法,匹配的記錄集被分流到不同的組,每個組都記錄著組的匹配記錄數以及根據當前排序方法本組中的最佳匹配記錄。
//最後的結果集包含各組的一個最佳匹配記錄,和匹配數量以及分組函數值
//結果集分組可以採用任意一個排序語句,包括文檔的屬性以及sphinx的下面幾個內部屬性
//@id--匹配文檔ID
//@weight, @rank, @relevance--匹配權重
//@group--group by 函數值
//@count--組內記錄數量
//$groupsort的默認排序方法是@group desc,就是按分組函數值大小倒序排列
$cl->SetGroupBy($attribute, $func, $groupsort);
//設定order by的內容,第一個參數是排序方法名,值有
// SPH_SORT_RELEVANCE,SPH_SORT_ATTR_DESC,SPH_SORT_ATTR_ASC,SPH_SORT_TIME_SEGMENTS,SPH_SORT_EXTENDED
//$sortby的值如"HITS desc"
$cl->SetSortMode(SPH_SORT_EXTENDED, $sortby);
//set count-distinct attribute for group-by queries,$distinct為字元串
$cl->SetGroupDistinct ( $distinct );
//相當於mysql的limit $offset,$limit
$cl->SetLimits($start,$limit)
//$q是查詢的關鍵字,$index是索引名稱,當等於*時表查詢所有索引
E. php 中文分詞難題 sphinx
1.看一下bin目錄是否存在search.exe
如果不存在,就不能用search命令,看你下載到的是什麼版本,有些版本是沒有這個文件的。
2.Query()的兩個參數,第一個是搜索的關鍵詞,第二個是索引。
沒有結果的話,請確保你的數據中有那個關鍵詞,還要確保索引存在,並配置正確。
以前弄這個的時候,我寫了篇筆記:https://my.oschina.net/agui1989/blog/738392
有興趣可以去看一下。
F. 在Ubuntu下安裝好sphinx 2.0之後怎麼測試是否安裝成功呢
sphinx 2.0安裝完成的時候,會有提示信息告知你安裝結果的。也可以在sphinx 2.0安裝好之後,找到一並安裝的php、python等多種語言的測試腳本,可以通過這些腳本來檢測sphinx是否安裝成功。或者直接通過 /usr/bin/indexer --all 命令來建索引從而檢查sphinx的運行狀況,當然建索引的前提是你的索引配置文件都已經配置ok。
G. sphinx 在 windows下到底怎麼安裝
一、關於Sphinx
Sphinx 是一個在GPLv2 下發布的一個全文檢索引擎,商業授權(例如, 嵌入到其他程序中)需要聯系作者(Sphinxsearch.com)以獲得商業授權。
一般而言,Sphinx是一個獨立的搜索引擎,意圖為其他應用提供高速、低空間佔用、高結果相關度的全文搜索功能。Sphinx可以非常容易的與SQL資料庫和腳本語言集成。
當前系統內置MySQL和PostgreSQL 資料庫數據源的支持,也支持從標准輸入讀取特定格式的XML數據。通過修改源代碼,用戶可以自行增加新的數據源(例如:其他類型的DBMS的原生支持)。
搜索API支持PHP、Python、Perl、Rudy和Java,並且也可以用作MySQL存儲引擎。搜索API非常簡單,可以在若干個小時之內移植到新的語言上。
Sphinx特性:
* 高速的建立索引(在當代CPU上,峰值性能可達到10MB/秒);
* 高性能的搜索(在2–4GB的文本數據上,平均每次檢索響應時間小於0.1秒);
* 可處理海量數據(目前已知可以處理超過100GB的文本數據,在單一CPU的系統上可處理100M文檔);
* 提供了優秀的相關度演算法,基於短語相似度和統計(BM25)的復合Ranking方法;
* 支持分布式搜索;
*
提供文件的摘錄生成;
*
可作為MySQL的存儲引擎提供搜索服務;
*
支持布爾、短語、詞語相似度等多種檢索模式;
*
文檔支持多個全文檢索欄位(最大不超過32個);
*
文檔支持多個額外的屬性信息(例如:分組信息,時間戳等);
*
停止詞查詢;
*
支持單一位元組編碼和UTF-8編碼;
*
原生的MySQL支持(同時支持MyISAM和InnoDB);
*
原生的PostgreSQL支持.
中文手冊可以在這里獲得(酷勤網備用下載地址:sphinx_doc_zhcn_0.9.pdf)。
二、Sphinx在windows上的安裝
1.直接在http://www.sphinxsearch.com/downloads.html找到最新的windows版本,我這里下的是Win32 release binaries with MySQL support,下載後解壓在D:\sphinx目錄下;
2.在D:\sphinx\下新建一個data目錄用來存放索引文件,一個log目錄方日誌文件,復制D:\sphinx\sphinx.conf.in到D:\sphinx\bin\sphinx.conf(注意修改文件名);
3.修改D:\sphinx\bin\sphinx.conf,我這里列出需要修改的幾個:
type = mysql # 數據源,我這里是mysql
sql_host = localhost # 資料庫伺服器
sql_user = root # 資料庫用戶名
sql_pass = '' # 資料庫密碼
sql_db = test # 資料庫
sql_port = 3306 # 資料庫埠
sql_query_pre = SET NAMES utf8 # 去掉此行前面的注釋,如果你的資料庫是uft8編碼的
index test1
{
# 放索引的目錄
path = D:/sphinx/data/
# 編碼
charset_type = utf-8
# 指定utf-8的編碼表
charset_table = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F
# 簡單分詞,只支持0和1,如果要搜索中文,請指定為1
ngram_len = 1
# 需要分詞的字元,如果要搜索中文,去掉前面的注釋
ngram_chars = U+3000..U+2FA1F
}
# index test1stemmed : test1
# {
# path = @CONFDIR@/data/test1stemmed
# morphology = stem_en
# }
# 如果沒有分布式索引,注釋掉下面的內容
# index dist1
# {
# 'distributed' index type MUST be specified
# type = distributed
# local index to be searched
# there can be many local indexes configured
# local = test1
# local = test1stemmed
# remote agent
# multiple remote agents may be specified
# syntax is 'hostname:port:index1,[index2[,...]]
# agent = localhost:3313:remote1
# agent = localhost:3314:remote2,remote3
# remote agent connection timeout, milliseconds
# optional, default is 1000 ms, ie. 1 sec
# agent_connect_timeout = 1000
# remote agent query timeout, milliseconds
# optional, default is 3000 ms, ie. 3 sec
# agent_query_timeout = 3000
# }
# 搜索服務需要修改的部分
searchd
{
# 日誌
log = D:/sphinx/log/searchd.log
# PID file, searchd process ID file name
pid_file = D:/sphinx/log/searchd.pid
# windows下啟動searchd服務一定要注釋掉這個
# seamless_rotate = 1
}
4.導入測試數據
C:\Program Files\MySQL\MySQL Server 5.0\bin>mysql -uroot test<d:/sphinx/example.sql
5.建立索引
D:\sphinx\bin>indexer.exe –all
Sphinx 0.9.8-release (r1533)
Copyright (c) 2001-2008, Andrew Aksyonoff
using config file 『./sphinx.conf』…
indexing index 『test1′…
collected 4 docs, 0.0 MB
sorted 0.0 Mhits, 100.0% done
total 4 docs, 193 bytes
total 0.101 sec, 1916.30 bytes/sec, 39.72 docs/sec
D:\sphinx\bin>
6.搜索』test』試試
D:\sphinx\bin>search.exe test
Sphinx 0.9.8-release (r1533)
Copyright (c) 2001-2008, Andrew Aksyonoff
using config file 『./sphinx.conf』…
index 『test1′: query 『test 『: returned 3 matches of 3 total in 0.000 sec
displaying matches:
1. document=1, weight=2, group_id=1, date_added=Wed Nov 26 14:58:59 2008
id=1
group_id=1
group_id2=5
date_added=2008-11-26 14:58:59
title=test one
content=this is my test document number one. also checking search within
phrases.
2. document=2, weight=2, group_id=1, date_added=Wed Nov 26 14:58:59 2008
id=2
group_id=1
group_id2=6
date_added=2008-11-26 14:58:59
title=test two
content=this is my test document number two
3. document=4, weight=1, group_id=2, date_added=Wed Nov 26 14:58:59 2008
id=4
group_id=2
group_id2=8
date_added=2008-11-26 14:58:59
title=doc number four
content=this is to test groups
words:
1. 『test』: 3 documents, 5 hits
D:\sphinx\bin>
都所出來了吧。
6.測試中文搜索
修改test資料庫中documents數據表,
UPDATE `test`.`documents` SET `title` = 『測試中文』, `content` = 『this is my test document number two,應該搜的到吧』 WHERE `documents`.`id` = 2;
重建索引:
D:\sphinx\bin>indexer.exe –all
搜索』中文』試試:
D:\sphinx\bin>search.exe 中文
Sphinx 0.9.8-release (r1533)
Copyright (c) 2001-2008, Andrew Aksyonoff
using config file 『./sphinx.conf』…
index 『test1′: query 『中文 『: returned 0 matches of 0 total in 0.000 sec
words:
D:\sphinx\bin>
貌似沒有搜到,這是因為windows命令行中的編碼是gbk,當然搜不出來。我們可以用程序試試,在D:\sphinx\api下新建一個foo.php的文件,注意utf-8編碼
<?php
require 』sphinxapi.php』;
$s = new SphinxClient();
$s->SetServer(』localhost』,3312);
$result = $s->Query(』中文』);
var_mp($result);
?>
啟動Sphinx searchd服務
D:\sphinx\bin>searchd.exe
Sphinx 0.9.8-release (r1533)
Copyright (c) 2001-2008, Andrew Aksyonoff
WARNING: forcing –console mode on Windows
using config file 『./sphinx.conf』…
creating server socket on 0.0.0.0:3312
accepting connections
執行PHP查詢:
php d:/sphinx/api/foo.php
H. php中如何使用sphinx搜索引擎
一. 安裝、配置
1.ubuntu安裝sphinx
如果沒有安裝aptitude ,需要先安裝 aptitude
2.配置
3.執行命令分詞,會在/var/lib/sphinxsearch/data/test1 目錄下生成一堆索引文件
sudo indexer -c /etc/sphinxsearch/sphinx.conf test1
test1為上述配置文件的index名字
4.命令行測試搜索
sudo search -c /etc/sphinxsearch/sphinx.conf google
二.在php中使用,安裝php、sphinx的依賴庫
安裝 aptitudeapt-get install aptitude
sudo aptitude install libsphinxclient-dev libsphinxclient-0.0.1 -y
2.安裝php sphinx的擴展
安裝 pecl
sudo apt-get install php-pear php5-dev
在安裝sphinx
sudo pecl install sphinx
3.在配置文件php.ini中添加sphinx的擴展,
我的php.ini文件為
sudo vim /etc/php5/fpm/php.ini
獲取自己的php.ini文件位置使用
php5-fpm -i|grep ini
添加:
extension=sphinx.so
4.重啟php5-fpm,查看php是否載入sphinx模塊
sudo /etc/init.d/php5-fpm restart
5.將search程序運行在後台
sudo searchd -c /etc/sphinxsearch/sphinx.conf
默認監聽配置文件中的埠:9312
6.在thinkphp中調用搜索
搜索完畢,返回結果(默認返回20條,修改返回條數用添加$s->SetLimits(0, 1000, 1000);),搜索速度相當快,索引80w條數據的title和description兩個欄位耗時不到10s,該搜索引擎支持增量索引,多種模式的搜索,網上的資料也非常多
I. 怎麼查看php安裝sphinx是否成功
如何調用Sphinx 按上面配置,第5節點對資料庫進行了索引,通過Sphinx自帶的search(在bin/release目錄)就可以在命令行進行搜索: (搜索CGArt) windows上: search -c d:/sphinx/sphinx.conf CGArt Linux上: cd /usr/local/sphinx ./bin/sear...