[Xunsearch PHP-SDK](http://www.xunsearch.com) v1.4.8 API 參考文檔
# XSFieldMeta
[All Packages](#)| [屬性](#)| [方法(函數)](#)
| 包 | [XS](#) |
|-----|-----|
| 繼承關系 | class XSFieldMeta |
| 版本 | 1.0.0 |
| 源代碼 | [sdk/php/lib/XSFieldScheme.class.php](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSFieldScheme.class.php) |
數據字段結構元數據每個搜索項目包含若干個字段, 字段元數據保存在項目的 ini 配置文件中
### Public 屬性
[隱去繼承來的屬性](#)
| 名稱 | 類型 | 描述 | 定義于 |
|-----|-----|-----|-----|
| [cutlen](#) | int | 剪取長度 (單位:字節)用于在返回搜索結果自動剪取較長內容的字段, 默認為 0表示不截取, body 型字段默認為 300 字節 | XSFieldMeta |
| [name](#) | string | 字段名稱理論上支持各種可視字符, 推薦字符范圍:[0-9A-Za-z-_], 長度控制在 1~32 字節為宜 | XSFieldMeta |
| [type](#) | int | 字段類型 | XSFieldMeta |
| [vno](#) | int | 字段序號取值為 0~255, 同一字段方案內不能重復, 由 [XSFieldScheme::addField](#) 進行確定 | XSFieldMeta |
| [weight](#) | int | 混合區檢索時的相對權重取值范圍: 1~63, title 類型的字段默認為 5, 其它字段默認為 1 | XSFieldMeta |
### Public 方法
[隱去繼承來的方法](#)
| 名稱 | 描述 | 定義于 |
|-----|-----|-----|
| [__construct()](#) | 構造函數 | XSFieldMeta |
| [__toString()](#) | 將對象轉換為字符串 | XSFieldMeta |
| [fromConfig()](#) | 解析字段對象屬性 | XSFieldMeta |
| [getCustomTokenizer()](#) | 獲取自定義詞法分析器 | XSFieldMeta |
| [hasCustomTokenizer()](#) | 判斷當前字段是否采用自定義分詞器 | XSFieldMeta |
| [hasIndex()](#) | 判斷當前字段是否需要索引 | XSFieldMeta |
| [hasIndexMixed()](#) | 判斷當前字段是否需要在混合區索引 | XSFieldMeta |
| [hasIndexSelf()](#) | 判斷當前字段是否需要在字段區索引 | XSFieldMeta |
| [isBoolIndex()](#) | 判斷當前字段的索引是否為布爾型 | XSFieldMeta |
| [isNumeric()](#) | 判斷當前字段是否為數字型 | XSFieldMeta |
| [isSpeical()](#) | 判斷當前字段是否為特殊類型 | XSFieldMeta |
| [toConfig()](#) | 將對象轉換為配置文件字符串 | XSFieldMeta |
| [val()](#) | 把給定的值轉換為符合這個字段的數據格式 | XSFieldMeta |
| [withPos()](#) | 判斷當前字段索引是否支持短語搜索 | XSFieldMeta |
### 屬性明細
cutlen屬性
public int **$cutlen**;
剪取長度 (單位:字節)用于在返回搜索結果自動剪取較長內容的字段, 默認為 0表示不截取, body 型字段默認為 300 字節
name屬性
public string **$name**;
字段名稱理論上支持各種可視字符, 推薦字符范圍:[0-9A-Za-z-_], 長度控制在 1~32 字節為宜
type屬性
public int **$type**;
字段類型
vno屬性
public int **$vno**;
字段序號取值為 0~255, 同一字段方案內不能重復, 由 [XSFieldScheme::addField](#) 進行確定
weight屬性
public int **$weight**;
混合區檢索時的相對權重取值范圍: 1~63, title 類型的字段默認為 5, 其它字段默認為 1
### 方法明細
__construct()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public void <b>__construct</b>(string $name, array $config=NULL)</div></td></tr><tr><td class="paramNameCol">$name</td> <td class="paramTypeCol">string</td> <td class="paramDescCol">字段名稱</td></tr><tr><td class="paramNameCol">$config</td> <td class="paramTypeCol">array</td> <td class="paramDescCol">可選參數, 初始化字段各項配置</td></tr></table>
**源碼:**[sdk/php/lib/XSFieldScheme.class.php#L298](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSFieldScheme.class.php#L298) (**[顯示](#)**)
`public?function?__construct($name,?$config?=?null)
{
????$this->name?=?strval($name);
????if?(is_array($config))?{
????????$this->fromConfig($config);
????}
}`
構造函數
__toString()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public string <b>__toString</b>()</div></td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">string</td> <td class="paramDescCol">字段名稱</td></tr></table>
**源碼:**[sdk/php/lib/XSFieldScheme.class.php#L310](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSFieldScheme.class.php#L310) (**[顯示](#)**)
`public?function?__toString()
{
????return?$this->name;
}`
將對象轉換為字符串
fromConfig()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public void <b>fromConfig</b>(array $config)</div></td></tr><tr><td class="paramNameCol">$config</td> <td class="paramTypeCol">array</td> <td class="paramDescCol">原始配置屬性數組</td></tr></table>
**源碼:**[sdk/php/lib/XSFieldScheme.class.php#L514](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSFieldScheme.class.php#L514) (**[顯示](#)**)
`public?function?fromConfig($config)
{
????//?type?&?default?setting
????if?(isset($config['type']))?{
????????$predef?=?'self::TYPE_'?.?strtoupper($config['type']);
????????if?(defined($predef))?{
????????????$this->type?=?constant($predef);
????????????if?($this->type?==?self::TYPE_ID)?{
????????????????$this->flag?=?self::FLAG_INDEX_SELF;
????????????????$this->tokenizer?=?'full';
????????????}?elseif?($this->type?==?self::TYPE_TITLE)?{
????????????????$this->flag?=?self::FLAG_INDEX_BOTH?|?self::FLAG_WITH_POSITION;
????????????????$this->weight?=?5;
????????????}?elseif?($this->type?==?self::TYPE_BODY)?{
????????????????$this->vno?=?XSFieldScheme::MIXED_VNO;
????????????????$this->flag?=?self::FLAG_INDEX_SELF?|?self::FLAG_WITH_POSITION;
????????????????$this->cutlen?=?300;
????????????}
????????}
????}
????//?index?flag
????if?(isset($config['index'])?&&?$this->type?!=?self::TYPE_BODY)?{
????????$predef?=?'self::FLAG_INDEX_'?.?strtoupper($config['index']);
????????if?(defined($predef))?{
????????????$this->flag?&=?~?self::FLAG_INDEX_BOTH;
????????????$this->flag?|=?constant($predef);
????????}
????????if?($this->type?==?self::TYPE_ID)?{
????????????$this->flag?|=?self::FLAG_INDEX_SELF;
????????}
????}
????//?others
????if?(isset($config['cutlen']))?{
????????$this->cutlen?=?intval($config['cutlen']);
????}
????if?(isset($config['weight'])?&&?$this->type?!=?self::TYPE_BODY)?{
????????$this->weight?=?intval($config['weight'])?&?self::MAX_WDF;
????}
????if?(isset($config['phrase']))?{
????????if?(!strcasecmp($config['phrase'],?'yes'))?{
????????????$this->flag?|=?self::FLAG_WITH_POSITION;
????????}?elseif?(!strcasecmp($config['phrase'],?'no'))?{
????????????$this->flag?&=?~?self::FLAG_WITH_POSITION;
????????}
????}
????if?(isset($config['non_bool']))?{
????????if?(!strcasecmp($config['non_bool'],?'yes'))?{
????????????$this->flag?|=?self::FLAG_NON_BOOL;
????????}?elseif?(!strcasecmp($config['non_bool'],?'no'))?{
????????????$this->flag?&=?~?self::FLAG_NON_BOOL;
????????}
????}
????if?(isset($config['tokenizer'])?&&?$this->type?!=?self::TYPE_ID
????????????&&?$config['tokenizer']?!=?'default')?{
????????$this->tokenizer?=?$config['tokenizer'];
????}
}`
解析字段對象屬性
getCustomTokenizer()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public <a href="XSTokenizer.html">XSTokenizer</a> <b>getCustomTokenizer</b>()</div></td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol"><a href="XSTokenizer.html">XSTokenizer</a></td> <td class="paramDescCol">獲取當前字段的自定義詞法分析器</td></tr></table>
**源碼:**[sdk/php/lib/XSFieldScheme.class.php#L414](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSFieldScheme.class.php#L414) (**[顯示](#)**)
`public?function?getCustomTokenizer()
{
????if?(isset(self::$_tokenizers[$this->tokenizer]))?{
????????return?self::$_tokenizers[$this->tokenizer];
????}?else?{
????????if?(($pos1?=?strpos($this->tokenizer,?'('))?!==?false
????????????????&&?($pos2?=?strrpos($this->tokenizer,?')',?$pos1?+?1)))?{
????????????$name?=?'XSTokenizer'?.?ucfirst(trim(substr($this->tokenizer,?0,?$pos1)));
????????????$arg?=?substr($this->tokenizer,?$pos1?+?1,?$pos2?-?$pos1?-?1);
????????}?else?{
????????????$name?=?'XSTokenizer'?.?ucfirst($this->tokenizer);
????????????$arg?=?null;
????????}
????????if?(!class_exists($name))?{
????????????$file?=?$name?.?'.class.php';
????????????if?(file_exists($file))?{
????????????????require_once?$file;
????????????}?else?if?(file_exists(XS_LIB_ROOT?.?DIRECTORY_SEPARATOR?.?$file))?{
????????????????require_once?XS_LIB_ROOT?.?DIRECTORY_SEPARATOR?.?$file;
????????????}
????????????if?(!class_exists($name))?{
????????????????throw?new?XSException('Undefined?custom?tokenizer?`'?.?$this->tokenizer?.?'\'?for?field?`'?.?$this->name?.?'\'');
????????????}
????????}
????????$obj?=?$arg?===?null???new?$name?:?new?$name($arg);
????????if?(!$obj?instanceof?XSTokenizer)?{
????????????throw?new?XSException($name?.?'?for?field?`'?.?$this->name?.?'\'?dose?not?implement?the?interface:?XSTokenizer');
????????}
????????self::$_tokenizers[$this->tokenizer]?=?$obj;
????????return?$obj;
????}
}`
獲取自定義詞法分析器自 1.4.8 起會自動加載 lib 或當前目錄下的 XSTokenizer???.class.php
hasCustomTokenizer()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public bool <b>hasCustomTokenizer</b>()</div></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#L403](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSFieldScheme.class.php#L403) (**[顯示](#)**)
`public?function?hasCustomTokenizer()
{
????return?($this->tokenizer?!==?XSTokenizer::DFL);
}`
判斷當前字段是否采用自定義分詞器
hasIndex()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public bool <b>hasIndex</b>()</div></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#L376](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSFieldScheme.class.php#L376) (**[顯示](#)**)
`public?function?hasIndex()
{
????return?($this->flag?&?self::FLAG_INDEX_BOTH)???true?:?false;
}`
判斷當前字段是否需要索引
hasIndexMixed()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public bool <b>hasIndexMixed</b>()</div></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#L385](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSFieldScheme.class.php#L385) (**[顯示](#)**)
`public?function?hasIndexMixed()
{
????return?($this->flag?&?self::FLAG_INDEX_MIXED)???true?:?false;
}`
判斷當前字段是否需要在混合區索引
hasIndexSelf()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public bool <b>hasIndexSelf</b>()</div></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#L394](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSFieldScheme.class.php#L394) (**[顯示](#)**)
`public?function?hasIndexSelf()
{
????return?($this->flag?&?self::FLAG_INDEX_SELF)???true?:?false;
}`
判斷當前字段是否需要在字段區索引
isBoolIndex()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public bool <b>isBoolIndex</b>()</div></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#L345](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSFieldScheme.class.php#L345) (**[顯示](#)**)
`public?function?isBoolIndex()
{
????if?($this->flag?&?self::FLAG_NON_BOOL)?{
????????return?false;
????}
????return?(!$this->hasIndex()?||?$this->tokenizer?!==?XSTokenizer::DFL);
}`
判斷當前字段的索引是否為布爾型目前只有內置分詞器支持語法型索引, 自 1.0.1 版本起把非索引字段也視為布爾便于判斷
isNumeric()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public bool <b>isNumeric</b>()</div></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#L357](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSFieldScheme.class.php#L357) (**[顯示](#)**)
`public?function?isNumeric()
{
????return?($this->type?==?self::TYPE_NUMERIC);
}`
判斷當前字段是否為數字型
isSpeical()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public bool <b>isSpeical</b>()</div></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#L367](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSFieldScheme.class.php#L367) (**[顯示](#)**)
`public?function?isSpeical()
{
????return?($this->type?==?self::TYPE_ID?||?$this->type?==?self::TYPE_TITLE?||?$this->type?==?self::TYPE_BODY);
}`
判斷當前字段是否為特殊類型特殊類型的字段是指 id, title, body, 每個項目至多只能有一個這種類型的字段
toConfig()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public string <b>toConfig</b>()</div></td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">string</td> <td class="paramDescCol">轉換后的配置文件字符串</td></tr></table>
**源碼:**[sdk/php/lib/XSFieldScheme.class.php#L452](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSFieldScheme.class.php#L452) (**[顯示](#)**)
`public?function?toConfig()
{
????//?type
????$str?=?"["?.?$this->name?.?"]\n";
????if?($this->type?===?self::TYPE_NUMERIC)?{
????????$str?.=?"type?=?numeric\n";
????}?elseif?($this->type?===?self::TYPE_DATE)?{
????????$str?.=?"type?=?date\n";
????}?elseif?($this->type?===?self::TYPE_ID)?{
????????$str?.=?"type?=?id\n";
????}?elseif?($this->type?===?self::TYPE_TITLE)?{
????????$str?.=?"type?=?title\n";
????}?elseif?($this->type?===?self::TYPE_BODY)?{
????????$str?.=?"type?=?body\n";
????}
????//?index
????if?($this->type?!==?self::TYPE_BODY?&&?($index?=?($this->flag?&?self::FLAG_INDEX_BOTH)))?{
????????if?($index?===?self::FLAG_INDEX_BOTH)?{
????????????if?($this->type?!==?self::TYPE_TITLE)?{
????????????????$str?.=?"index?=?both\n";
????????????}
????????}?elseif?($index?===?self::FLAG_INDEX_MIXED)?{
????????????$str?.=?"index?=?mixed\n";
????????}?else?{
????????????if?($this->type?!==?self::TYPE_ID)?{
????????????????$str?.=?"index?=?self\n";
????????????}
????????}
????}
????//?tokenizer
????if?($this->type?!==?self::TYPE_ID?&&?$this->tokenizer?!==?XSTokenizer::DFL)?{
????????$str?.=?"tokenizer?=?"?.?$this->tokenizer?.?"\n";
????}
????//?cutlen
????if?($this->cutlen?>?0?&&?!($this->cutlen?===?300?&&?$this->type?===?self::TYPE_BODY))?{
????????$str?.=?"cutlen?=?"?.?$this->cutlen?.?"\n";
????}
????//?weight
????if?($this->weight?!==?1?&&?!($this->weight?===?5?&&?$this->type?===?self::TYPE_TITLE))?{
????????$str?.=?"weight?=?"?.?$this->weight?.?"\n";
????}
????//?phrase
????if?($this->flag?&?self::FLAG_WITH_POSITION)?{
????????if?($this->type?!==?self::TYPE_BODY?&&?$this->type?!==?self::TYPE_TITLE)?{
????????????$str?.=?"phrase?=?yes\n";
????????}
????}?else?{
????????if?($this->type?===?self::TYPE_BODY?||?$this->type?===?self::TYPE_TITLE)?{
????????????$str?.=?"phrase?=?no\n";
????????}
????}
????//?non-bool
????if?($this->flag?&?self::FLAG_NON_BOOL)?{
????????$str?.=?"non_bool?=?yes\n";
????}
????return?$str;
}`
將對象轉換為配置文件字符串
val()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public mixed <b>val</b>(mixed $value)</div></td></tr><tr><td class="paramNameCol">$value</td> <td class="paramTypeCol">mixed</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/XSFieldScheme.class.php#L320](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSFieldScheme.class.php#L320) (**[顯示](#)**)
`public?function?val($value)
{
????if?($this->type?==?self::TYPE_DATE)?{
????????//?日期類型:?轉換成專用的?YYYYmmdd?格式
????????if?(!is_numeric($value)?||?strlen($value)?!==?8)?{
????????????$value?=?date('Ymd',?is_numeric($value)???$value?:?strtotime($value));
????????}
????}
????return?$value;
}`
把給定的值轉換為符合這個字段的數據格式
withPos()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public bool <b>withPos</b>()</div></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#L335](https://github.com/hightman/xunsearch/blob/master/sdk/php/lib/XSFieldScheme.class.php#L335) (**[顯示](#)**)
`public?function?withPos()
{
????return?($this->flag?&?self::FLAG_WITH_POSITION)???true?:?false;
}`
判斷當前字段索引是否支持短語搜索
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 授權許可證