標籤:pxc dtd 引用 arp mss osd pmu eee XML
由於項目需要全文檢索索引,後面就去網上查了下資料,找到了Sphinx【中文是獅身人面像】這個全文檢索索引引擎,聽說挺好用的,不過沒有中文分詞。後面又去找了一下,找到了Coreseek,一款中文全文檢索索引/搜尋軟體。
一、Sphinx PHP擴充下載
PHP已經有專門的Sphinx的擴充檔案,點擊這裡可以下載到不同版本的擴充。
擴充安裝成功後,就能看到sphinx資訊了。
也可以通過引用一個php類檔案達到同樣的效果,檔案是在api檔案夾下面的sphinxapi.php。這個更好用點,在調試的時候發現,如果是直接引用的那個dll有時候會報方法不存在,但是明明在PHP:Sphinx的API說明裡面是有的。
二、安裝Coreseek
1) 我這邊下載了4.1的版本。按照網站上面寫的過程,我在本地布了一下。
這個local目錄其實可以自己隨便建立,這裡建的我感覺好深,打dos命令的時候挺麻煩的。
2) 開啟下載下來的壓縮包,etc裡面有很多conf的設定檔。
在var\test檔案裡有個documents.sql的檔案,這一個demo資料庫,等下就會用這個做測試。
3) 把etc中的csft_mysql.conf檔案複製到bin中,並改名為sphinx.conf。
因為我的全文檢索索引需要配合MySQL資料庫,所以需要配置這個檔案。
4) 修改設定檔,剛開始設定檔沒設定好,老會報錯。
#MySQL資料來源配置,詳情請查看:http://www.coreseek.cn/products-install/mysql/#請先將var/test/documents.sql匯入資料庫,並配置好以下的MySQL使用者密碼資料庫#源定義source mysql{ type = mysql sql_host = localhost sql_user = root sql_pass = 123456 sql_db = sphinx sql_port = 3306 sql_query_pre = SET NAMES utf8 sql_query = SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content FROM documents #sql_query第一列id需為整數 #title、content作為字串/文字欄位,被全文索引 sql_attr_uint = group_id #從SQL讀取到的值必須為整數 sql_attr_timestamp = date_added #從SQL讀取到的值必須為整數,作為時間屬性 sql_query_info_pre = SET NAMES utf8 #命令列查詢時,設定正確的字元集 sql_query_info = SELECT * FROM documents WHERE id=$id #命令列查詢時,從資料庫讀取未經處理資料資訊}#index定義index mysql{ source = mysql #對應的source名稱 path = C:/usr/local/coreseek-4.1-win32/var/data/documents #請修改為實際使用的絕對路徑,例如:/usr/local/coreseek/var/... docinfo = extern mlock = 0 morphology = none min_word_len = 1 html_strip = 0 #中文分詞配置,詳情請查看:http://www.coreseek.cn/products-install/coreseek_mmseg/ #charset_dictpath = /usr/local/mmseg3/etc/ #BSD、Linux環境下設定,/符號結尾 charset_dictpath = C:/usr/local/coreseek-4.1-win32/etc/ #Windows環境下設定,/符號結尾,最好給出絕對路徑,例如:C:/usr/local/coreseek/etc/... charset_type = zh_cn.utf-8}#全域index定義indexer{ mem_limit = 128M}#searchd服務定義searchd{ compat_sphinxql_magics = 0 listen = 9312 read_timeout = 5 max_children = 30 max_matches = 1000 seamless_rotate = 0 preopen_indexes = 0 unlink_old = 1 pid_file = C:/usr/local/coreseek-4.1-win32/var/log/searchd_mysql.pid #請修改為實際使用的絕對路徑,例如:/usr/local/coreseek/var/... log = C:/usr/local/coreseek-4.1-win32/var/log/searchd_mysql.log #請修改為實際使用的絕對路徑,例如:/usr/local/coreseek/var/... query_log = C:/usr/local/coreseek-4.1-win32/var/log/query_mysql.log #請修改為實際使用的絕對路徑,例如:/usr/local/coreseek/var/... binlog_path = #關閉binlog日誌}
5) 建立索引,打一句dos命令就行。
在#index定義中,設定了path的路徑,在這個路徑裡面能夠看到索引檔案。
6) 開啟控制台讓Sphinx監聽連接埠,接收搜尋命令。
searchd 可以安裝成一個Windows服務,命令如下:
C:\usr\local\coreseek> C:\usr\local\coreseek\bin\searchd.exe --install --config C:\usr\local\coreseek\etc\coreseek.conf --servicename Coreseek
這樣 searchd
服務應該出現在“控制台->系統管理->服務”的列表中了. 服務應該出現在“控制台->系統管理->服務”的列表中了。
刪除服務的命令如下:
sc delete Coreseek
三、PHP代碼測試
<?phpheader(‘Content-Type: text/html; charset=utf-8‘);//防止中文顯示為亂碼$s = new SphinxClient;//已經引入擴充,所以不需要額外require檔案了$s->setServer("127.0.0.1", 9312); $s->setMatchMode(SPH_MATCH_PHRASE); $s->setMaxQueryTime(30); $res = $s->query(‘愚人‘, ‘mysql‘); #[愚人]關鍵字,[mysql]資料來源source $err = $s->GetLastError(); echo ‘<pre>‘; var_dump($res); var_dump($err); echo ‘</pre>‘;
查看到結果:
與資料庫比對一下,可以看到搜尋出了第一條的資料。
四、計算經緯度
現在時髦的APP用戶端,基本上都會涉及到一個功能,就是尋找附近的XX,在我們服務端就是需要計算經緯度距離了。原先我們的做法是在MySQL中自訂一個計算兩個經緯度點距離的函數,在做查詢的時候調用,現在用Coreseek可以直接調用一個方法SetGeoAnchor。
先看看Coreseek中對這個方法的說明:
原型: function SetGeoAnchor ( $attrlat, $attrlong, $lat, $long )為地理距離計算設定錨點,並且允許使用它們。$attrlat 和 $attrlong是字串,分別指定了對應經度和緯度的屬性名稱。
$lat 和 $long是浮點值,指定了錨點的經度和緯度值,以角度為單位。
再看看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這兩個其實是弧度,而不是翻譯的角度。
1)在做索引的時候 ,將經緯度資料來源轉換成弧度。 radians是MySQL中由度轉化為弧度的函數。
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函數將角度轉換為弧度。
$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公裡的地點
3)查看頁面結果,距離大約為49749;同時我用以前的那個MySQL自訂函數,在資料庫中跑了一下,距離是49704,兩個結果差不多。
Coreseek Windows下安裝調試