包 | XS |
---|---|
继承关系 | class XSFieldMeta |
版本 | 1.0.0 |
源代码 | sdk/php/lib/XSFieldScheme.class.php |
名称 | 类型 | 描述 | 定义于 |
---|---|---|---|
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 |
名称 | 描述 | 定义于 |
---|---|---|
__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 |
剪取长度 (单位:字节) 用于在返回搜索结果自动剪取较长内容的字段, 默认为 0表示不截取, body 型字段默认为 300 字节
字段名称 理论上支持各种可视字符, 推荐字符范围:[0-9A-Za-z-_], 长度控制在 1~32 字节为宜
字段类型
字段序号 取值为 0~255, 同一字段方案内不能重复, 由 XSFieldScheme::addField 进行确定
混合区检索时的相对权重 取值范围: 1~63, title 类型的字段默认为 5, 其它字段默认为 1
public void __construct(string $name, array $config=NULL)
| ||
$name | string | 字段名称 |
$config | array | 可选参数, 初始化字段各项配置 |
public function __construct($name, $config = null)
{
$this->name = strval($name);
if (is_array($config)) {
$this->fromConfig($config);
}
}
构造函数
public string __toString()
| ||
{return} | string | 字段名称 |
public function __toString()
{
return $this->name;
}
将对象转换为字符串
public void fromConfig(array $config)
| ||
$config | array | 原始配置属性数组 |
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'];
}
}
解析字段对象属性
public XSTokenizer getCustomTokenizer()
| ||
{return} | XSTokenizer | 获取当前字段的自定义词法分析器 |
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
public bool hasCustomTokenizer()
| ||
{return} | bool | 是返回 true, 不是返回 false |
public function hasCustomTokenizer()
{
return ($this->tokenizer !== XSTokenizer::DFL);
}
判断当前字段是否采用自定义分词器
public bool hasIndex()
| ||
{return} | bool | 若需要返回 true, 不需要则返回 false |
public function hasIndex()
{
return ($this->flag & self::FLAG_INDEX_BOTH) ? true : false;
}
判断当前字段是否需要索引
public bool hasIndexMixed()
| ||
{return} | bool | 若需要返回 true, 不需要则返回 false |
public function hasIndexMixed()
{
return ($this->flag & self::FLAG_INDEX_MIXED) ? true : false;
}
判断当前字段是否需要在混合区索引
public bool hasIndexSelf()
| ||
{return} | bool | 若需要返回 true, 不需要则返回 false |
public function hasIndexSelf()
{
return ($this->flag & self::FLAG_INDEX_SELF) ? true : false;
}
判断当前字段是否需要在字段区索引
public bool isBoolIndex()
| ||
{return} | bool | 是返回 true, 不是返回 false |
public function isBoolIndex()
{
if ($this->flag & self::FLAG_NON_BOOL) {
return false;
}
return (!$this->hasIndex() || $this->tokenizer !== XSTokenizer::DFL);
}
判断当前字段的索引是否为布尔型 目前只有内置分词器支持语法型索引, 自 1.0.1 版本起把非索引字段也视为布尔便于判断
public bool isNumeric()
| ||
{return} | bool | 是返回 true, 不是返回 false |
public function isNumeric()
{
return ($this->type == self::TYPE_NUMERIC);
}
判断当前字段是否为数字型
public bool isSpeical()
| ||
{return} | bool | 是返回 true, 不是返回 false |
public function isSpeical()
{
return ($this->type == self::TYPE_ID || $this->type == self::TYPE_TITLE || $this->type == self::TYPE_BODY);
}
判断当前字段是否为特殊类型 特殊类型的字段是指 id, title, body, 每个项目至多只能有一个这种类型的字段
public string toConfig()
| ||
{return} | string | 转换后的配置文件字符串 |
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;
}
将对象转换为配置文件字符串
public mixed val(mixed $value)
| ||
$value | mixed | 原值 |
{return} | mixed | 转换后的值 |
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;
}
把给定的值转换为符合这个字段的数据格式
public bool withPos()
| ||
{return} | bool | 是返回 true, 不是返回 false |
public function withPos()
{
return ($this->flag & self::FLAG_WITH_POSITION) ? true : false;
}
判断当前字段索引是否支持短语搜索
包 | XS |
---|---|
继承关系 | class XSFieldMeta |
版本 | 1.0.0 |
源代码 | sdk/php/lib/XSFieldScheme.class.php |
名称 | 类型 | 描述 | 定义于 |
---|---|---|---|
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 |
名称 | 描述 | 定义于 |
---|---|---|
__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 |
剪取长度 (单位:字节) 用于在返回搜索结果自动剪取较长内容的字段, 默认为 0表示不截取, body 型字段默认为 300 字节
字段名称 理论上支持各种可视字符, 推荐字符范围:[0-9A-Za-z-_], 长度控制在 1~32 字节为宜
字段类型
字段序号 取值为 0~255, 同一字段方案内不能重复, 由 XSFieldScheme::addField 进行确定
混合区检索时的相对权重 取值范围: 1~63, title 类型的字段默认为 5, 其它字段默认为 1
public void __construct(string $name, array $config=NULL)
| ||
$name | string | 字段名称 |
$config | array | 可选参数, 初始化字段各项配置 |
public function __construct($name, $config = null)
{
$this->name = strval($name);
if (is_array($config)) {
$this->fromConfig($config);
}
}
构造函数
public string __toString()
| ||
{return} | string | 字段名称 |
public function __toString()
{
return $this->name;
}
将对象转换为字符串
public void fromConfig(array $config)
| ||
$config | array | 原始配置属性数组 |
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'];
}
}
解析字段对象属性
public XSTokenizer getCustomTokenizer()
| ||
{return} | XSTokenizer | 获取当前字段的自定义词法分析器 |
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
public bool hasCustomTokenizer()
| ||
{return} | bool | 是返回 true, 不是返回 false |
public function hasCustomTokenizer()
{
return ($this->tokenizer !== XSTokenizer::DFL);
}
判断当前字段是否采用自定义分词器
public bool hasIndex()
| ||
{return} | bool | 若需要返回 true, 不需要则返回 false |
public function hasIndex()
{
return ($this->flag & self::FLAG_INDEX_BOTH) ? true : false;
}
判断当前字段是否需要索引
public bool hasIndexMixed()
| ||
{return} | bool | 若需要返回 true, 不需要则返回 false |
public function hasIndexMixed()
{
return ($this->flag & self::FLAG_INDEX_MIXED) ? true : false;
}
判断当前字段是否需要在混合区索引
public bool hasIndexSelf()
| ||
{return} | bool | 若需要返回 true, 不需要则返回 false |
public function hasIndexSelf()
{
return ($this->flag & self::FLAG_INDEX_SELF) ? true : false;
}
判断当前字段是否需要在字段区索引
public bool isBoolIndex()
| ||
{return} | bool | 是返回 true, 不是返回 false |
public function isBoolIndex()
{
if ($this->flag & self::FLAG_NON_BOOL) {
return false;
}
return (!$this->hasIndex() || $this->tokenizer !== XSTokenizer::DFL);
}
判断当前字段的索引是否为布尔型 目前只有内置分词器支持语法型索引, 自 1.0.1 版本起把非索引字段也视为布尔便于判断
public bool isNumeric()
| ||
{return} | bool | 是返回 true, 不是返回 false |
public function isNumeric()
{
return ($this->type == self::TYPE_NUMERIC);
}
判断当前字段是否为数字型
public bool isSpeical()
| ||
{return} | bool | 是返回 true, 不是返回 false |
public function isSpeical()
{
return ($this->type == self::TYPE_ID || $this->type == self::TYPE_TITLE || $this->type == self::TYPE_BODY);
}
判断当前字段是否为特殊类型 特殊类型的字段是指 id, title, body, 每个项目至多只能有一个这种类型的字段
public string toConfig()
| ||
{return} | string | 转换后的配置文件字符串 |
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;
}
将对象转换为配置文件字符串
public mixed val(mixed $value)
| ||
$value | mixed | 原值 |
{return} | mixed | 转换后的值 |
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;
}
把给定的值转换为符合这个字段的数据格式
public bool withPos()
| ||
{return} | bool | 是返回 true, 不是返回 false |
public function withPos()
{
return ($this->flag & self::FLAG_WITH_POSITION) ? true : false;
}
判断当前字段索引是否支持短语搜索
留下一条评论吧!
请到论坛 登录 后刷新本页面!