php实现搜索类封装示例
发布:smiling 来源: PHP粉丝网 添加日期:2021-07-24 16:36:01 浏览: 评论:0
这篇文章主要为大家详细介绍了php实现搜索类封装示例,感兴趣的小伙伴们可以参考一下,本文为大家分享了php实现搜索类封装示例,供大家参考,具体内容如下:
- <?php
- /**
- * SoClass.php
- * 索引与搜索类 */
- class SoClass {
- private $_xindex;
- private $_xsearch;
- private $_project;
- public function __construct($project){
- //载入引导文件
- require_once 'lib/XS.php';
- //初始化
- $xs = new XS($project);
- $this->_project = $project;
- $this->_xindex = $xs->index;
- $this->_xsearch = $xs->search;
- $this->_xsearch->setCharset('UTF-8');
- }
- public function query($keyWord,$row=20,$jnum=0){
- $xs = new XS($this->_project);
- $xs->search->setFuzzy();
- $xs->search->setAutoSynonyms();
- $xs->search->setQuery($keyWord); //支持同义词搜索,默认打开
- $xs->search->setLimit($row, $jnum); //设置返回结果最多为 5 条,并跳过前 10 条
- $docs = $xs->search->search(); //执行搜索,将搜索结果文档保存在 $docs 数组中
- $count = $xs->search->count(); //获取搜索结果的匹配总数估算值
- if($count){
- $data = array();
- foreach ($docs as $key=>$doc){
- $data[$key]['pid'] = $doc->pid;
- $data[$key]['nid'] = $doc->nid;
- $data[$key]['category'] = $doc->category;
- $data[$key]['url'] = $doc->url;
- $data[$key]['name'] = $xs->search->highlight(htmlspecialchars($doc->name));
- $data[$key]['message'] = $xs->search->highlight(htmlspecialchars($doc->message));
- }
- return array('data'=>$data,'count'=>$count);
- }
- return array();
- }
- public function hotWord($num,$type='lastnum'){
- return $this->_xsearch->getHotQuery($num,$type);
- }
- public function expanded($keyWord){
- return $this->_xsearch->getExpandedQuery($keyWord);
- }
- public function lastCount(){
- return $this->_xsearch->getLastCount();
- }
- public function index($data,$update=0){
- // 创建文档对象
- $doc = new XSDocument;
- $doc->setFields($data);
- // 添加或更新到索引数据库中
- if(!$update){
- $this->_xindex->add($doc);
- }else{
- $this->_xindex->update($doc);
- }
- }
- public function delete($idArray){
- //删除索引(主键删除array('1','2','3'))
- $this->_xindex->del($idArray);
- }
- public function addSynonym($word1,$word2){
- $this->_xindex->addSynonym($word1,$word2);
- }
- public function clearIndex(){
- $this->_xindex->clean();
- }
- }
- ?>
以上就是本文的全部内容,希望对大家学习php程序设计有所帮助。
Tags: php搜索类
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)