# 创建索引库
# 配置字段
索引库相当于数据库的表,创建索引库(表)需要配置字段;
字段名称只能以字母开头,且名称只能包含数字、字母、下划线,字母均要小写;
所有字段,无论是否配置了索引,都必须配置数据类型。
$mapping = [
//索引库名称 类似于表名称
'name' => $indexName,
// 配置业务字段
'field' => [
[
'name' => 'id',// 主键名称 主键必须设置
'type' => 'primarykey', //数据类型为主键 必须设置
'primarykey_type' => 'Int_Incremental', // int递增
],
[
'name' => 'title',
'type' => 'text', //数据类型:文本
],
[
'name' => 'descr',
'type' => 'text', //数据类型:文本
],
[
'name' => 'location',
'type' => 'geo_point', // 数据类型:geo_point 经纬度数据类型
],
]
];
# 配置字段索引
当业务字段设置为'index' => true,
时,代表此字段需要被索引,不设置或为false,则不会被索引,且只有text
、keyword
、geo_point
类型,可以配置索引
$mapping = [
// 索引库名称 类似于表名称
'name' => $indexName,
// 配置业务字段
'field' => [
[
'name' => 'id',// 主键名称 主键必须设置
'type' => 'primarykey', //数据类型为主键 必须设置
'primarykey_type' => 'Int_Incremental', // int递增
],
[
'name' => 'title',
'type' => 'text', //数据类型:文本类型,会根据analyzer配置的方式进行分词
'index' => true, // 会被索引
],
[
'name' => 'descr', //不会被索引
'type' => 'text',
],
[
'name' => 'tags',
'type' => 'keyword',
'index' => true, // 会被索引
],
[
'name' => 'location',
'type'=>'geo_point',// 数据类型:geo_point
'index' => true, // 会被索引
],
]
];
# 配置字段分词方式
只有text类型,可以配置分词方式,其它类型配置无效。
分词模式:
- 为空/complete/not:代表不分词,会进行整体匹配
- segment:中文分词
- ngram:ngram分词
- separator:分隔符分词
// 第一种分词方式
'name' => 'title',
'index' => true,
'type'=>'text',// 数据类型:文本类型
'analyzer' => 'segment', // 中文分词
// 第二种分词方式
'name' => 'title',
'index' => true,
'type'=>'text',// 数据类型:文本类型
'analyzer' => [ // ngram分词
'ngram' => 3,// 窗口长度为3个字符
// 或者
'ngram' => [2,4],// 代表窗口长度分别为2,3,4
],
// 第三种分词方式
'name' => 'title',
'index' => true,
'type'=>'text',// 数据类型:文本类型
'analyzer' => [
'separator' => ',',// 分隔符模式,这里的示例,会以逗号切分
],
keyword类型,无法配置分词方式,只会被整体索引
'name' => 'tags',
'index' => true,// 会被索引
'type'=>'keyword',// 数据类型:关键词 不会分词,只会整体索引,analyzer配置无效
# 完整mapping
$mapping = [
//设置索引库的名称,比如对应的表名
'name' => 'test',
// 字段配置
'field' => [
[
'name' => 'id',// 主键名称 主键必须设置
'type' => 'primarykey', //数据类型为主键 必须设置
'primarykey_type' => 'Int_Incremental', // int递增
],
[
'name' => 'title',
'index' => true,
'type' => 'text',
'analyzer' => 'segment',
],
[
'name' => 'tags',
'index' => true,
'type' => 'keyword',
],
[
'name' => 'location',
'index' => true,
'type' => 'geo_point'
],
[
'name' => 'score',
'type' => 'numeric',
],
[
'name' => 'time',
'type' => 'date'
],
[
'name' => 'descr',
'type' => 'text',
],
[
'name' => 'link',
'type' => 'text',
],
]
];
// 实例化对象
$Wind = new \WindSearch\Index\Wind('test'); //test 当前索引库的名称
//检查是否存在此索引库
$is_index = $Wind->checkIndex();
// 如果存在此索引库
if ($is_index) {
//删除索引库
$Wind->delIndex();
}
//创建索引库
$Wind->createIndex($mapping);