coreseek实战(四):php接口的使用,完善php脚本代码

在上一篇文章 coreseeek实战(三)中,已经能够正常搜索到结果,这篇文章主要是把 index.php 文件代码写得相对完整一点点(过滤、权重设定等等很多设置仍然没有使用),同时记录一下在测试过程中出现的问题。

index.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="index.php" method="post">
输入搜索的关键词:<input type="text" name="keyword" size="30" />
<input type="submit" name="sub" value="搜索" />
</form>
<hr />
<?php
echo "<pre />";
#引入接口文件,其实你懂的,就是一个类
require_once('sphinxapi.php');
if(isset($_POST['sub']) && $_POST['keyword'] != ''){
    $keyword = trim($_POST['keyword']);    //接收关键词

    $sph = new SphinxClient();            //实例化 sphinx 对象
    $sph->SetServer('localhost',9312);    //连接9312端口
    $sph->SetMatchMode(SPH_MATCH_ANY);    //设置匹配方式
    $sph->SetSortMode(SPH_SORT_RELEVANCE);    //查询结果根据相似度排序
    $sph->SetArrayResult(false);            //设置结果返回格式,true以数组,false以PHP hash格式返回,默认为false
    
    /**
        *关键词高亮显示,以及产生文本摘要
        *BuildExcerpts($docs, $index, $words, $opts=array())
        *参数(包含文档内容的数组,索引名,关键词,高亮参数)
    **/
    $opts = array(
        "before_match"    => "<font color='red'>",    //关键词高亮开始的html代码
        "after_match"    => "</font>",                //关键词高亮结束的html代码
        "limit"            => 100,                        //摘要最多包含的符号数,默认256
        "around"        => 3,                        //每个关键词左右选取的词的数目,默认为5
    );
    
    $result = $sph->Query($keyword,'dede');//执行搜索操作,参数(关键词,[索引名])
    if(!array_key_exists('matches', $result)){    //如果没有匹配结果,直接返回
        echo "搜索无结果";
        return;
    }
    $arr_key = array_keys($result['matches']);    
    //获取到匹配文章的ID
    $ids = implode(',',$arr_key);    //数组转成字符串
    echo "<font color='blue'>按相关性排序id(结果数:",count($arr_key),"):</font>",$ids,"<hr />";
    
    //连接数据库
    $mysqli = new Mysqli('localhost','root','123456','dedecmsv57gbksp1');
    $mysqli->query('set names gbk');
    $query = "select typeid,id,title,description from dede_archives where id in({$ids}) order by find_in_set(id,'{$ids}')";
    $res = $mysqli->query($query);
    echo "<table border='1' bordercolor='green' cellspacing='0'><tr><th>文章栏目</th><th>文章id</th><th>标题</th><th>描述</th></th></tr>";
    
    //搜索词没有高亮显示
    /* while($row = $res->fetch_assoc()){
        echo "<tr><td>",$row['typeid'],"</td><td>",$row['id'],"</td><td>",$row['title'],"</td><td>",$row['description'],"</td></tr>";
    } */
    
    //使用高亮显示代码
    while($row = $res->fetch_assoc()){
        $result = $sph->BuildExcerpts($row, 'dede', $keyword, $opts);
        if(!$res){
            die("Error:".$sph->GetLastError());
        }
        echo "<tr><td>",$result[0],"</td><td>",$result[1],"</td><td>",iconv('utf-8','gbk',$result[2]),"</td><td>",iconv('utf-8','gbk',$result[3]),"</td></tr>";
    }
    
    echo "</table>";    
}
?>

存在的问题总结:

(1)最严重的就是速度问题:使用高亮函数 buildExcerpts() 后,查询速度变得异常之慢,需要10~20秒这样;而不使用高亮,基本上 1 秒左右出结果。

(2)高亮之后,从数据库读取出来的内容为乱码,只能做转换。但数据库编码为gbk,且文档编码、header设置的编码也已经设置为gbk,不知道是哪里的编码出了问题。

(3)使用 php api 接口时,不解的是,有时候搜索无结果;而折腾一下,索引重建、searchd服务重启等等之后,便可正常(关键是有时候这些操作都搞过一遍,还是不行,搞几次却好了,灰常的郁闷)

原文地址:https://www.cnblogs.com/phpway/p/2908655.html