🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[Xunsearch PHP-SDK](http://www.xunsearch.com) v1.4.8 API 参考文档 # XSFieldScheme [All Packages](#)| [方法(函数)](#) | 包 | [XS](#) | |-----|-----| | 继承关系 | class XSFieldScheme | | 实现接口 | IteratorAggregate, Traversable | | 版本 | 1.0.0 | | 源代码 | [sdk/php/lib/XSFieldScheme.class.php](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSFieldScheme.class.php) | XS 数据字段方案每个方案包含若干个字段结构对象 [XSFieldMeta](#)每个方案必须并且只能包含一个类型为 ID 的字段, 支持 foreach 遍历所有字段 ### Public 方法 [隐去继承来的方法](#) | 名称 | 描述 | 定义于 | |-----|-----|-----| | [__toString()](#) | 将对象转换为配置文件字符串 | XSFieldScheme | | [addField()](#) | 添加字段到方案中 | XSFieldScheme | | [checkValid()](#) | 判断该字段方案是否有效、可用 | XSFieldScheme | | [getAllFields()](#) | 获取项目所有字段结构设置 | XSFieldScheme | | [getField()](#) | 获取项目字段元数据 | XSFieldScheme | | [getFieldBody()](#) | 获取内容字段元数据 | XSFieldScheme | | [getFieldId()](#) | 获取主键字段元数据 | XSFieldScheme | | [getFieldTitle()](#) | 获取标题字段元数据 | XSFieldScheme | | [getIterator()](#) | IteratorAggregate 接口, 以支持 foreach 遍历访问所有字段 | XSFieldScheme | | [getVnoMap()](#) | 获取所有字段的vno与名称映映射关系 | XSFieldScheme | | [logger()](#) | 获取搜索日志的字段方案 | XSFieldScheme | ### 方法明细 __toString()方法 <table class="summaryTable"><tr><td colspan="3"><div class="signature2">public void <b>__toString</b>()</div></td></tr></table> **源码:**[sdk/php/lib/XSFieldScheme.class.php#L33](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSFieldScheme.class.php#L33) (**[显示](#)**) `public function __toString() {     $str = '';     foreach ($this->_fields as $field) {         $str .= $field->toConfig() . "\n";     }     return $str; }` 将对象转换为配置文件字符串 addField()方法 <table class="summaryTable"><tr><td colspan="3"><div class="signature2">public void <b>addField</b>(mixed $field, array $config=NULL)</div></td></tr><tr><td class="paramNameCol">$field</td> <td class="paramTypeCol">mixed</td> <td class="paramDescCol">若类型为 XSFieldMeta 表示要添加的字段对象, 若类型为 string 表示字段名称, 连同 $config 参数一起创建字段对象</td></tr><tr><td class="paramNameCol">$config</td> <td class="paramTypeCol">array</td> <td class="paramDescCol">当 $field 参数为 string 时作为新建字段的配置内容</td></tr></table> **源码:**[sdk/php/lib/XSFieldScheme.class.php#L139](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSFieldScheme.class.php#L139) (**[显示](#)**) `public function addField($field, $config = null) {     if (!$field instanceof XSFieldMeta) {         $field = new XSFieldMeta($field, $config);     }     if (isset($this->_fields[$field->name])) {         throw new XSException('Duplicated field name: `' . $field->name . '\'');     }     if ($field->isSpeical()) {         if (isset($this->_typeMap[$field->type])) {             $prev = $this->_typeMap[$field->type];             throw new XSException('Duplicated ' . strtoupper($config['type']) . ' field: `' . $field->name . '\' and `' . $prev . '\'');         }         $this->_typeMap[$field->type] = $field->name;     }     $field->vno = ($field->type == XSFieldMeta::TYPE_BODY) ? self::MIXED_VNO : count($this->_vnoMap);     $this->_vnoMap[$field->vno] = $field->name;     // save field, ensure ID is the first field     if ($field->type == XSFieldMeta::TYPE_ID) {         $this->_fields = array_merge(array($field->name => $field), $this->_fields);     } else {         $this->_fields[$field->name] = $field;     } }` 添加字段到方案中每个方案中的特殊类型字段都不能重复出现 checkValid()方法 <table class="summaryTable"><tr><td colspan="3"><div class="signature2">public bool <b>checkValid</b>(bool $throw=false)</div></td></tr><tr><td class="paramNameCol">$throw</td> <td class="paramTypeCol">bool</td> <td class="paramDescCol">当没有通过检测时是否抛出异常, 默认为 false</td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">bool</td> <td class="paramDescCol">有效返回 true, 无效则返回 false</td></tr></table> **源码:**[sdk/php/lib/XSFieldScheme.class.php#L175](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSFieldScheme.class.php#L175) (**[显示](#)**) `public function checkValid($throw = false) {     if (!isset($this->_typeMap[XSFieldMeta::TYPE_ID])) {         if ($throw) {             throw new XSException('Missing field of type ID');         }         return false;     }     return true; }` 判断该字段方案是否有效、可用每个方案必须并且只能包含一个类型为 ID 的字段 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/XSFieldScheme.class.php#L117](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSFieldScheme.class.php#L117) (**[显示](#)**) `public function getAllFields() {     return $this->_fields; }` 获取项目所有字段结构设置 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">字段元数据对象, 若不存在则返回 false</td></tr></table> **源码:**[sdk/php/lib/XSFieldScheme.class.php#L93](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSFieldScheme.class.php#L93) (**[显示](#)**) `public function getField($name, $throw = true) {     if (is_int($name)) {         if (!isset($this->_vnoMap[$name])) {             if ($throw === true) {                 throw new XSException('Not exists field with vno: `' . $name . '\'');             }             return false;         }         $name = $this->_vnoMap[$name];     }     if (!isset($this->_fields[$name])) {         if ($throw === true) {             throw new XSException('Not exists field with name: `' . $name . '\'');         }         return false;     }     return $this->_fields[$name]; }` 获取项目字段元数据 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/XSFieldScheme.class.php#L77](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSFieldScheme.class.php#L77) (**[显示](#)**) `public function getFieldBody() {     if (isset($this->_typeMap[XSFieldMeta::TYPE_BODY])) {         $name = $this->_typeMap[XSFieldMeta::TYPE_BODY];         return $this->_fields[$name];     }     return false; }` 获取内容字段元数据 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/XSFieldScheme.class.php#L46](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSFieldScheme.class.php#L46) (**[显示](#)**) `public function getFieldId() {     if (isset($this->_typeMap[XSFieldMeta::TYPE_ID])) {         $name = $this->_typeMap[XSFieldMeta::TYPE_ID];         return $this->_fields[$name];     }     return false; }` 获取主键字段元数据 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/XSFieldScheme.class.php#L59](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSFieldScheme.class.php#L59) (**[显示](#)**) `public function getFieldTitle() {     if (isset($this->_typeMap[XSFieldMeta::TYPE_TITLE])) {         $name = $this->_typeMap[XSFieldMeta::TYPE_TITLE];         return $this->_fields[$name];     }     foreach ($this->_fields as $name => $field) {         if ($field->type === XSFieldMeta::TYPE_STRING && !$field->isBoolIndex()) {             return $field;         }     }     return false; }` 获取标题字段元数据 getIterator()方法 <table class="summaryTable"><tr><td colspan="3"><div class="signature2">public void <b>getIterator</b>()</div></td></tr></table> **源码:**[sdk/php/lib/XSFieldScheme.class.php#L189](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSFieldScheme.class.php#L189) (**[显示](#)**) `public function getIterator() {     return new ArrayIterator($this->_fields); }` IteratorAggregate 接口, 以支持 foreach 遍历访问所有字段 getVnoMap()方法 <table class="summaryTable"><tr><td colspan="3"><div class="signature2">public array <b>getVnoMap</b>()</div></td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">array</td> <td class="paramDescCol">vno为键, 字段名为值的数组</td></tr></table> **源码:**[sdk/php/lib/XSFieldScheme.class.php#L126](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSFieldScheme.class.php#L126) (**[显示](#)**) `public function getVnoMap() {     return $this->_vnoMap; }` 获取所有字段的vno与名称映映射关系 logger()方法 <table class="summaryTable"><tr><td colspan="3"><div class="signature2">public static XSFieldScheme <b>logger</b>()</div></td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">XSFieldScheme</td> <td class="paramDescCol">搜索日志字段方案</td></tr></table> **源码:**[sdk/php/lib/XSFieldScheme.class.php#L198](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSFieldScheme.class.php#L198) (**[显示](#)**) `public static function logger() {     if (self::$_logger === null) {         $scheme = new self;         $scheme->addField('id', array('type' => 'id'));         $scheme->addField('pinyin');         $scheme->addField('partial');         $scheme->addField('total', array('type' => 'numeric', 'index' => 'self'));         $scheme->addField('lastnum', array('type' => 'numeric', 'index' => 'self'));         $scheme->addField('currnum', array('type' => 'numeric', 'index' => 'self'));         $scheme->addField('currtag', array('type' => 'string'));         $scheme->addField('body', array('type' => 'body'));         self::$_logger = $scheme;     }     return self::$_logger; }` 获取搜索日志的字段方案 Copyright © 2008-2011 by [杭州云圣网络科技有限公司](http://www.xunsearch.com) All Rights Reserved.