[Xunsearch PHP-SDK](http://www.xunsearch.com) v1.4.8 API 參考文檔
# XSSearch
[All Packages](#)| [屬性](#)| [方法(函數)](#)
| 包 | [XS](#) |
|-----|-----|
| 繼承關系 | class XSSearch ?[XSServer](#) ?[XSComponent](#) |
| 版本 | 1.0.0 |
| 源代碼 | [sdk/php/lib/XSSearch.class.php](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSSearch.class.php) |
XS 搜索類, 執行搜索功能有部分方法支持串接操作
~~~
$xs->search->setQuery($str)->setLimit(10, 10)->search();
$xs->close();
~~~
### Public 屬性
[隱去繼承來的屬性](#)
| 名稱 | 類型 | 描述 | 定義于 |
|-----|-----|-----|-----|
| [allSynonyms](#) | array | 獲取當前庫內的全部同義詞列表 | XSSearch |
| [connString](#) | string | 連接字符串 | [XSServer](#) |
| [correctedQuery](#) | array | 獲取修正后的搜索詞列表 | XSSearch |
| [dbTotal](#) | int | 獲取搜索數據庫內的數據總量 | XSSearch |
| [facets](#) | array | 讀取最近一次分面搜索記數 | XSSearch |
| [hotQuery](#) | array | 獲取熱門搜索詞列表 | XSSearch |
| [lastCount](#) | int | 獲取最近那次搜索的匹配總數估值 | XSSearch |
| [project](#) | string | 獲取當前項目名稱 | [XSServer](#) |
| [query](#) | string | 獲取解析后的搜索語句 | XSSearch |
| [relatedQuery](#) | array | 獲取相關搜索詞列表 | XSSearch |
| [respond](#) | [XSCommand](#) | 從服務器讀取響應指令 | [XSServer](#) |
| [socket](#) | mixed | 獲取連接資源描述符 | [XSServer](#) |
| [xs](#) | [XS](#) | 服務端關聯的 XS 對象 | [XSServer](#) |
### Protected 屬性
[隱去繼承來的屬性](#)
| 名稱 | 類型 | 描述 | 定義于 |
|-----|-----|-----|-----|
| [_conn](#) | | | [XSServer](#) |
| [_flag](#) | | | [XSServer](#) |
| [_project](#) | | | [XSServer](#) |
| [_sendBuffer](#) | | | [XSServer](#) |
| [_sock](#) | | | [XSServer](#) |
### Public 方法
[隱去繼承來的方法](#)
| 名稱 | 描述 | 定義于 |
|-----|-----|-----|
| [__construct()](#) | 構造函數, 打開連接 | [XSServer](#) |
| [__destruct()](#) | 析構函數, 關閉連接 | [XSServer](#) |
| [__get()](#) | 魔術方法 __get | [XSComponent](#) |
| [__isset()](#) | 魔術方法 __isset | [XSComponent](#) |
| [__set()](#) | 魔術方法 __set | [XSComponent](#) |
| [__unset()](#) | 魔術方法 __unset | [XSComponent](#) |
| [addDb()](#) | 添加搜索的數據庫名, 支持多庫同時搜索 | XSSearch |
| [addQueryString()](#) | 增加默認搜索語句 | XSSearch |
| [addQueryTerm()](#) | 增加默認搜索詞匯 | XSSearch |
| [addRange()](#) | 添加搜索過濾區間或范圍 | XSSearch |
| [addSearchLog()](#) | 添加搜索日志關鍵詞到緩沖區里 | XSSearch |
| [addWeight()](#) | 添加權重索引詞 | XSSearch |
| [close()](#) | 關閉連接 | [XSServer](#) |
| [count()](#) | 估算搜索語句的匹配數據量 | XSSearch |
| [execCommand()](#) | 執行服務端指令并獲取返回值 | [XSServer](#) |
| [getAllSynonyms()](#) | 獲取當前庫內的全部同義詞列表 | XSSearch |
| [getConnString()](#) | Returns 連接字符串 | [XSServer](#) |
| [getCorrectedQuery()](#) | 獲取修正后的搜索詞列表 | XSSearch |
| [getDbTotal()](#) | 獲取搜索數據庫內的數據總量 | XSSearch |
| [getExpandedQuery()](#) | 獲取展開的搜索詞列表 | XSSearch |
| [getFacets()](#) | 讀取最近一次分面搜索記數 | XSSearch |
| [getHotQuery()](#) | 獲取熱門搜索詞列表 | XSSearch |
| [getLastCount()](#) | 獲取最近那次搜索的匹配總數估值 | XSSearch |
| [getProject()](#) | 獲取當前項目名稱 | [XSServer](#) |
| [getQuery()](#) | 獲取解析后的搜索語句 | XSSearch |
| [getRelatedQuery()](#) | 獲取相關搜索詞列表 | XSSearch |
| [getRespond()](#) | 從服務器讀取響應指令 | [XSServer](#) |
| [getSocket()](#) | 獲取連接資源描述符 | [XSServer](#) |
| [hasRespond()](#) | 判斷服務端是否有可讀數據 | [XSServer](#) |
| [highlight()](#) | 搜索結果字符串高亮處理 | XSSearch |
| [markResetScheme()](#) | 標記字段方案重置 | XSSearch |
| [open()](#) | 連接搜索服務端并初始化 | XSSearch |
| [reopen()](#) | 重新打開連接 | [XSServer](#) |
| [search()](#) | 獲取匹配的搜索結果文檔 | XSSearch |
| [sendCommand()](#) | 往服務器直接發送指令 (無緩存) | [XSServer](#) |
| [setAutoSynonyms()](#) | 開啟自動同義詞搜索功能 | XSSearch |
| [setCharset()](#) | 設置字符集 | XSSearch |
| [setCollapse()](#) | 設置折疊搜索結果 | XSSearch |
| [setCutOff()](#) | 設置百分比/權重剔除參數 | XSSearch |
| [setDb()](#) | 設置要搜索的數據庫名 | XSSearch |
| [setDocOrder()](#) | 設置結果按索引入庫先后排序 | XSSearch |
| [setFacets()](#) | 設置分面搜索記數 | XSSearch |
| [setFuzzy()](#) | 開啟模糊搜索 | XSSearch |
| [setLimit()](#) | 設置搜索結果的數量和偏移 | XSSearch |
| [setMultiSort()](#) | 設置多字段組合排序方式 | XSSearch |
| [setProject()](#) | 設置當前項目 | [XSServer](#) |
| [setQuery()](#) | 設置默認搜索語句 | XSSearch |
| [setRequireMatchedTerm()](#) | 設置在搜索結果文檔中返回匹配詞表 | XSSearch |
| [setScwsMulti()](#) | 設置當前搜索語句的分詞復合等級 | XSSearch |
| [setSort()](#) | 設置搜索結果的排序方式 | XSSearch |
| [setSynonymScale()](#) | 設置同義詞搜索的權重比例 | XSSearch |
| [setTimeout()](#) | 設置服務端超時秒數 | [XSServer](#) |
| [terms()](#) | 獲取搜索語句中的高亮詞條列表 | XSSearch |
### Protected 方法
[隱去繼承來的方法](#)
| 名稱 | 描述 | 定義于 |
|-----|-----|-----|
| [check()](#) | 檢測服務端的連接情況 | [XSServer](#) |
| [connect()](#) | 連接服務端 | [XSServer](#) |
| [read()](#) | 讀取數據 | [XSServer](#) |
| [write()](#) | 寫入數據 | [XSServer](#) |
### 屬性明細
allSynonyms屬性 *只讀* (自版本 v1.3.0 起可用)
public array [**getAllSynonyms**](#)(int $limit=0, int $offset=0, bool $stemmed=false)
獲取當前庫內的全部同義詞列表
correctedQuery屬性 *只讀*
public array [**getCorrectedQuery**](#)(string $query=NULL)
獲取修正后的搜索詞列表通常當某次檢索結果數量偏少時, 可以用該函數設計 "你是不是要找: ..." 功能
dbTotal屬性 *只讀*
public int [**getDbTotal**](#)()
獲取搜索數據庫內的數據總量
facets屬性 (自版本 v1.1.0 起可用)
public array [**getFacets**](#)(string $field=NULL)
public XSSearch [**setFacets**](#)(mixed $field, bool $exact=false)
讀取最近一次分面搜索記數必須在某一次 [search](#) 之后調用本函數才有意義
hotQuery屬性 *只讀*
public array [**getHotQuery**](#)(int $limit=6, string $type='total')
獲取熱門搜索詞列表
lastCount屬性 *只讀*
public int [**getLastCount**](#)()
獲取最近那次搜索的匹配總數估值
#### 參見
- [search](#)
query屬性
public string [**getQuery**](#)(string $query=NULL)
public XSSearch [**setQuery**](#)(string $query)
獲取解析后的搜索語句
relatedQuery屬性 *只讀*
public array [**getRelatedQuery**](#)(string $query=NULL, int $limit=6)
獲取相關搜索詞列表
### 方法明細
addDb()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public XSSearch <b>addDb</b>(string $name)</div></td></tr><tr><td class="paramNameCol">$name</td> <td class="paramTypeCol">string</td> <td class="paramDescCol"></td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">XSSearch</td> <td class="paramDescCol">返回對象本身以支持串接操作</td></tr></table>
**源碼:**[sdk/php/lib/XSSearch.class.php#L474](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSSearch.class.php#L474) (**[顯示](#)**)
`public?function?addDb($name)
{
????$name?=?strval($name);
????$this->execCommand(array('cmd'?=>?CMD_SEARCH_ADD_DB,?'buf'?=>?$name));
????$this->_curDbs[]?=?$name;
????return?$this;
}`
添加搜索的數據庫名, 支持多庫同時搜索
#### 參見
- [setDb](#)
addQueryString()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public string <b>addQueryString</b>(string $query, int $addOp=0, float $scale=1)</div></td></tr><tr><td class="paramNameCol">$query</td> <td class="paramTypeCol">string</td> <td class="paramDescCol">搜索語句</td></tr><tr><td class="paramNameCol">$addOp</td> <td class="paramTypeCol">int</td> <td class="paramDescCol">與舊語句的結合操作符, 如果無舊語句或為空則這此無意義, 支持的操作符有: CMD_QUERY_OP_AND CMD_QUERY_OP_OR CMD_QUERY_OP_AND_NOT CMD_QUERY_OP_XOR CMD_QUERY_OP_AND_MAYBE CMD_QUERY_OP_FILTER</td></tr><tr><td class="paramNameCol">$scale</td> <td class="paramTypeCol">float</td> <td class="paramDescCol">權重計算縮放比例, 默認為 1表示不縮放, 其它值范圍 0.xx ~ 655.35</td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">string</td> <td class="paramDescCol">修正后的搜索語句</td></tr></table>
**源碼:**[sdk/php/lib/XSSearch.class.php#L931](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSSearch.class.php#L931) (**[顯示](#)**)
`public?function?addQueryString($query,?$addOp?=?CMD_QUERY_OP_AND,?$scale?=?1)
{
????$query?=?$this->preQueryString($query);
????$bscale?=?($scale?>?0?&&?$scale?!=?1)???pack('n',?intval($scale?*?100))?:?'';
????$cmd?=?new?XSCommand(CMD_QUERY_PARSE,?$addOp,?$this->_defaultOp,?$query,?$bscale);
????$this->execCommand($cmd);
????return?$query;
}`
增加默認搜索語句
addQueryTerm()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public XSSearch <b>addQueryTerm</b>(string $field, string $term, int $addOp=0, float $scale=1)</div></td></tr><tr><td class="paramNameCol">$field</td> <td class="paramTypeCol">string</td> <td class="paramDescCol">索引詞所屬的字段, 若為混合區詞匯可設為 null 或 body 型的字段名</td></tr><tr><td class="paramNameCol">$term</td> <td class="paramTypeCol">string</td> <td class="paramDescCol">索引詞 (強制轉為小寫)</td></tr><tr><td class="paramNameCol">$addOp</td> <td class="paramTypeCol">int</td> <td class="paramDescCol">與舊語句的結合操作符, 如果無舊語句或為空則這此無意義, 支持的操作符有:</td></tr><tr><td class="paramNameCol">$scale</td> <td class="paramTypeCol">float</td> <td class="paramDescCol">權重計算縮放比例, 默認為 1表示不縮放, 其它值范圍 0.xx ~ 655.35</td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">XSSearch</td> <td class="paramDescCol">返回對象本身以支持串接操作</td></tr></table>
**源碼:**[sdk/php/lib/XSSearch.class.php#L950](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSSearch.class.php#L950) (**[顯示](#)**)
`public?function?addQueryTerm($field,?$term,?$addOp?=?CMD_QUERY_OP_AND,?$scale?=?1)
{
????$term?=?strtolower($term);
????$term?=?XS::convert($term,?'UTF-8',?$this->_charset);
????$bscale?=?($scale?>?0?&&?$scale?!=?1)???pack('n',?intval($scale?*?100))?:?'';
????$vno?=?$field?===?null???XSFieldScheme::MIXED_VNO?:?$this->xs->getField($field,?true)->vno;
????$cmd?=?new?XSCommand(CMD_QUERY_TERM,?$addOp,?$vno,?$term,?$bscale);
????$this->execCommand($cmd);
????return?$this;
}`
增加默認搜索詞匯
#### 參見
- [addQueryString](#)
addRange()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public XSSearch <b>addRange</b>(string $field, mixed $from, mixed $to)</div></td></tr><tr><td class="paramNameCol">$field</td> <td class="paramTypeCol">string</td> <td class="paramDescCol"></td></tr><tr><td class="paramNameCol">$from</td> <td class="paramTypeCol">mixed</td> <td class="paramDescCol">起始值(不包含), 若設為 null 則相當于匹配 <= to (字典順序)</td></tr><tr><td class="paramNameCol">$to</td> <td class="paramTypeCol">mixed</td> <td class="paramDescCol">結束值(包含), 若設為 null 則相當于匹配 >= from (字典順序)</td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">XSSearch</td> <td class="paramDescCol">返回對象本身以支持串接操作</td></tr></table>
**源碼:**[sdk/php/lib/XSSearch.class.php#L331](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSSearch.class.php#L331) (**[顯示](#)**)
`public?function?addRange($field,?$from,?$to)
{
????if?($from?===?''?||?$from?===?false)?{
????????$from?=?null;
????}
????if?($to?===?''?||?$to?===?false)?{
????????$to?=?null;
????}
????if?($from?!==?null?||?$to?!==?null)?{
????????if?(strlen($from)?>?255?||?strlen($to)?>?255)?{
????????????throw?new?XSException('Value?of?range?is?too?long');
????????}
????????$vno?=?$this->xs->getField($field)->vno;
????????$from?=?XS::convert($from,?'UTF-8',?$this->_charset);
????????$to?=?XS::convert($to,?'UTF-8',?$this->_charset);
????????if?($from?===?null)?{
????????????$cmd?=?new?XSCommand(CMD_QUERY_VALCMP,?CMD_QUERY_OP_FILTER,?$vno,?$to,?chr(CMD_VALCMP_LE));
????????}?elseif?($to?===?null)?{
????????????$cmd?=?new?XSCommand(CMD_QUERY_VALCMP,?CMD_QUERY_OP_FILTER,?$vno,?$from,?chr(CMD_VALCMP_GE));
????????}?else?{
????????????$cmd?=?new?XSCommand(CMD_QUERY_RANGE,?CMD_QUERY_OP_FILTER,?$vno,?$from,?$to);
????????}
????????$this->execCommand($cmd);
????}
????return?$this;
}`
添加搜索過濾區間或范圍
addSearchLog()方法(自版本 v1.1.1 起可用)
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public void <b>addSearchLog</b>(string $query, int $wdf=1)</div></td></tr><tr><td class="paramNameCol">$query</td> <td class="paramTypeCol">string</td> <td class="paramDescCol">需要記錄的數據</td></tr><tr><td class="paramNameCol">$wdf</td> <td class="paramTypeCol">int</td> <td class="paramDescCol">需要記錄的次數, 默認為 1</td></tr></table>
**源碼:**[sdk/php/lib/XSSearch.class.php#L813](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSSearch.class.php#L813) (**[顯示](#)**)
`public?function?addSearchLog($query,?$wdf?=?1)
{
????$cmd?=?array('cmd'?=>?CMD_SEARCH_ADD_LOG,?'buf'?=>?$query);
????if?($wdf?>?1)?{
????????$cmd['buf1']?=?pack('i',?$wdf);
????}
????$this->execCommand($cmd,?CMD_OK_LOGGED);
}`
添加搜索日志關鍵詞到緩沖區里需要調用 [XSIndex::flushLogging](#) 才能確保立即刷新, 否則要隔一段時間
addWeight()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public XSSearch <b>addWeight</b>(string $field, string $term, float $weight=1)</div></td></tr><tr><td class="paramNameCol">$field</td> <td class="paramTypeCol">string</td> <td class="paramDescCol">索引詞所屬的字段</td></tr><tr><td class="paramNameCol">$term</td> <td class="paramTypeCol">string</td> <td class="paramDescCol">索引詞</td></tr><tr><td class="paramNameCol">$weight</td> <td class="paramTypeCol">float</td> <td class="paramDescCol">權重計算縮放比例</td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">XSSearch</td> <td class="paramDescCol">返回對象本身以支持串接操作</td></tr></table>
**源碼:**[sdk/php/lib/XSSearch.class.php#L368](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSSearch.class.php#L368) (**[顯示](#)**)
`public?function?addWeight($field,?$term,?$weight?=?1)
{
????return?$this->addQueryTerm($field,?$term,?CMD_QUERY_OP_AND_MAYBE,?$weight);
}`
添加權重索引詞無論是否包含這種詞都不影響搜索匹配, 但會參與計算結果權重, 使結果的相關度更高
#### 參見
- [addQueryTerm](#)
count()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public int <b>count</b>(string $query=NULL)</div></td></tr><tr><td class="paramNameCol">$query</td> <td class="paramTypeCol">string</td> <td class="paramDescCol">搜索語句, 若傳入 null 使用默認語句, 調用后會還原默認排序方式 如果搜索語句和最近一次 <a href="XSSearch.html#search">search</a> 的語句一樣, 請改用 <a href="XSSearch.html#getLastCount">getLastCount</a> 以提升效率 最大長度為 80 字節</td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">int</td> <td class="paramDescCol">匹配的搜索結果數量, 估算數值</td></tr></table>
**源碼:**[sdk/php/lib/XSSearch.class.php#L529](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSSearch.class.php#L529) (**[顯示](#)**)
`public?function?count($query?=?null)
{
????$query?=?$query?===?null???''?:?$this->preQueryString($query);
????if?($query?===?''?&&?$this->_count?!==?null)?{
????????return?$this->_count;
????}
????$cmd?=?new?XSCommand(CMD_SEARCH_GET_TOTAL,?0,?$this->_defaultOp,?$query);
????$res?=?$this->execCommand($cmd,?CMD_OK_SEARCH_TOTAL);
????$ret?=?unpack('Icount',?$res->buf);
????if?($query?===?'')?{
????????$this->_count?=?$ret['count'];
????}
????return?$ret['count'];
}`
估算搜索語句的匹配數據量
getAllSynonyms()方法(自版本 v1.3.0 起可用)
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public array <b>getAllSynonyms</b>(int $limit=0, int $offset=0, bool $stemmed=false)</div></td></tr><tr><td class="paramNameCol">$limit</td> <td class="paramTypeCol">int</td> <td class="paramDescCol">數量上限, 若設為 0 則啟用默認值 100 個</td></tr><tr><td class="paramNameCol">$offset</td> <td class="paramTypeCol">int</td> <td class="paramDescCol">偏移量, 即跳過的結果數量, 默認為 0</td></tr><tr><td class="paramNameCol">$stemmed</td> <td class="paramTypeCol">bool</td> <td class="paramDescCol">是否包含處理過的詞根同義詞, 默認為 false 表示否</td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">array</td> <td class="paramDescCol">同義詞記錄數組, 每個詞條為鍵, 同義詞條組成的數組為值</td></tr></table>
**源碼:**[sdk/php/lib/XSSearch.class.php#L164](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSSearch.class.php#L164) (**[顯示](#)**)
`public?function?getAllSynonyms($limit?=?0,?$offset?=?0,?$stemmed?=?false)
{
????$page?=?$limit?>?0???pack('II',?intval($offset),?intval($limit))?:?'';
????$cmd?=?array('cmd'?=>?CMD_SEARCH_GET_SYNONYMS,?'buf1'?=>?$page);
????$cmd['arg1']?=?$stemmed?==?true???1?:?0;
????$res?=?$this->execCommand($cmd,?CMD_OK_RESULT_SYNONYMS);
????$ret?=?array();
????if?(!empty($res->buf))?{
????????foreach?(explode("\n",?$res->buf)?as?$line)?{
????????????$value?=?explode("\t",?$line);
????????????$key?=?array_shift($value);
????????????$ret[$key]?=?$value;
????????}
????}
????return?$ret;
}`
獲取當前庫內的全部同義詞列表
getCorrectedQuery()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public array <b>getCorrectedQuery</b>(string $query=NULL)</div></td></tr><tr><td class="paramNameCol">$query</td> <td class="paramTypeCol">string</td> <td class="paramDescCol">需要展開的前綴, 可為拼音、英文、中文</td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">array</td> <td class="paramDescCol">返回搜索詞組成的數組</td></tr></table>
**源碼:**[sdk/php/lib/XSSearch.class.php#L777](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSSearch.class.php#L777) (**[顯示](#)**)
`public?function?getCorrectedQuery($query?=?null)
{
????$ret?=?array();
????try?{
????????if?($query?===?null)?{
????????????if?($this->_count?>?0?&&?$this->_count?>?ceil($this->getDbTotal()?*?0.001))?{
????????????????return?$ret;
????????????}
????????????$query?=?$this->cleanFieldQuery($this->_query);
????????}
????????if?(empty($query)?||?strpos($query,?':')?!==?false)?{
????????????return?$ret;
????????}
????????$buf?=?XS::convert($query,?'UTF-8',?$this->_charset);
????????$cmd?=?array('cmd'?=>?CMD_QUERY_GET_CORRECTED,?'buf'?=>?$buf);
????????$res?=?$this->execCommand($cmd,?CMD_OK_QUERY_CORRECTED);
????????if?($res->buf?!==?'')?{
????????????$ret?=?explode("\n",?XS::convert($res->buf,?$this->_charset,?'UTF-8'));
????????}
????}?catch?(XSException?$e)?{
????????if?($e->getCode()?!=?CMD_ERR_XAPIAN)?{
????????????throw?$e;
????????}
????}
????return?$ret;
}`
獲取修正后的搜索詞列表通常當某次檢索結果數量偏少時, 可以用該函數設計 "你是不是要找: ..." 功能
getDbTotal()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public int <b>getDbTotal</b>()</div></td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">int</td> <td class="paramDescCol">數據總量</td></tr></table>
**源碼:**[sdk/php/lib/XSSearch.class.php#L641](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSSearch.class.php#L641) (**[顯示](#)**)
`public?function?getDbTotal()
{
????$cmd?=?new?XSCommand(CMD_SEARCH_DB_TOTAL);
????$res?=?$this->execCommand($cmd,?CMD_OK_DB_TOTAL);
????$tmp?=?unpack('Itotal',?$res->buf);
????return?$tmp['total'];
}`
獲取搜索數據庫內的數據總量
getExpandedQuery()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public array <b>getExpandedQuery</b>(string $query, int $limit=10)</div></td></tr><tr><td class="paramNameCol">$query</td> <td class="paramTypeCol">string</td> <td class="paramDescCol">需要展開的前綴, 可為拼音、英文、中文</td></tr><tr><td class="paramNameCol">$limit</td> <td class="paramTypeCol">int</td> <td class="paramDescCol">需要返回的搜索詞數量上限, 默認為 10, 最大值為 20</td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">array</td> <td class="paramDescCol">返回搜索詞組成的數組</td></tr></table>
**源碼:**[sdk/php/lib/XSSearch.class.php#L737](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSSearch.class.php#L737) (**[顯示](#)**)
`public?function?getExpandedQuery($query,?$limit?=?10)
{
????$ret?=?array();
????$limit?=?max(1,?min(20,?intval($limit)));
????try?{
????????$buf?=?XS::convert($query,?'UTF-8',?$this->_charset);
????????$cmd?=?array('cmd'?=>?CMD_QUERY_GET_EXPANDED,?'arg1'?=>?$limit,?'buf'?=>?$buf);
????????$res?=?$this->execCommand($cmd,?CMD_OK_RESULT_BEGIN);
????????//?echo?"Raw?Query:?"?.?$res->buf?.?"\n";
????????//?get?result?documents
????????while?(true)?{
????????????$res?=?$this->getRespond();
????????????if?($res->cmd?==?CMD_SEARCH_RESULT_FIELD)?{
????????????????$ret[]?=?XS::convert($res->buf,?$this->_charset,?'UTF-8');
????????????}?elseif?($res->cmd?==?CMD_OK?&&?$res->arg?==?CMD_OK_RESULT_END)?{
????????????????//?got?the?end
????????????????//?echo?"Parsed?Query:?"?.?$res->buf?.?"\n";
????????????????break;
????????????}?else?{
????????????????$msg?=?'Unexpected?respond?in?search?{CMD:'?.?$res->cmd?.?',?ARG:'?.?$res->arg?.?'}';
????????????????throw?new?XSException($msg);
????????????}
????????}
????}?catch?(XSException?$e)?{
????????if?($e->getCode()?!=?CMD_ERR_XAPIAN)?{
????????????throw?$e;
????????}
????}
????return?$ret;
}`
獲取展開的搜索詞列表
getFacets()方法(自版本 v1.1.0 起可用)
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public array <b>getFacets</b>(string $field=NULL)</div></td></tr><tr><td class="paramNameCol">$field</td> <td class="paramTypeCol">string</td> <td class="paramDescCol">讀取分面記數的字段, 若為 null 則返回全部分面搜索記錄</td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">array</td> <td class="paramDescCol">返回由值和計數組成的關聯數組, 若不存在或未曾登記過則返回空數組</td></tr></table>
**源碼:**[sdk/php/lib/XSSearch.class.php#L409](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSSearch.class.php#L409) (**[顯示](#)**)
`public?function?getFacets($field?=?null)
{
????if?($field?===?null)?{
????????return?$this->_facets;
????}
????return?isset($this->_facets[$field])???$this->_facets[$field]?:?array();
}`
讀取最近一次分面搜索記數必須在某一次 [search](#) 之后調用本函數才有意義
getHotQuery()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public array <b>getHotQuery</b>(int $limit=6, string $type='total')</div></td></tr><tr><td class="paramNameCol">$limit</td> <td class="paramTypeCol">int</td> <td class="paramDescCol">需要返回的熱門搜索數量上限, 默認為 6, 最大值為 50</td></tr><tr><td class="paramNameCol">$type</td> <td class="paramTypeCol">string</td> <td class="paramDescCol">排序類型, 默認為 total(搜索總量), 可選值還有 lastnum(上周), currnum(本周)</td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">array</td> <td class="paramDescCol">返回以搜索詞為鍵, 搜索指數為值的關聯數組</td></tr></table>
**源碼:**[sdk/php/lib/XSSearch.class.php#L655](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSSearch.class.php#L655) (**[顯示](#)**)
`public?function?getHotQuery($limit?=?6,?$type?=?'total')
{
????$ret?=?array();
????$limit?=?max(1,?min(50,?intval($limit)));
????//?query?from?log_db
????$this->xs->setScheme(XSFieldScheme::logger());
????try?{
????????$this->setDb(self::LOG_DB)->setLimit($limit);
????????if?($type?!==?'lastnum'?&&?$type?!==?'currnum')?{
????????????$type?=?'total';
????????}
????????$result?=?$this->search($type?.?':1');
????????foreach?($result?as?$doc)?/*?@var?$doc?XSDocument?*/?{
????????????$body?=?$doc->body;
????????????$ret[$body]?=?$doc->f($type);
????????}
????????$this->restoreDb();
????}?catch?(XSException?$e)?{
????????if?($e->getCode()?!=?CMD_ERR_XAPIAN)?{
????????????throw?$e;
????????}
????}
????$this->xs->restoreScheme();
????return?$ret;
}`
獲取熱門搜索詞列表
getLastCount()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public int <b>getLastCount</b>()</div></td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">int</td> <td class="paramDescCol">匹配數據量, 如從未搜索則返回 false</td></tr></table>
**源碼:**[sdk/php/lib/XSSearch.class.php#L632](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSSearch.class.php#L632) (**[顯示](#)**)
`public?function?getLastCount()
{
????return?$this->_lastCount;
}`
獲取最近那次搜索的匹配總數估值
#### 參見
- [search](#)
getQuery()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public string <b>getQuery</b>(string $query=NULL)</div></td></tr><tr><td class="paramNameCol">$query</td> <td class="paramTypeCol">string</td> <td class="paramDescCol">搜索語句, 若傳入 null 使用默認語句</td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">string</td> <td class="paramDescCol">返回解析后的搜索語句</td></tr></table>
**源碼:**[sdk/php/lib/XSSearch.class.php#L186](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSSearch.class.php#L186) (**[顯示](#)**)
`public?function?getQuery($query?=?null)
{
????$query?=?$query?===?null???''?:?$this->preQueryString($query);
????$cmd?=?new?XSCommand(CMD_QUERY_GET_STRING,?0,?$this->_defaultOp,?$query);
????$res?=?$this->execCommand($cmd,?CMD_OK_QUERY_STRING);
????if?(strpos($res->buf,?'VALUE_RANGE')?!==?false)?{
????????$regex?=?'/(VALUE_RANGE)?(\d+)?(\S+)?(\S+?)(?=\))/';
????????$res->buf?=?preg_replace_callback($regex,?array($this,?'formatValueRange'),?$res->buf);
????}
????if?(strpos($res->buf,?'VALUE_GE')?!==?false?||?strpos($res->buf,?'VALUE_LE')?!==?false)?{
????????$regex?=?'/(VALUE_[GL]E)?(\d+)?(\S+?)(?=\))/';
????????$res->buf?=?preg_replace_callback($regex,?array($this,?'formatValueRange'),?$res->buf);
????}
????return?XS::convert($res->buf,?$this->_charset,?'UTF-8');
}`
獲取解析后的搜索語句
getRelatedQuery()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public array <b>getRelatedQuery</b>(string $query=NULL, int $limit=6)</div></td></tr><tr><td class="paramNameCol">$query</td> <td class="paramTypeCol">string</td> <td class="paramDescCol">搜索語句, 若傳入 null 使用默認語句</td></tr><tr><td class="paramNameCol">$limit</td> <td class="paramTypeCol">int</td> <td class="paramDescCol">需要返回的相關搜索數量上限, 默認為 6, 最大值為 20</td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">array</td> <td class="paramDescCol">返回搜索詞組成的數組</td></tr></table>
**源碼:**[sdk/php/lib/XSSearch.class.php#L689](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSSearch.class.php#L689) (**[顯示](#)**)
`public?function?getRelatedQuery($query?=?null,?$limit?=?6)
{
????$ret?=?array();
????$limit?=?max(1,?min(20,?intval($limit)));
????//?Simple?to?disable?query?with?field?filter
????if?($query?===?null)?{
????????$query?=?$this->cleanFieldQuery($this->_query);
????}
????if?(empty($query)?||?strpos($query,?':')?!==?false)?{
????????return?$ret;
????}
????//?Search?the?log?database
????$op?=?$this->_defaultOp;
????$this->xs->setScheme(XSFieldScheme::logger());
????try?{
????????$result?=?$this->setDb(self::LOG_DB)->setFuzzy()->setLimit($limit?+?1)->search($query);
????????foreach?($result?as?$doc)?/*?@var?$doc?XSDocument?*/?{
????????????$doc->setCharset($this->_charset);
????????????$body?=?$doc->body;
????????????if?(!strcasecmp($body,?$query))?{
????????????????continue;
????????????}
????????????$ret[]?=?$body;
????????????if?(count($ret)?==?$limit)?{
????????????????break;
????????????}
????????}
????}?catch?(XSException?$e)?{
????????if?($e->getCode()?!=?CMD_ERR_XAPIAN)?{
????????????throw?$e;
????????}
????}
????$this->restoreDb();
????$this->xs->restoreScheme();
????$this->_defaultOp?=?$op;
????return?$ret;
}`
獲取相關搜索詞列表
highlight()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public string <b>highlight</b>(string $value, $strtr=false)</div></td></tr><tr><td class="paramNameCol">$value</td> <td class="paramTypeCol">string</td> <td class="paramDescCol">需要處理的數據</td></tr><tr><td class="paramNameCol">$strtr</td> <td class="paramTypeCol"></td> <td class="paramDescCol"></td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">string</td> <td class="paramDescCol">高亮后的數據</td></tr></table>
**源碼:**[sdk/php/lib/XSSearch.class.php#L828](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSSearch.class.php#L828) (**[顯示](#)**)
`public?function?highlight($value,?$strtr?=?false)
{
????//?return?empty?value?directly
????if?(empty($value))?{
????????return?$value;
????}
????//?initlize?the?highlight?replacements
????if?(!is_array($this->_highlight))?{
????????$this->initHighlight();
????}
????//?process?replace
????if?(isset($this->_highlight['pattern']))?{
????????$value?=?preg_replace($this->_highlight['pattern'],?$this->_highlight['replace'],?$value);
????}
????if?(isset($this->_highlight['pairs']))?{
????????$value?=?$strtr??
????????????????strtr($value,?$this->_highlight['pairs'])?:
????????????????str_replace(array_keys($this->_highlight['pairs']),?array_values($this->_highlight['pairs']),?$value);
????}
????return?$value;
}`
搜索結果字符串高亮處理對搜索結果文檔的字段進行高亮、飄紅處理, 高亮部分加上 em 標記
markResetScheme()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public void <b>markResetScheme</b>()</div></td></tr></table>
**源碼:**[sdk/php/lib/XSSearch.class.php#L486](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSSearch.class.php#L486) (**[顯示](#)**)
`public?function?markResetScheme()
{
????$this->_resetScheme?=?true;
}`
標記字段方案重置
#### 參見
- [XS::setScheme](#)
open()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public void <b>open</b>(string $conn)</div></td></tr><tr><td class="paramNameCol">$conn</td> <td class="paramTypeCol">string</td> <td class="paramDescCol"></td></tr></table>
**源碼:**[sdk/php/lib/XSSearch.class.php#L55](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSSearch.class.php#L55) (**[顯示](#)**)
`public?function?open($conn)
{
????parent::open($conn);
????$this->_prefix?=?array();
????$this->_fieldSet?=?false;
????$this->_lastCount?=?false;
}`
連接搜索服務端并初始化每次重新連接后所有的搜索語句相關設置均被還原
#### 參見
- [XSServer::open](#)
search()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public XSDocument[] <b>search</b>(string $query=NULL, boolean $saveHighlight=true)</div></td></tr><tr><td class="paramNameCol">$query</td> <td class="paramTypeCol">string</td> <td class="paramDescCol">搜索語句, 若傳入 null 使用默認語句, 最大長度為 80 字節</td></tr><tr><td class="paramNameCol">$saveHighlight</td> <td class="paramTypeCol">boolean</td> <td class="paramDescCol">是否存儲查詢詞用于高亮處理, 默認為 true</td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">XSDocument[]</td> <td class="paramDescCol">匹配的搜索結果文檔列表</td></tr></table>
**源碼:**[sdk/php/lib/XSSearch.class.php#L554](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSSearch.class.php#L554) (**[顯示](#)**)
`public?function?search($query?=?null,?$saveHighlight?=?true)
{
????if?($this->_curDb?!==?self::LOG_DB?&&?$saveHighlight)?{
????????$this->_highlight?=?$query;
????}
????$query?=?$query?===?null???''?:?$this->preQueryString($query);
????$page?=?pack('II',?$this->_offset,?$this->_limit?>?0???$this->_limit?:?self::PAGE_SIZE);
????//?get?result?header
????$cmd?=?new?XSCommand(CMD_SEARCH_GET_RESULT,?0,?$this->_defaultOp,?$query,?$page);
????$res?=?$this->execCommand($cmd,?CMD_OK_RESULT_BEGIN);
????$tmp?=?unpack('Icount',?$res->buf);
????$this->_lastCount?=?$tmp['count'];
????//?load?vno?map?to?name?of?fields
????$ret?=?$this->_facets?=?array();
????$vnoes?=?$this->xs->getScheme()->getVnoMap();
????//?get?result?documents
????while?(true)?{
????????$res?=?$this->getRespond();
????????if?($res->cmd?==?CMD_SEARCH_RESULT_FACETS)?{
????????????$off?=?0;
????????????while?(($off?+?6)?<?strlen($res->buf))?{
????????????????$tmp?=?unpack('Cvno/Cvlen/Inum',?substr($res->buf,?$off,?6));
????????????????if?(isset($vnoes[$tmp['vno']]))?{
????????????????????$name?=?$vnoes[$tmp['vno']];
????????????????????$value?=?substr($res->buf,?$off?+?6,?$tmp['vlen']);
????????????????????if?(!isset($this->_facets[$name]))?{
????????????????????????$this->_facets[$name]?=?array();
????????????????????}
????????????????????$this->_facets[$name][$value]?=?$tmp['num'];
????????????????}
????????????????$off?+=?$tmp['vlen']?+?6;
????????????}
????????}?elseif?($res->cmd?==?CMD_SEARCH_RESULT_DOC)?{
????????????//?got?new?doc
????????????$doc?=?new?XSDocument($res->buf,?$this->_charset);
????????????$ret[]?=?$doc;
????????}?elseif?($res->cmd?==?CMD_SEARCH_RESULT_FIELD)?{
????????????//?fields?of?doc
????????????if?(isset($doc))?{
????????????????$name?=?isset($vnoes[$res->arg])???$vnoes[$res->arg]?:?$res->arg;
????????????????$doc->setField($name,?$res->buf);
????????????}
????????}?elseif?($res->cmd?==?CMD_SEARCH_RESULT_MATCHED)?{
????????????//?matched?terms
????????????if?(isset($doc))?{
????????????????$doc->setField('matched',?explode('?',?$res->buf),?true);
????????????}
????????}?elseif?($res->cmd?==?CMD_OK?&&?$res->arg?==?CMD_OK_RESULT_END)?{
????????????//?got?the?end
????????????break;
????????}?else?{
????????????$msg?=?'Unexpected?respond?in?search?{CMD:'?.?$res->cmd?.?',?ARG:'?.?$res->arg?.?'}';
????????????throw?new?XSException($msg);
????????}
????}
????if?($query?===?'')?{
????????$this->_count?=?$this->_lastCount;
????????//?trigger?log?&?highlight
????????if?($this->_curDb?!==?self::LOG_DB)?{
????????????$this->logQuery();
????????????if?($saveHighlight)?{
????????????????$this->initHighlight();
????????????}
????????}
????}
????$this->_limit?=?$this->_offset?=?0;
????return?$ret;
}`
獲取匹配的搜索結果文檔默認提取最匹配的前 self::PAGE_SIZE 個結果如需分頁請參見 [setLimit](#) 設置, 每次調用本函數后都會還原 setLimit 的設置
setAutoSynonyms()方法(自版本 v1.3.0 起可用)
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public XSSearch <b>setAutoSynonyms</b>(bool $value=true)</div></td></tr><tr><td class="paramNameCol">$value</td> <td class="paramTypeCol">bool</td> <td class="paramDescCol">設為 true 表示開啟同義詞功能, 設為 false 關閉同義詞功能</td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">XSSearch</td> <td class="paramDescCol">返回對象本身以支持串接操作</td></tr></table>
**源碼:**[sdk/php/lib/XSSearch.class.php#L129](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSSearch.class.php#L129) (**[顯示](#)**)
`public?function?setAutoSynonyms($value?=?true)
{
????$flag?=?CMD_PARSE_FLAG_BOOLEAN?|?CMD_PARSE_FLAG_PHRASE?|?CMD_PARSE_FLAG_LOVEHATE;
????if?($value?===?true)?{
????????$flag?|=?CMD_PARSE_FLAG_AUTO_MULTIWORD_SYNONYMS;
????}
????$cmd?=?array('cmd'?=>?CMD_QUERY_PARSEFLAG,?'arg'?=>?$flag);
????$this->execCommand($cmd);
????return?$this;
}`
開啟自動同義詞搜索功能
setCharset()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public XSSearch <b>setCharset</b>(string $charset)</div></td></tr><tr><td class="paramNameCol">$charset</td> <td class="paramTypeCol">string</td> <td class="paramDescCol"></td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">XSSearch</td> <td class="paramDescCol">返回對象本身以支持串接操作</td></tr></table>
**源碼:**[sdk/php/lib/XSSearch.class.php#L69](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSSearch.class.php#L69) (**[顯示](#)**)
`public?function?setCharset($charset)
{
????$this->_charset?=?strtoupper($charset);
????if?($this->_charset?==?'UTF8')?{
????????$this->_charset?=?'UTF-8';
????}
????return?$this;
}`
設置字符集默認字符集是 UTF-8, 如果您提交的搜索語句和預期得到的搜索結果為其它字符集, 請先設置
setCollapse()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public XSSearch <b>setCollapse</b>(string $field, int $num=1)</div></td></tr><tr><td class="paramNameCol">$field</td> <td class="paramTypeCol">string</td> <td class="paramDescCol">依據該字段的值折疊搜索結果, 設為 null 則取消折疊</td></tr><tr><td class="paramNameCol">$num</td> <td class="paramTypeCol">int</td> <td class="paramDescCol">折疊后只是返最匹配的數據數量, 默認為 1, 最大值 255</td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">XSSearch</td> <td class="paramDescCol">返回對象本身以支持串接操作</td></tr></table>
**源碼:**[sdk/php/lib/XSSearch.class.php#L314](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSSearch.class.php#L314) (**[顯示](#)**)
`public?function?setCollapse($field,?$num?=?1)
{
????$vno?=?$field?===?null???XSFieldScheme::MIXED_VNO?:?$this->xs->getField($field,?true)->vno;
????$max?=?min(255,?intval($num));
????$cmd?=?new?XSCommand(CMD_SEARCH_SET_COLLAPSE,?$max,?$vno);
????$this->execCommand($cmd);
????return?$this;
}`
設置折疊搜索結果注意, 每當調用 [setDb](#) 或 [addDb](#) 修改當前數據庫時會重置此項設置
setCutOff()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public XSSearch <b>setCutOff</b>(int $percent, float $weight=0)</div></td></tr><tr><td class="paramNameCol">$percent</td> <td class="paramTypeCol">int</td> <td class="paramDescCol">剔除匹配百分比低于此值的文檔, 值范圍 0-100</td></tr><tr><td class="paramNameCol">$weight</td> <td class="paramTypeCol">float</td> <td class="paramDescCol">剔除權重低于此值的文檔, 值范圍 0.1-25.5, 0 表示不剔除</td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">XSSearch</td> <td class="paramDescCol">返回對象本身以支持串接操作</td></tr></table>
**源碼:**[sdk/php/lib/XSSearch.class.php#L98](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSSearch.class.php#L98) (**[顯示](#)**)
`public?function?setCutOff($percent,?$weight?=?0)
{
????$percent?=?max(0,?min(100,?intval($percent)));
????$weight?=?max(0,?(intval($weight?*?10)?&?255));
????$cmd?=?new?XSCommand(CMD_SEARCH_SET_CUTOFF,?$percent,?$weight);
????$this->execCommand($cmd);
????return?$this;
}`
設置百分比/權重剔除參數通常是在開啟 [setFuzzy](#) 或使用 OR 連接搜索語句時才需要設置此項
#### 參見
- [setFuzzy](#)
setDb()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public XSSearch <b>setDb</b>(string $name)</div></td></tr><tr><td class="paramNameCol">$name</td> <td class="paramTypeCol">string</td> <td class="paramDescCol"></td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">XSSearch</td> <td class="paramDescCol">返回對象本身以支持串接操作</td></tr></table>
**源碼:**[sdk/php/lib/XSSearch.class.php#L457](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSSearch.class.php#L457) (**[顯示](#)**)
`public?function?setDb($name)
{
????$name?=?strval($name);
????$this->execCommand(array('cmd'?=>?CMD_SEARCH_SET_DB,?'buf'?=>?strval($name)));
????$this->_lastDb?=?$this->_curDb;
????$this->_lastDbs?=?$this->_curDbs;
????$this->_curDb?=?$name;
????$this->_curDbs?=?array();
????return?$this;
}`
設置要搜索的數據庫名若未設置, 使用默認數據庫, 數據庫必須位于服務端用戶目錄下對于遠程數據庫, 請使用 stub 文件來支持
setDocOrder()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public XSSearch <b>setDocOrder</b>(bool $asc=false)</div></td></tr><tr><td class="paramNameCol">$asc</td> <td class="paramTypeCol">bool</td> <td class="paramDescCol">是否為正序排列, 即從先到后, 默認為反序</td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">XSSearch</td> <td class="paramDescCol">返回對象本身以支持串接操作</td></tr></table>
**源碼:**[sdk/php/lib/XSSearch.class.php#L299](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSSearch.class.php#L299) (**[顯示](#)**)
`public?function?setDocOrder($asc?=?false)
{
????$type?=?CMD_SORT_TYPE_DOCID?|?($asc???CMD_SORT_FLAG_ASCENDING?:?0);
????$cmd?=?new?XSCommand(CMD_SEARCH_SET_SORT,?$type);
????$this->execCommand($cmd);
????return?$this;
}`
設置結果按索引入庫先后排序注意, 此項排序不影響相關排序, 權重高的仍會在前面, 主要適合用于布爾檢索
setFacets()方法(自版本 v1.1.0 起可用)
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public XSSearch <b>setFacets</b>(mixed $field, bool $exact=false)</div></td></tr><tr><td class="paramNameCol">$field</td> <td class="paramTypeCol">mixed</td> <td class="paramDescCol">要進行分組統計的字段或字段組成的數組, 最多同時支持 8 個</td></tr><tr><td class="paramNameCol">$exact</td> <td class="paramTypeCol">bool</td> <td class="paramDescCol">是否要求絕對精確搜索, 這會造成較大的系統開銷</td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">XSSearch</td> <td class="paramDescCol">返回對象本身以支持串接操作</td></tr></table>
**源碼:**[sdk/php/lib/XSSearch.class.php#L383](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSSearch.class.php#L383) (**[顯示](#)**)
`public?function?setFacets($field,?$exact?=?false)
{
????$buf?=?'';
????if?(!is_array($field))?{
????????$field?=?array($field);
????}
????foreach?($field?as?$name)?{
????????$ff?=?$this->xs->getField($name);
????????if?($ff->type?!==?XSFieldMeta::TYPE_STRING)?{
????????????throw?new?XSException("Field?`$name'?cann't?be?used?for?facets?search,?can?only?be?string?type");
????????}
????????$buf?.=?chr($ff->vno);
????}
????$cmd?=?array('cmd'?=>?CMD_SEARCH_SET_FACETS,?'buf'?=>?$buf);
????$cmd['arg1']?=?$exact?===?true???1?:?0;
????$this->execCommand($cmd);
????return?$this;
}`
設置分面搜索記數用于記錄匹配搜索結果中按字段值分組的數量統計, 每次調用 [search](#) 后會還原設置 對于多次調用 $exact 參數以最后一次為準, 只支持字段值不超過 255 字節的情況
setFuzzy()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public XSSearch <b>setFuzzy</b>(bool $value=true)</div></td></tr><tr><td class="paramNameCol">$value</td> <td class="paramTypeCol">bool</td> <td class="paramDescCol">設為 true 表示開啟模糊搜索, 設為 false 關閉模糊搜索</td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">XSSearch</td> <td class="paramDescCol">返回對象本身以支持串接操作</td></tr></table>
**源碼:**[sdk/php/lib/XSSearch.class.php#L84](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSSearch.class.php#L84) (**[顯示](#)**)
`public?function?setFuzzy($value?=?true)
{
????$this->_defaultOp?=?$value?===?true???CMD_QUERY_OP_OR?:?CMD_QUERY_OP_AND;
????return?$this;
}`
開啟模糊搜索默認情況只返回包含所有搜索詞的記錄, 通過本方法可以獲得更多搜索結果
setLimit()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public XSSearch <b>setLimit</b>(int $limit, int $offset=0)</div></td></tr><tr><td class="paramNameCol">$limit</td> <td class="paramTypeCol">int</td> <td class="paramDescCol">數量上限, 若設為 0 則啟用默認值 self::PAGE_SIZE</td></tr><tr><td class="paramNameCol">$offset</td> <td class="paramTypeCol">int</td> <td class="paramDescCol">偏移量, 即跳過的結果數量, 默認為 0</td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">XSSearch</td> <td class="paramDescCol">返回對象本身以支持串接操作</td></tr></table>
**源碼:**[sdk/php/lib/XSSearch.class.php#L443](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSSearch.class.php#L443) (**[顯示](#)**)
`public?function?setLimit($limit,?$offset?=?0)
{
????$this->_limit?=?intval($limit);
????$this->_offset?=?intval($offset);
????return?$this;
}`
設置搜索結果的數量和偏移用于搜索結果分頁, 每次調用 [search](#) 后會還原這2個變量到初始值
setMultiSort()方法(自版本 v1.1.0 起可用)
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public XSSearch <b>setMultiSort</b>(array $fields, bool $reverse=false, bool $relevance_first=false)</div></td></tr><tr><td class="paramNameCol">$fields</td> <td class="paramTypeCol">array</td> <td class="paramDescCol">排序依據的字段數組, 以字段名稱為鍵, true/false 為值表示正序或逆序</td></tr><tr><td class="paramNameCol">$reverse</td> <td class="paramTypeCol">bool</td> <td class="paramDescCol">是否為倒序顯示, 默認為正向, 此處和 <a href="XSSearch.html#setSort">setSort</a> 略有不同</td></tr><tr><td class="paramNameCol">$relevance_first</td> <td class="paramTypeCol">bool</td> <td class="paramDescCol">是否優先相關性排序, 默認為否</td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">XSSearch</td> <td class="paramDescCol">返回對象本身以支持串接操作</td></tr></table>
**源碼:**[sdk/php/lib/XSSearch.class.php#L228](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSSearch.class.php#L228) (**[顯示](#)**)
`public?function?setMultiSort($fields,?$reverse?=?false,?$relevance_first?=?false)
{
????if?(!is_array($fields))?{
????????return?$this->setSort($fields,?!$reverse,?$relevance_first);
????}
????//?[vno][0/1]?(0:reverse,1:asc)
????$buf?=?'';
????foreach?($fields?as?$key?=>?$value)?{
????????if?(is_bool($value))?{
????????????$vno?=?$this->xs->getField($key,?true)->vno;
????????????$asc?=?$value;
????????}?else?{
????????????$vno?=?$this->xs->getField($value,?true)->vno;
????????????$asc?=?false;
????????}
????????if?($vno?!=?XSFieldScheme::MIXED_VNO)?{
????????????$buf?.=?chr($vno)?.?chr($asc???1?:?0);
????????}
????}
????if?($buf?!==?'')?{
????????$type?=?CMD_SORT_TYPE_MULTI;
????????if?($relevance_first)?{
????????????$type?|=?CMD_SORT_FLAG_RELEVANCE;
????????}
????????if?(!$reverse)?{
????????????$type?|=?CMD_SORT_FLAG_ASCENDING;
????????}
????????$cmd?=?new?XSCommand(CMD_SEARCH_SET_SORT,?$type,?0,?$buf);
????????$this->execCommand($cmd);
????}
????return?$this;
}`
設置多字段組合排序方式當您需要根據多個字段的值按不同的方式綜合排序時, 請使用這項
setQuery()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public XSSearch <b>setQuery</b>(string $query)</div></td></tr><tr><td class="paramNameCol">$query</td> <td class="paramTypeCol">string</td> <td class="paramDescCol">搜索語句, 設為 null 則清空搜索語句, 最大長度為 80 字節</td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">XSSearch</td> <td class="paramDescCol">返回對象本身以支持串接操作</td></tr></table>
**源碼:**[sdk/php/lib/XSSearch.class.php#L209](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSSearch.class.php#L209) (**[顯示](#)**)
`public?function?setQuery($query)
{
????$this->clearQuery();
????if?($query?!==?null)?{
????????$this->_query?=?$query;
????????$this->addQueryString($query);
????}
????return?$this;
}`
設置默認搜索語句用于不帶參數的 [count](#) 或 [search](#) 以及 [terms](#) 調用可與 [addWeight](#) 組合運用
setRequireMatchedTerm()方法(自版本 v1.4.8 起可用)
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public XSSearch <b>setRequireMatchedTerm</b>(bool $value=true)</div></td></tr><tr><td class="paramNameCol">$value</td> <td class="paramTypeCol">bool</td> <td class="paramDescCol">設為 true 表示開啟返回, 設為 false 關閉該功能, 默認是不開啟</td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">XSSearch</td> <td class="paramDescCol">返回對象本身以支持串接操作</td></tr></table>
**源碼:**[sdk/php/lib/XSSearch.class.php#L114](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSSearch.class.php#L114) (**[顯示](#)**)
`public?function?setRequireMatchedTerm($value?=?true)
{
????$arg1?=?CMD_SEARCH_MISC_MATCHED_TERM;
????$arg2?=?$value?===?true???1?:?0;
????$cmd?=?new?XSCommand(CMD_SEARCH_SET_MISC,?$arg1,?$arg2);
????$this->execCommand($cmd);
????return?$this;
}`
設置在搜索結果文檔中返回匹配詞表請在 [search](#) 前調用本方法, 然后使用 XSDocument::matched 獲取
setScwsMulti()方法(自版本 v1.4.7 起可用)
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public XSSearch <b>setScwsMulti</b>(int $level)</div></td></tr><tr><td class="paramNameCol">$level</td> <td class="paramTypeCol">int</td> <td class="paramDescCol">要設置的分詞復合等級</td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">XSSearch</td> <td class="paramDescCol">返回自身對象以支持串接操作</td></tr></table>
**源碼:**[sdk/php/lib/XSSearch.class.php#L426](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSSearch.class.php#L426) (**[顯示](#)**)
`public?function?setScwsMulti($level)
{
????$level?=?intval($level);
????if?($level?>=?0?&&?$level?<?16)?{
????????$cmd?=?array('cmd'?=>?CMD_SEARCH_SCWS_SET,?'arg1'?=>?CMD_SCWS_SET_MULTI,?'arg2'?=>?$level);
????????$this->execCommand($cmd);
????}
????return?$this;
}`
設置當前搜索語句的分詞復合等級復合等級是 scws 分詞粒度控制的一個重要參數, 是長詞細分處理依據, 默認為 3, 值范圍 0~15注意: 這個設置僅直對本次搜索有效, 僅對設置之后的 [setQuery](#) 起作用, 由于 query設計的方式問題, 目前無法支持搜索語句單字切分, 但您可以在模糊檢索時設為 0 來關閉復合分詞
setSort()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public XSSearch <b>setSort</b>(string $field, bool $asc=false, bool $relevance_first=false)</div></td></tr><tr><td class="paramNameCol">$field</td> <td class="paramTypeCol">string</td> <td class="paramDescCol">依據指定字段的值排序, 設為 null 則用默認順序</td></tr><tr><td class="paramNameCol">$asc</td> <td class="paramTypeCol">bool</td> <td class="paramDescCol">是否為正序排列, 即從小到大, 從少到多, 默認為反序</td></tr><tr><td class="paramNameCol">$relevance_first</td> <td class="paramTypeCol">bool</td> <td class="paramDescCol">是否優先相關性排序, 默認為否</td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">XSSearch</td> <td class="paramDescCol">返回對象本身以支持串接操作</td></tr></table>
**源碼:**[sdk/php/lib/XSSearch.class.php#L271](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSSearch.class.php#L271) (**[顯示](#)**)
`public?function?setSort($field,?$asc?=?false,?$relevance_first?=?false)
{
????if?(is_array($field))?{
????????return?$this->setMultiSort($field,?$asc,?$relevance_first);
????}
????if?($field?===?null)?{
????????$cmd?=?new?XSCommand(CMD_SEARCH_SET_SORT,?CMD_SORT_TYPE_RELEVANCE);
????}?else?{
????????$type?=?CMD_SORT_TYPE_VALUE;
????????if?($relevance_first)?{
????????????$type?|=?CMD_SORT_FLAG_RELEVANCE;
????????}
????????if?($asc)?{
????????????$type?|=?CMD_SORT_FLAG_ASCENDING;
????????}
????????$vno?=?$this->xs->getField($field,?true)->vno;
????????$cmd?=?new?XSCommand(CMD_SEARCH_SET_SORT,?$type,?$vno);
????}
????$this->execCommand($cmd);
????return?$this;
}`
設置搜索結果的排序方式注意, 每當調用 [setDb](#) 或 [addDb](#) 修改當前數據庫時會重置排序設定此函數第一參數的用法與 [setMultiSort](#) 兼容, 即也可以用該方法實現多字段排序
setSynonymScale()方法(自版本 v1.4.7 起可用)
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public XSSearch <b>setSynonymScale</b>(float $value)</div></td></tr><tr><td class="paramNameCol">$value</td> <td class="paramTypeCol">float</td> <td class="paramDescCol">取值范圍 0.01-2.55, 1 表示不調整</td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">XSSearch</td> <td class="paramDescCol">返回對象本身以支持串接操作</td></tr></table>
**源碼:**[sdk/php/lib/XSSearch.class.php#L147](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSSearch.class.php#L147) (**[顯示](#)**)
`public?function?setSynonymScale($value)
{
????$arg1?=?CMD_SEARCH_MISC_SYN_SCALE;
????$arg2?=?max(0,?(intval($value?*?100)?&?255));
????$cmd?=?new?XSCommand(CMD_SEARCH_SET_MISC,?$arg1,?$arg2);
????$this->execCommand($cmd);
????return?$this;
}`
設置同義詞搜索的權重比例
terms()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public array <b>terms</b>(string $query=NULL, bool $convert=true)</div></td></tr><tr><td class="paramNameCol">$query</td> <td class="paramTypeCol">string</td> <td class="paramDescCol">搜索語句, 若傳入 null 使用默認語句, 最大長度為 80 字節</td></tr><tr><td class="paramNameCol">$convert</td> <td class="paramTypeCol">bool</td> <td class="paramDescCol">是否進行編碼轉換, 默認為 true</td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">array</td> <td class="paramDescCol">可用于高亮顯示的詞條列表</td></tr></table>
**源碼:**[sdk/php/lib/XSSearch.class.php#L497](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSSearch.class.php#L497) (**[顯示](#)**)
`public?function?terms($query?=?null,?$convert?=?true)
{
????$query?=?$query?===?null???''?:?$this->preQueryString($query);
????if?($query?===?''?&&?$this->_terms?!==?null)?{
????????$ret?=?$this->_terms;
????}?else?{
????????$cmd?=?new?XSCommand(CMD_QUERY_GET_TERMS,?0,?$this->_defaultOp,?$query);
????????$res?=?$this->execCommand($cmd,?CMD_OK_QUERY_TERMS);
????????$ret?=?array();
????????$tmps?=?explode('?',?$res->buf);
????????for?($i?=?0;?$i?<?count($tmps);?$i++)?{
????????????if?($tmps[$i]?===?''?||?strpos($tmps[$i],?':')?!==?false)?{
????????????????continue;
????????????}
????????????$ret[]?=?$tmps[$i];
????????}
????????if?($query?===?'')?{
????????????$this->_terms?=?$ret;
????????}
????}
????return?$convert???XS::convert($ret,?$this->_charset,?'UTF-8')?:?$ret;
}`
獲取搜索語句中的高亮詞條列表
Copyright ? 2008-2011 by [杭州云圣網絡科技有限公司](http://www.xunsearch.com)
All Rights Reserved.
- 權威指南
- 新手上路
- 最新主要變動
- 概述
- 關于 Xunsearch PHP-SDK
- 安裝、升級
- 體驗 demo 項目
- 開發規范
- 開發流程
- 了解基礎對象
- 基礎對象概述
- XS 項目
- XSException 異常
- XSDocument 文檔
- XSIndex 索引管理
- XSSearch 搜索
- XSTokenizer 分詞接口
- 編寫項目配置文件
- 項目配置詳解
- 自定義分詞器
- 編寫第一個配置文件
- 管理索引
- 索引概述
- 添加文檔
- 更新、修改文檔
- 刪除文檔
- 清空索引
- 平滑重建索引
- 使用索引緩沖區
- 自定義SCWS詞庫
- 使用搜索
- 搜索概述
- 構建搜索語句
- 獲取搜索匹配結果
- 獲取搜索匹配數量
- 獲取熱門搜索詞
- 獲取相關搜索詞
- 搜索建議和糾錯
- 按字段值分面搜索
- 使用輔助工具
- RequiredCheck 運行檢測
- Indexer 索引管理器
- Quest 搜索測試工具
- SearchSkel 生成搜索代碼
- IniWizzard 配置文件向導
- Logger 搜索日志管理
- 專題
- 同義詞搜索功能
- 在SDK中使用SCWS分詞
- API 指南
- XS
- XS
- XSCommand
- XSComponent
- XSDocument
- XSErrorException
- XSException
- XSFieldMeta
- XSFieldScheme
- XSIndex
- XSSearch
- XSServer
- XS.tokenizer
- XSTokenizer
- XSTokenizerFull
- XSTokenizerNone
- XSTokenizerScws
- XSTokenizerSplit
- XSTokenizerXlen
- XSTokenizerXstep
- XS.util
- XSCsvDataSource
- XSDataFilter
- XSDatabaseDataSource
- XSDebugFilter
- XSJsonDataSource
- XSUtil
- XS.util.db
- XSDatabase
- XSDatabaseMySQL
- XSDatabaseMySQLI
- XSDatabasePDO
- XSDatabasePDO_MySQL
- XSDatabasePDO_PgSQL
- XSDatabasePDO_SQLite
- XSDatabasePgSQL
- XSDatabaseSQLite
- XSDatabaseSQLite3
- XS.utilf
- XSDataSource
- 其它文檔
- 關于 xunsearch
- 特色和優勢
- Xunsearch 架構簡圖
- 下載 Xunsearch
- 商業服務與支持
- XunSearch 授權許可證