Coreseek Windows下安装调试
由于項(xiàng)目需要全文檢索,后面就去網(wǎng)上查了下資料,找到了Sphinx【中文是獅身人面像】這個全文檢索引擎,聽說挺好用的,不過沒有中文分詞。后面又去找了一下,找到了Coreseek,一款中文全文檢索/搜索軟件。
?
一、Sphinx PHP擴(kuò)展下載
PHP已經(jīng)有專門的Sphinx的擴(kuò)展文件,點(diǎn)擊這里可以下載到不同版本的擴(kuò)展。
擴(kuò)展安裝成功后,就能看到sphinx信息了。
也可以通過引用一個php類文件達(dá)到同樣的效果,文件是在api文件夾下面的sphinxapi.php。這個更好用點(diǎn),在調(diào)試的時候發(fā)現(xiàn),如果是直接引用的那個dll有時候會報方法不存在,但是明明在PHP:Sphinx的API說明里面是有的。
?
二、安裝Coreseek
1) 我這邊下載了4.1的版本。按照網(wǎng)站上面寫的過程,我在本地布了一下。
這個local目錄其實(shí)可以自己隨便建立,這里建的我感覺好深,打dos命令的時候挺麻煩的。
?
2) 打開下載下來的壓縮包,etc里面有很多conf的配置文件。
在var\test文件里有個documents.sql的文件,這一個demo數(shù)據(jù)庫,等下就會用這個做測試。
?
3) 把etc中的csft_mysql.conf文件復(fù)制到bin中,并改名為sphinx.conf。
因?yàn)槲业娜臋z索需要配合MySQL數(shù)據(jù)庫,所以需要配置這個文件。
?
4) 修改配置文件,剛開始配置文件沒設(shè)置好,老會報錯。
#MySQL數(shù)據(jù)源配置,詳情請查看:http://www.coreseek.cn/products-install/mysql/ #請先將var/test/documents.sql導(dǎo)入數(shù)據(jù)庫,并配置好以下的MySQL用戶密碼數(shù)據(jù)庫#源定義 source mysql {type = mysqlsql_host = localhostsql_user = rootsql_pass = 123456sql_db = sphinxsql_port = 3306sql_query_pre = SET NAMES utf8sql_query = SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content FROM documents#sql_query第一列id需為整數(shù)#title、content作為字符串/文本字段,被全文索引sql_attr_uint = group_id #從SQL讀取到的值必須為整數(shù)sql_attr_timestamp = date_added #從SQL讀取到的值必須為整數(shù),作為時間屬性sql_query_info_pre = SET NAMES utf8 #命令行查詢時,設(shè)置正確的字符集sql_query_info = SELECT * FROM documents WHERE id=$id #命令行查詢時,從數(shù)據(jù)庫讀取原始數(shù)據(jù)信息 }#index定義 index mysql {source = mysql #對應(yīng)的source名稱path = C:/usr/local/coreseek-4.1-win32/var/data/documents #請修改為實(shí)際使用的絕對路徑,例如:/usr/local/coreseek/var/...docinfo = externmlock = 0morphology = nonemin_word_len = 1html_strip = 0#中文分詞配置,詳情請查看:http://www.coreseek.cn/products-install/coreseek_mmseg/#charset_dictpath = /usr/local/mmseg3/etc/ #BSD、Linux環(huán)境下設(shè)置,/符號結(jié)尾charset_dictpath = C:/usr/local/coreseek-4.1-win32/etc/ #Windows環(huán)境下設(shè)置,/符號結(jié)尾,最好給出絕對路徑,例如:C:/usr/local/coreseek/etc/...charset_type = zh_cn.utf-8 }#全局index定義 indexer {mem_limit = 128M }#searchd服務(wù)定義 searchd {compat_sphinxql_magics = 0listen = 9312read_timeout = 5max_children = 30max_matches = 1000seamless_rotate = 0preopen_indexes = 0unlink_old = 1pid_file = C:/usr/local/coreseek-4.1-win32/var/log/searchd_mysql.pid #請修改為實(shí)際使用的絕對路徑,例如:/usr/local/coreseek/var/...log = C:/usr/local/coreseek-4.1-win32/var/log/searchd_mysql.log #請修改為實(shí)際使用的絕對路徑,例如:/usr/local/coreseek/var/...query_log = C:/usr/local/coreseek-4.1-win32/var/log/query_mysql.log #請修改為實(shí)際使用的絕對路徑,例如:/usr/local/coreseek/var/...binlog_path = #關(guān)閉binlog日志 }?
5) 建立索引,打一句dos命令就行。
在#index定義中,設(shè)置了path的路徑,在這個路徑里面能夠看到索引文件。
?
6) 打開控制臺讓Sphinx監(jiān)聽端口,接收搜索命令。
searchd 可以安裝成一個Windows服務(wù),命令如下:
C:\usr\local\coreseek> C:\usr\local\coreseek\bin\searchd.exe --install --config C:\usr\local\coreseek\etc\coreseek.conf --servicename Coreseek這樣?searchd?服務(wù)應(yīng)該出現(xiàn)在“控制面板->系統(tǒng)管理->服務(wù)”的列表中了. 服務(wù)應(yīng)該出現(xiàn)在“控制面板->系統(tǒng)管理->服務(wù)”的列表中了。
? ? 刪除服務(wù)的命令如下:
sc delete Coreseek?
三、PHP代碼測試
<?php header('Content-Type: text/html; charset=utf-8');//防止中文顯示為亂碼 $s = new SphinxClient;//已經(jīng)引入擴(kuò)展,所以不需要額外require文件了 $s->setServer("127.0.0.1", 9312); $s->setMatchMode(SPH_MATCH_PHRASE); $s->setMaxQueryTime(30); $res = $s->query('愚人', 'mysql'); #[愚人]關(guān)鍵字,[mysql]數(shù)據(jù)源source $err = $s->GetLastError(); echo '<pre>'; var_dump($res); var_dump($err); echo '</pre>';查看到結(jié)果:
與數(shù)據(jù)庫比對一下,可以看到搜索出了第一條的數(shù)據(jù)。
?
四、計算經(jīng)緯度
現(xiàn)在時髦的APP客戶端,基本上都會涉及到一個功能,就是尋找附近的XX,在我們服務(wù)端就是需要計算經(jīng)緯度距離了。原先我們的做法是在MySQL中自定義一個計算兩個經(jīng)緯度點(diǎn)距離的函數(shù),在做查詢的時候調(diào)用,現(xiàn)在用Coreseek可以直接調(diào)用一個方法SetGeoAnchor。
先看看Coreseek中對這個方法的說明:
原型: function SetGeoAnchor ( $attrlat, $attrlong, $lat, $long )為地理距離計算設(shè)置錨點(diǎn),并且允許使用它們。$attrlat 和 $attrlong是字符串,分別指定了對應(yīng)經(jīng)度和緯度的屬性名稱。$lat 和 $long是浮點(diǎn)值,指定了錨點(diǎn)的經(jīng)度和緯度值,以角度為單位。
再看看sphinx中API的說明:
Prototype: function SetGeoAnchor ( $attrlat, $attrlong, $lat, $long )Sets anchor point for and geosphere distance (geodistance) calculations, and enable them.$attrlat and $attrlong must be strings that contain the names of latitude and longitude attributes, respectively.$lat and $long are floats that specify anchor point latitude and longitude, in radians.
原文說的$lat 和 $long這兩個其實(shí)是弧度,而不是翻譯的角度。
?
1)在做索引的時候 ,將經(jīng)緯度數(shù)據(jù)源轉(zhuǎn)換成弧度。?radians是MySQL中由度轉(zhuǎn)化為弧度的函數(shù)。
sql_query = SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content,radians(longtitude) as longtitude, radians(latitude) as latitude FROM documents order by id desc
2)PHP中引用方法,并可做過濾與排序,deg2rad函數(shù)將角度轉(zhuǎn)換為弧度。
$lon = 121; $lat = 31; $s->SetGeoAnchor('latitude', 'longtitude', (float)deg2rad($lat), (float) deg2rad($lon)); $s->SetSortMode(SPH_SORT_EXTENDED, '@geodist asc'); // 按距離正向排序 //$s->SetFilterFloatRange('@geodist', 0.0, $radius); // 過濾掉大于10公里的地點(diǎn)3)查看頁面結(jié)果,距離大約為49749;同時我用以前的那個MySQL自定義函數(shù),在數(shù)據(jù)庫中跑了一下,距離是49704,兩個結(jié)果差不多。
總結(jié)
以上是生活随笔為你收集整理的Coreseek Windows下安装调试的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《Android应用开发攻略》——2.2
- 下一篇: AutoCAD_acadiso.dwt卡