封閉xunsearch方便其進(jìn)行操作
require_once WEB_ROOT . '/lib/xs/php/lib/XS.class.php';
class Myxs {
private $xs;
private $scws;
private $doc;
private $index;
private $search;
private $_sort;
private static $myxs = [];
private function __construct($pro) {
$this->xs = new XS($pro);
$this->doc = new XSDocument();
$this->index = $this->xs->index;
$this->search = $this->xs->search;
$this->scws = new XSTokenizerSCWS;
}
public function getsearch() {
return $this->search;
}
public function getindex() {
return $this->index;
}
public function getScws() {
return $this->scws;
}
public static function init($pro){
if (!empty($myxs[$pro]) && self::$myxs[$pro] instanceof XS) {
return self::$myxs;
} else {
self::$myxs[$pro] = new self($pro);
}
return self::$myxs[$pro];
}
public function limit ($limit, $offset = 0) {
$this->search->setlimit($limit, $offset);
}
/**
* 排序time:[0,1]0反序1正序
* @param [type] $str [description]
* @return [type] [description]
*/
public function addsort($str) {
$this->_sort = $str;
}
public function clean () {
$this->index->clean();
}
/**
* 添加索引
* @param [type] $data [description]
* @param boolean $update [description]
* @return [type] [description]
*/
public function docadd($data,$update = true) {
if (!is_array($data)) {
return false;
}
//$doc = new XSDocument();
$this->doc->setFields($data);
if ($update) {
$this->index->update($this->doc);
} else {
$this->index->add($this->doc);
}
$this->index->flushIndex();
}
/**
* 刪除索引
* @param [type] $data [description]
* @return [type] [description]
*/
public function docdel($data) {
if (!is_array($data)) {
return false;
}
$this->doc->setFields($data);
$this->index->update();
}
/**
* 刷新索引
* @param string $doc [description]
* @return [type] [description]
*/
public static function docflush($doc = 'all'){
if ($doc == 'all'){
foreach(self::$myxs as $k => $v) {
try{
$v->index->flushIndex();
}catch(Exception $e) {
echo $e;
}
}
}else {
try{
self::$myxs[$doc]->index->flushIndex();
}catch(Exception $e){
echo $e;
}
}
}
public function getCount(){
$this->docflush();
return $this->search->getDbTotal();
}
/**
* 以數(shù)組形式搜索
* @param [type] $arr [description]
* @param string $sep [description]
* @return [type] [description]
*/
function getByKey ($key, $limit = 10, $page =1,$fuzzy = true, $sep = 'OR') {
$page = max(1, $page);
$this->search = $this->search->setSort("time", false);
$this->search->setLimit($limit, ($page-1) * $limit);
$this->search->setFuzzy($fuzzy);
if (is_array($key)) {
$key = implode($sep, $key);
}
return $this->search->search($key);
}
/**
* 獲取熱搜排序類型, 默認(rèn)為 total(搜索總量), 可選值還有 lastnum(上周), currnum(本周)
* @param integer $limit [description]
* @param string $type [description]
* @return [type] [description]
*/
function getHotArc($type = "total", $arclimit = 10, $keylimit = 6) {
$words = $this->search->getHotQuery($keylimit, $type);
$words = array_keys($words);
shuffle($words);
return $this->getByKey($words, $arclimit);
}
/**
* 提取重要詞,并返回由重要詞組成的數(shù)組
* @param [type] $text [description]
* @param integer $limit [description]
* @param string $attr [description]
*/
function scwsByText($text, $limit = 4, $attr = 'n') {
$this->scws->setIgnore(true);
$this->scws->setDuality(true)->setMulti(3);
$results = $this->scws->getTops($text, $limit, $attr);
$tmp = [];
foreach ($results as $key => $value) {
array_push($tmp, $value['word']);
}
return $tmp;
}
function getScwsAll ($str, $attr = []) {
$source=["nr"=>4,"n"=>3,"v"=>2];
$re = [];
$this->scws->setIgnore(true);
$this->scws->setDuality(true);
$results = $this->scws->getResult($str);
$kwd=[];
foreach ($results as $k => $v) {
$kwd[$v['word']]=strlen($v['word'])*(isset($source[$v['attr']])?$source[$v['attr']]:1);
}
arsort($kwd);
return array_slice($kwd, 0,3);
}
}