Home php教程 PHP源码 xunsearch搜索类封装

xunsearch搜索类封装

May 25, 2016 pm 05:07 PM

php代码

<?php
/**
 * SoClass.php
 * 索引与搜索类
 * 作者:tonyfeng (465200185@qq.com)
 * 创建时间:2012-12-19 11:44:42
 */

class SoClass {
	
	private $_xindex;
	
	private $_xsearch;
	
	private $_project;
	
	public function __construct($project){

		//载入引导文件
		require_once &#39;lib/XS.php&#39;;
		//初始化
		$xs = new XS($project); 
		$this->_project = $project;
		$this->_xindex = $xs->index; 
		$this->_xsearch = $xs->search;
		$this->_xsearch->setCharset(&#39;UTF-8&#39;);
	}
	
	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][&#39;pid&#39;] = $doc->pid;
				$data[$key][&#39;nid&#39;] = $doc->nid;
				$data[$key][&#39;category&#39;] = $doc->category;
				$data[$key][&#39;url&#39;] = $doc->url;
				$data[$key][&#39;name&#39;] = $xs->search->highlight(htmlspecialchars($doc->name));
				$data[$key][&#39;message&#39;] = $xs->search->highlight(htmlspecialchars($doc->message));
			}
			
			return array(&#39;data&#39;=>$data,&#39;count&#39;=>$count);
		}
		return array();
	}
	
	public function hotWord($num,$type=&#39;lastnum&#39;){
	
		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(&#39;1&#39;,&#39;2&#39;,&#39;3&#39;))
		$this->_xindex->del($idArray); 
	}
	
	public function addSynonym($word1,$word2){
		
		$this->_xindex->addSynonym($word1,$word2);
	}
	
	public function clearIndex(){
		
		$this->_xindex->clean();
	}
	
	
}

?>
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)