ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[Xunsearch PHP-SDK](http://www.xunsearch.com) v1.4.8 API 参考文档 # XS [All Packages](#)| [属性](#)| [方法(函数)](#) | 包 | [XS](#) | |-----|-----| | 继承关系 | class XS »[XSComponent](#) | | 版本 | 1.0.0 | | 源代码 | [sdk/php/lib/XS.class.php](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XS.class.php) | XS 搜索项目主类 ### Public 属性 [隐去继承来的属性](#) | 名称 | 类型 | 描述 | 定义于 | |-----|-----|-----|-----| | [allFields](#) | XSFieldMeta[] | 获取项目所有字段结构设置 | XS | | [config](#) | array | 获取配置原始数据 | XS | | [defaultCharset](#) | string | 获取项目的默认字符集 | XS | | [fieldBody](#) | [XSFieldMeta](#) | 获取当前内容字段 | XS | | [fieldId](#) | [XSFieldMeta](#) | 获取当前主键字段 | XS | | [fieldTitle](#) | [XSFieldMeta](#) | 获取当前标题字段 | XS | | [index](#) | [XSIndex](#) | 获取索引操作对象 | XS | | [name](#) | string | 获取当前项目名称 | XS | | [scheme](#) | [XSFieldScheme](#) | 获取当前在用的字段方案 | XS | | [scwsServer](#) | [XSServer](#) | 创建 scws 分词连接 | XS | | [search](#) | [XSSearch](#) | 获取搜索操作对象 | XS | ### Public 方法 [隐去继承来的方法](#) | 名称 | 描述 | 定义于 | |-----|-----|-----| | [__construct()](#) | 构造函数 | XS | | [__destruct()](#) | 析构函数 | XS | | [__get()](#) | 魔术方法 __get | [XSComponent](#) | | [__isset()](#) | 魔术方法 __isset | [XSComponent](#) | | [__set()](#) | 魔术方法 __set | [XSComponent](#) | | [__unset()](#) | 魔术方法 __unset | [XSComponent](#) | | [autoload()](#) | 智能加载类库文件 | XS | | [convert()](#) | 字符集转换 | XS | | [getAllFields()](#) | 获取项目所有字段结构设置 | XS | | [getConfig()](#) | Returns 获取配置原始数据 | XS | | [getDefaultCharset()](#) | 获取项目的默认字符集 | XS | | [getField()](#) | 获取项目字段元数据 | XS | | [getFieldBody()](#) | 获取当前内容字段 | XS | | [getFieldId()](#) | 获取当前主键字段 | XS | | [getFieldTitle()](#) | 获取当前标题字段 | XS | | [getIndex()](#) | 获取索引操作对象 | XS | | [getLastXS()](#) | 获取最新的 XS 实例 | XS | | [getName()](#) | 获取当前项目名称 | XS | | [getScheme()](#) | 获取当前在用的字段方案 | XS | | [getScwsServer()](#) | 创建 scws 分词连接 | XS | | [getSearch()](#) | 获取搜索操作对象 | XS | | [restoreScheme()](#) | 还原字段方案为项目绑定方案 | XS | | [setDefaultCharset()](#) | 改变项目的默认字符集 | XS | | [setName()](#) | 修改当前项目名称 | XS | | [setScheme()](#) | 设置当前在用的字段方案 | XS | ### 属性明细 allFields属性 *只读* public XSFieldMeta[] [**getAllFields**](#)() 获取项目所有字段结构设置 config属性 *只读* public array [**getConfig**](#)() 获取配置原始数据 defaultCharset属性 public string [**getDefaultCharset**](#)() public void [**setDefaultCharset**](#)(string $charset) 获取项目的默认字符集 fieldBody属性 *只读* public [XSFieldMeta](#)[**getFieldBody**](#)() 获取当前内容字段 #### 参见 - [XSFieldScheme::getFieldBody](#) fieldId属性 *只读* public [XSFieldMeta](#)[**getFieldId**](#)() 获取当前主键字段 #### 参见 - [XSFieldScheme::getFieldId](#) fieldTitle属性 *只读* public [XSFieldMeta](#)[**getFieldTitle**](#)() 获取当前标题字段 #### 参见 - [XSFieldScheme::getFieldTitle](#) index属性 *只读* public [XSIndex](#)[**getIndex**](#)() 获取索引操作对象 name属性 public string [**getName**](#)() public void [**setName**](#)(string $name) 获取当前项目名称 scheme属性 public [XSFieldScheme](#)[**getScheme**](#)() public void [**setScheme**](#)([XSFieldScheme](#) $fs) 获取当前在用的字段方案通用于搜索结果文档和修改、添加的索引文档 scwsServer属性 *只读* public [XSServer](#)[**getScwsServer**](#)() 创建 scws 分词连接 search属性 *只读* public [XSSearch](#)[**getSearch**](#)() 获取搜索操作对象 ### 方法明细 __construct()方法 <table class="summaryTable"><tr><td colspan="3"><div class="signature2">public void <b>__construct</b>(string $file)</div></td></tr><tr><td class="paramNameCol">$file</td> <td class="paramTypeCol">string</td> <td class="paramDescCol">要加载的项目配置文件</td></tr></table> **源码:**[sdk/php/lib/XS.class.php#L284](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XS.class.php#L284) (**[显示](#)**) `public function __construct($file) {     if (strlen($file) < 255 && !is_file($file)) {         $file2 = XS_LIB_ROOT . '/../app/' . $file . '.ini';         if (is_file($file2)) {             $file = $file2;         }     }     $this->loadIniFile($file);     self::$_lastXS = $this; }` 构造函数特别说明一个小技巧, 参数 $file 可以直接是配置文件的内容, 还可以是仅仅是文件名,如果只是文件名会自动查找 XS_LIB_ROOT/../app/$file.ini __destruct()方法 <table class="summaryTable"><tr><td colspan="3"><div class="signature2">public void <b>__destruct</b>()</div></td></tr></table> **源码:**[sdk/php/lib/XS.class.php#L300](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XS.class.php#L300) (**[显示](#)**) `public function __destruct() {     $this->_index = null;     $this->_search = null; }` 析构函数由于对象交叉引用, 如需提前销毁对象, 请强制调用该函数 autoload()方法 <table class="summaryTable"><tr><td colspan="3"><div class="signature2">public static void <b>autoload</b>(string $name)</div></td></tr><tr><td class="paramNameCol">$name</td> <td class="paramTypeCol">string</td> <td class="paramDescCol">类的名称</td></tr></table> **源码:**[sdk/php/lib/XS.class.php#L529](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XS.class.php#L529) (**[显示](#)**) `public static function autoload($name) {     $file = XS_LIB_ROOT . '/' . $name . '.class.php';     if (file_exists($file)) {         require_once $file;     } }` 智能加载类库文件要求以 Name.class.php 命名并与本文件存放在同一目录, 如: XSTokenizerXxx.class.php convert()方法 <table class="summaryTable"><tr><td colspan="3"><div class="signature2">public static mixed <b>convert</b>(mixed $data, string $to, string $from)</div></td></tr><tr><td class="paramNameCol">$data</td> <td class="paramTypeCol">mixed</td> <td class="paramDescCol">需要转换的数据, 支持 string 和 array, 数组会自动递归转换</td></tr><tr><td class="paramNameCol">$to</td> <td class="paramTypeCol">string</td> <td class="paramDescCol">转换后的字符集</td></tr><tr><td class="paramNameCol">$from</td> <td class="paramTypeCol">string</td> <td class="paramDescCol">转换前的字符集</td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">mixed</td> <td class="paramDescCol">转换后的数据</td></tr></table> **源码:**[sdk/php/lib/XS.class.php#L546](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XS.class.php#L546) (**[显示](#)**) `public static function convert($data, $to, $from) {     // need not convert     if ($to == $from) {         return $data;     }     // array traverse     if (is_array($data)) {         foreach ($data as $key => $value) {             $data[$key] = self::convert($value, $to, $from);         }         return $data;     }     // string contain 8bit characters     if (is_string($data) && preg_match('/[\x81-\xfe]/', $data)) {         // mbstring, iconv, throw ...         if (function_exists('mb_convert_encoding')) {             return mb_convert_encoding($data, $to, $from);         } elseif (function_exists('iconv')) {             return iconv($from, $to . '//TRANSLIT', $data);         } else {             throw new XSException('Cann\'t find the mbstring or iconv extension to convert encoding');         }     }     return $data; }` 字符集转换要求安装有 mbstring, iconv 中的一种 getAllFields()方法 <table class="summaryTable"><tr><td colspan="3"><div class="signature2">public XSFieldMeta[] <b>getAllFields</b>()</div></td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">XSFieldMeta[]</td> <td class="paramDescCol"></td></tr></table> **源码:**[sdk/php/lib/XS.class.php#L519](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XS.class.php#L519) (**[显示](#)**) `public function getAllFields() {     return $this->_scheme->getAllFields(); }` 获取项目所有字段结构设置 getConfig()方法 <table class="summaryTable"><tr><td colspan="3"><div class="signature2">public array <b>getConfig</b>()</div></td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">array</td> <td class="paramDescCol">获取配置原始数据</td></tr></table> **源码:**[sdk/php/lib/XS.class.php#L355](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XS.class.php#L355) (**[显示](#)**) `public function getConfig() {     return $this->_config; }` getDefaultCharset()方法 <table class="summaryTable"><tr><td colspan="3"><div class="signature2">public string <b>getDefaultCharset</b>()</div></td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">string</td> <td class="paramDescCol">默认字符集(已大写)</td></tr></table> **源码:**[sdk/php/lib/XS.class.php#L383](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XS.class.php#L383) (**[显示](#)**) `public function getDefaultCharset() {     return isset($this->_config['project.default_charset']) ?             strtoupper($this->_config['project.default_charset']) : 'UTF-8'; }` 获取项目的默认字符集 getField()方法 <table class="summaryTable"><tr><td colspan="3"><div class="signature2">public <a href="XSFieldMeta.html">XSFieldMeta</a> <b>getField</b>(mixed $name, bool $throw=true)</div></td></tr><tr><td class="paramNameCol">$name</td> <td class="paramTypeCol">mixed</td> <td class="paramDescCol">字段名称(string) 或字段序号(vno, int)</td></tr><tr><td class="paramNameCol">$throw</td> <td class="paramTypeCol">bool</td> <td class="paramDescCol">当字段不存在时是否抛出异常, 默认为 true</td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol"><a href="XSFieldMeta.html">XSFieldMeta</a></td> <td class="paramDescCol">字段元数据对象</td></tr></table> **源码:**[sdk/php/lib/XS.class.php#L510](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XS.class.php#L510) (**[显示](#)**) `public function getField($name, $throw = true) {     return $this->_scheme->getField($name, $throw); }` 获取项目字段元数据 #### 参见 - [XSFieldScheme::getField](#) getFieldBody()方法 <table class="summaryTable"><tr><td colspan="3"><div class="signature2">public <a href="XSFieldMeta.html">XSFieldMeta</a> <b>getFieldBody</b>()</div></td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol"><a href="XSFieldMeta.html">XSFieldMeta</a></td> <td class="paramDescCol">类型为 BODY 的字段</td></tr></table> **源码:**[sdk/php/lib/XS.class.php#L497](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XS.class.php#L497) (**[显示](#)**) `public function getFieldBody() {     return $this->_scheme->getFieldBody(); }` 获取当前内容字段 #### 参见 - [XSFieldScheme::getFieldBody](#) getFieldId()方法 <table class="summaryTable"><tr><td colspan="3"><div class="signature2">public <a href="XSFieldMeta.html">XSFieldMeta</a> <b>getFieldId</b>()</div></td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol"><a href="XSFieldMeta.html">XSFieldMeta</a></td> <td class="paramDescCol">类型为 ID 的字段</td></tr></table> **源码:**[sdk/php/lib/XS.class.php#L477](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XS.class.php#L477) (**[显示](#)**) `public function getFieldId() {     return $this->_scheme->getFieldId(); }` 获取当前主键字段 #### 参见 - [XSFieldScheme::getFieldId](#) getFieldTitle()方法 <table class="summaryTable"><tr><td colspan="3"><div class="signature2">public <a href="XSFieldMeta.html">XSFieldMeta</a> <b>getFieldTitle</b>()</div></td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol"><a href="XSFieldMeta.html">XSFieldMeta</a></td> <td class="paramDescCol">类型为 TITLE 的字段</td></tr></table> **源码:**[sdk/php/lib/XS.class.php#L487](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XS.class.php#L487) (**[显示](#)**) `public function getFieldTitle() {     return $this->_scheme->getFieldTitle(); }` 获取当前标题字段 #### 参见 - [XSFieldScheme::getFieldTitle](#) getIndex()方法 <table class="summaryTable"><tr><td colspan="3"><div class="signature2">public <a href="XSIndex.html">XSIndex</a> <b>getIndex</b>()</div></td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol"><a href="XSIndex.html">XSIndex</a></td> <td class="paramDescCol">索引操作对象</td></tr></table> **源码:**[sdk/php/lib/XS.class.php#L402](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XS.class.php#L402) (**[显示](#)**) `public function getIndex() {     if ($this->_index === null) {         $adds = array();         $conn = isset($this->_config['server.index']) ? $this->_config['server.index'] : 8383;         if (($pos = strpos($conn, ';')) !== false) {             $adds = explode(';', substr($conn, $pos + 1));             $conn = substr($conn, 0, $pos);         }         $this->_index = new XSIndex($conn, $this);         $this->_index->setTimeout(0);         foreach ($adds as $conn) {             $conn = trim($conn);             if ($conn !== '') {                 $this->_index->addServer($conn)->setTimeout(0);             }         }     }     return $this->_index; }` 获取索引操作对象 getLastXS()方法 <table class="summaryTable"><tr><td colspan="3"><div class="signature2">public static XS <b>getLastXS</b>()</div></td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">XS</td> <td class="paramDescCol">最近创建的 XS 对象</td></tr></table> **源码:**[sdk/php/lib/XS.class.php#L310](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XS.class.php#L310) (**[显示](#)**) `public static function getLastXS() {     return self::$_lastXS; }` 获取最新的 XS 实例 getName()方法 <table class="summaryTable"><tr><td colspan="3"><div class="signature2">public string <b>getName</b>()</div></td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">string</td> <td class="paramDescCol">当前项目名称</td></tr></table> **源码:**[sdk/php/lib/XS.class.php#L364](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XS.class.php#L364) (**[显示](#)**) `public function getName() {     return $this->_config['project.name']; }` 获取当前项目名称 getScheme()方法 <table class="summaryTable"><tr><td colspan="3"><div class="signature2">public <a href="XSFieldScheme.html">XSFieldScheme</a> <b>getScheme</b>()</div></td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol"><a href="XSFieldScheme.html">XSFieldScheme</a></td> <td class="paramDescCol">当前字段方案</td></tr></table> **源码:**[sdk/php/lib/XS.class.php#L320](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XS.class.php#L320) (**[显示](#)**) `public function getScheme() {     return $this->_scheme; }` 获取当前在用的字段方案通用于搜索结果文档和修改、添加的索引文档 getScwsServer()方法 <table class="summaryTable"><tr><td colspan="3"><div class="signature2">public <a href="XSServer.html">XSServer</a> <b>getScwsServer</b>()</div></td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol"><a href="XSServer.html">XSServer</a></td> <td class="paramDescCol">分词服务器</td></tr></table> **源码:**[sdk/php/lib/XS.class.php#L463](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XS.class.php#L463) (**[显示](#)**) `public function getScwsServer() {     if ($this->_scws === null) {         $conn = isset($this->_config['server.search']) ? $this->_config['server.search'] : 8384;         $this->_scws = new XSServer($conn, $this);     }     return $this->_scws; }` 创建 scws 分词连接 getSearch()方法 <table class="summaryTable"><tr><td colspan="3"><div class="signature2">public <a href="XSSearch.html">XSSearch</a> <b>getSearch</b>()</div></td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol"><a href="XSSearch.html">XSSearch</a></td> <td class="paramDescCol">搜索操作对象</td></tr></table> **源码:**[sdk/php/lib/XS.class.php#L427](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XS.class.php#L427) (**[显示](#)**) `public function getSearch() {     if ($this->_search === null) {         $conns = array();         if (!isset($this->_config['server.search'])) {             $conns[] = 8384;         } else {             foreach (explode(';', $this->_config['server.search']) as $conn) {                 $conn = trim($conn);                 if ($conn !== '') {                     $conns[] = $conn;                 }             }         }         if (count($conns) > 1) {             shuffle($conns);         }         for ($i = 0; $i < count($conns); $i++) {             try {                 $this->_search = new XSSearch($conns[$i], $this);                 $this->_search->setCharset($this->getDefaultCharset());                 return $this->_search;             } catch (XSException $e) {                 if (($i + 1) === count($conns)) {                     throw $e;                 }             }         }     }     return $this->_search; }` 获取搜索操作对象 restoreScheme()方法 <table class="summaryTable"><tr><td colspan="3"><div class="signature2">public void <b>restoreScheme</b>()</div></td></tr></table> **源码:**[sdk/php/lib/XS.class.php#L342](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XS.class.php#L342) (**[显示](#)**) `public function restoreScheme() {     if ($this->_scheme !== $this->_bindScheme) {         $this->_scheme = $this->_bindScheme;         if ($this->_search !== null) {             $this->_search->markResetScheme(true);         }     } }` 还原字段方案为项目绑定方案 setDefaultCharset()方法 <table class="summaryTable"><tr><td colspan="3"><div class="signature2">public void <b>setDefaultCharset</b>(string $charset)</div></td></tr><tr><td class="paramNameCol">$charset</td> <td class="paramTypeCol">string</td> <td class="paramDescCol">修改后的字符集</td></tr></table> **源码:**[sdk/php/lib/XS.class.php#L393](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XS.class.php#L393) (**[显示](#)**) `public function setDefaultCharset($charset) {     $this->_config['project.default_charset'] = strtoupper($charset); }` 改变项目的默认字符集 setName()方法 <table class="summaryTable"><tr><td colspan="3"><div class="signature2">public void <b>setName</b>(string $name)</div></td></tr><tr><td class="paramNameCol">$name</td> <td class="paramTypeCol">string</td> <td class="paramDescCol">项目名称</td></tr></table> **源码:**[sdk/php/lib/XS.class.php#L374](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XS.class.php#L374) (**[显示](#)**) `public function setName($name) {     $this->_config['project.name'] = $name; }` 修改当前项目名称注意,必须在 [getSearch](#) 和 [getIndex](#) 前调用才能起作用 setScheme()方法 <table class="summaryTable"><tr><td colspan="3"><div class="signature2">public void <b>setScheme</b>(<a href="XSFieldScheme.html">XSFieldScheme</a> $fs)</div></td></tr><tr><td class="paramNameCol">$fs</td> <td class="paramTypeCol"><a href="XSFieldScheme.html">XSFieldScheme</a></td> <td class="paramDescCol">一个有效的字段方案对象</td></tr></table> **源码:**[sdk/php/lib/XS.class.php#L330](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XS.class.php#L330) (**[显示](#)**) `public function setScheme(XSFieldScheme $fs) {     $fs->checkValid(true);     $this->_scheme = $fs;     if ($this->_search !== null) {         $this->_search->markResetScheme();     } }` 设置当前在用的字段方案 Copyright © 2008-2011 by [杭州云圣网络科技有限公司](http://www.xunsearch.com) All Rights Reserved.