封裝xunsearch類
封閉xunsearch方便其進行操作
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();
}
/**
* 以陣列形式搜尋
* @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);
}
/**
* 獲取熱搜排序型別, 預設為 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);
}
/**
* 提取重要詞,並返回由重要片語成的陣列
* @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);
}
}
相關文章
- 封裝Redis工具類封裝Redis
- 封裝Date工具類封裝
- 一、類的封裝性封裝
- 4、類和物件—封裝物件封裝
- 自用驗證類封裝封裝
- c#封裝DBHelper類C#封裝
- 十五、類與封裝的概念封裝
- Android之Activity基類封裝Android封裝
- c# Lambda操作類封裝C#封裝
- 靜態庫封裝之ComDir類封裝
- 靜態庫封裝之ComFile類封裝
- 靜態庫封裝之ComStr類封裝
- Laravel xunsearch 全文搜尋Laravel
- 前端自定義類事件回撥封裝前端事件封裝
- JavaFx 生成二維碼工具類封裝Java封裝
- node.js 封裝操作檔案類Node.js封裝
- 基於javascript的拖拽類封裝^o^JavaScript封裝
- Android常用工具類的封裝Android封裝
- php封裝db 類連線sqlite3PHP封裝SQLite
- 5.Hibernate工具類的簡易封裝封裝
- python 打飛機專案 ( 基類封裝 )Python封裝
- iOS微信支付接入以及工具類封裝iOS封裝
- 【JavaScript框架封裝】實現一個類似於JQuery的動畫框架的封裝JavaScript框架封裝jQuery動畫
- 【JavaScript框架封裝】實現一個類似於JQuery的CSS樣式框架的封裝JavaScript框架封裝jQueryCSS
- 【封裝那些事】 缺失封裝封裝
- C/C++ Zlib庫封裝MyZip壓縮類C++封裝
- jwt是什麼?php jwt類封裝和使用JWTPHP封裝
- 使用C++/CLI呼叫C#封裝類庫C++C#封裝
- 一次Android動畫工具類的封裝Android動畫封裝
- Java類的設計和封裝及類成員的訪問控制Java封裝
- 封裝封裝
- 封裝avalonia指定元件允許拖動的工具類封裝元件
- web自動化框架—BasePage 類的簡單封裝Web框架封裝
- (3)Tcp Socket程式設計的封裝類 TcpListener/TcpClientTCP程式設計封裝client
- Retrofit+okhttp+Rxjava封裝網路請求工具類HTTPRxJava封裝
- RN自定義元件封裝 - 播放類似PPT動畫元件封裝動畫
- 【封裝小技巧】is 系列方法的封裝封裝
- 【JavaScript框架封裝】公共框架的封裝JavaScript框架封裝
- Dapper的封裝、二次封裝、官方擴充套件包封裝,以及ADO.NET原生封裝APP封裝套件