[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.
- 權威指南
- 新手上路
- 最新主要變動
- 概述
- 關于 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 授權許可證