一、xunsearch和sphinx的對比
二、xunsearch的安裝
三、xunsearch的使用
四、xunsearch的卸載
根目錄地址/bin/xs-ctl.sh faststop rm -fr $prefix
2.1、安裝編譯工具 yum install make gcc g++ gcc-c++ libtool autoconf automake imake mysql-devel libxml2-devel expat-devel
2.2、進入欲安裝文件夾 例如:cd /usr/loacl/
-
運行下面指令下載、解壓安裝包
wget http://www.xunsearch.com/download/xunsearch-full-latest.tar.bz2 tar -xjf xunsearch-full-latest.tar.bz2
-
執行安裝腳本,根據提示進行操作,主要是輸入
xunsearch
軟件包的安裝目錄,強烈建議單獨 規划一個目錄,而不是混到別的軟件目錄中。cd xunsearch-full-1.3.0/ sh setup.sh
- 如果您的 SDK 調用和
xunsearch
服務端不在同一服務器,請使用 -b inet 方式啟動腳本, 並注意借助類似iptables
的防火牆來控制xunsearch
的8383/8384
兩個端口的訪問權限。啟動腳本用法舉例如下,以下均為合法使用方式: -
bin/xs-ctl.sh -b local start // 監聽在本地回環地址 127.0.0.1 上 bin/xs-ctl.sh -b inet start // 監聽在所有本地 IP 地址上 bin/xs-ctl.sh -b a.b.c.d start // 監聽在指定 IP 上 我是不在同一服務器上,因此我選擇這一種(bin/xs-ctl.sh -b 195.241.23.32 start) bin/xs-ctl.sh -b unix start // 分別監聽在 tmp/indexd.sock 和 tmp/searchd.sock
啟動啟動/重新啟動 xunsearch 的后台服務$prefix 替換為你的xunsearch安裝目錄cd $prefix;bin/xs-ctl.sh restart添加到開機啟動腳本,在 Linux 系統中將腳本指令$prefix/bin/xs-ctl.sh restart寫進 /etc/rc.local 即可配置防火牆,開啟8383端口、8384端口 -
vi /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 8383 -j ACCEPT #允許8383端口通過防火牆 -A INPUT -m state --state NEW -m tcp -p tcp --dport 8484 -j ACCEPT #允許8384端口通過防火牆
/etc/init.d/iptables restart #重啟防火牆使配置生效
- 使用 netstat -anlpt 查看端口 (殺掉占用端口的進程 kill -9 進程id)
- 檢測 PHP-SDK 的運行條件
$prefix/sdk/php/util/RequiredCheck.php $prefix #替換成你的安裝目錄
- 至此,安裝和准備工作已經完成了,您可以開始使用 Xunsearch PHP-SDK 開發自己的搜索應用了。
-
- 建立索引 先創建要索引的數據庫表
創建庫:CREATE DATABASE `test2` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
創建表:……
先在/usr/local/xunsearch/sdk/php/app配置ini文件:project.name = source
project.default_charset = utf-8
server.index = 23.27.127.32:8383
server.search = 23.27.127.32:8384[id]
type = id[product_name]
type = title[product_model]
index = self導入mysql數據到xunsearch里面:/usr/local/xunsearch/sdk/php/util/Indexer.php --rebuild --source=mysql://yourdbuser:yourdbpass@localhost/yourdbname --sql="select id,product_name,product_model from products" --project=source
以上為服務器部分 以下為php調用部分
-
<html> <head> <meta http-equiv="content-type" content="text/html;charset=gbk" /> <title>coreseek中文全文搜索在php程序中的應用</title> </head> <body> <h3><font color="blue">coreseek全文搜索在php程序中應用</font></h3> <form action="test2.php" method="post"> 輸入搜索的關鍵詞:<input type="text" name="keyword" size="30" <?php echo $_POST['keyword'];?> /> <input type="submit" name="sub" value="搜索" /> </form> <hr /> <?php echo "<pre />"; #引入接口文件,其實你懂的,就是一個類 require './lib/XS.php'; // 引入 xunsearch sdk if(isset($_POST['sub']) && $_POST['keyword'] != ''){ $keyword = trim($_POST['keyword']); //接收關鍵詞 $xs = new XS('source'); // demo 為項目名稱,配置文件是:$sdk/app/demo.in i //$index = $xs->index; // 獲取索引對象 $search = $xs->search; // 獲取搜索對象 $search->setLimit(20); $docs = $search->setQuery( $keyword)->search(); // 搜索 ‘ 測試’ echo "<table border='1' bordercolor='green' cellspacing='0'><tr><th>標題</th></tr>"; foreach ($docs as $doc) { $subject = $search->highlight($doc->product_name); // 高亮處理標題 echo "<tr><td>".$subject."</td></tr>"; } echo "</table>"; // $search->setQuery($keyword); // // 獲取前 6 個和默認搜索語句 "西湖" 相關搜索詞 // $words = $search->getRelatedQuery(); // print_r($words); } ?>