多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[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.