xunsearch之php索引维护(四)


1.添加文档

$xs = new XS('njw');
$index = $xs->index;
$data = array(
'pid' => 234, // 此字段为主键,必须指定
'subject' => '测试文档的标题',
'message' => '测试文档的内容部分',
'chrono' => time()
);
//创建文档对象
$doc = new XSDocument;
$doc->setFields($data);
​//添加到索引数据库中
$index->add($doc);

2.更新文档

//创建文档对象
$doc = new XSDocument;
$doc->setFields($data);
//更新到索引数据库中
$index->update($doc);

3.平滑批量重建更新索引

header('Content-Type:text/html;charset=utf-8;');
require_once '../../../local/xunsearch/sdk/php/lib/XS.php';
include "./mysql_conn.php";
try{
$xs = new XS('njw');
//平滑重建索引
//宣布开始重建索引
$xs->index->beginRebuild();
$sql = "select g.id id,g.title title,g.norms norms,i.picture picture from b2b_goods g INNER JOIN b2b_goods_images i ON g.id=i.goods_id limit";
$result = $db->query($sql);
while( $row = $result -> fetch_assoc ()) {
$doc = new XSDocument;
$doc->setFields($row);
//添加到索引数据库中
$xs->index->add($doc);
$xs->index->update($doc);
}
//告诉服务器重建索引完成
$xs->index->endRebuild();
}catch(XSException $e){
echo $e;
}

4.使用缓冲区批量重建更新索引

header('Content-Type:text/html;charset=utf-8;');
require_once '../../../local/xunsearch/sdk/php/lib/XS.php';
include "./mysql_conn.php";
try{
    $xs = new XS('njw');
    //使用索引缓冲区
    $xs->index->openBuffer();
    $sql = "select g.id id,g.title title,g.norms norms,i.picture picture from b2b_goods g INNER JOIN b2b_goods_images i ON g.id=i.goods_id";
    $result = $db->query($sql);
    while( $row = $result -> fetch_assoc ()) {
        $doc = new XSDocument;
        $doc->setFields($row);
        //添加到索引数据库中
        $xs->index->add($doc);
        $xs->index->update($doc);
    }
    //告诉服务器重建索引完成
    $xs->index->closeBuffer();
}catch(XSException $e){
    echo $e;
}
原文地址:https://www.cnblogs.com/lisqiong/p/5509187.html