PHP環境(版本5.4及以上)
Xunsearch搜索引擎服務器(請下載並安裝)
require_once '/path/to/sdk/php/lib/XS.php';
$xs = new XS('/path/to/xunsearch/app.ini');
$search = $xs->search; $search->setFuzzy(); $search->setLimit(10); $search->setScwsMulti(3); $search->addWeight('title', 10); $search->addWeight('content', 5);
$keyword = $_GET['q']; $result = $search->search($keyword);
foreach ($result as $item) { echo $item->title . '<br> '; echo $item->content . '<br><br> '; }
require_once '/path/to/sdk/php/lib/XS.php';
$xs = new XS('/path/to/xunsearch/app.ini');
$index = $xs->index;
$pdo = new PDO('mysql:host=localhost;dbname=test', 'username', 'password'); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $pdo->prepare('SELECT * FROM articles'); $stmt->execute(); $data = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($data as $item) { $doc = new XSDocument(); $doc->setFields($item); $index->update($doc); }
$index->flushIndex();