映射
为了能够把日期字段处理成日期,把数字字段处理成数字,把字符串字段处理成全文本(Full-text)或精确(Exact-value)的字符串值,Elasticsearch需要知道每个字段里面都包含什么数据类型。这些类型和字段的信息存储在映射中
创建索引的时候,可以预先定义字段的类型以及相关属性,相当于定义数据库字段的属性
字段类型设置
{
"type": "string", //字符串类型
"index": "analyzed", //分词,不分词是:not_analyzed ,设置成no,字段将不会被索引
"analyzer":"ik", //指定分词器
"boost":1.23, //字段级别的分数加权
"doc_values":false, //对not_analyzed字段,默认都是开启,分词字段不能使用,对排序和聚合能提升较大性能,节约内存
"fielddata":{"format":"disabled"}, //针对分词字段,参与排序或聚合时能提高性能,不分词字段统一建议使用doc_value
"fields":{"raw":{"type":"string","index":"not_analyzed"}}, //可以对一个字段提供多种索引模式,同一个字段的值,一个分词,一个不分词
"ignore_above":100, //超过100个字符的文本,将会被忽略,不被索引
"include_in_all":ture, //设置是否此字段包含在_all字段中,默认是true,除非index设置成no选项
"index_options":"docs", //4个可选参数docs(索引文档号) ,freqs(文档号+词频),positions(文档号+词频+位置,通常用来距离查询),offsets(文档号+词频+位置+偏移量,通常被使用在高亮字段)分词字段默认是position,其他的默认是docs
"norms":{"enable":true,"loading":"lazy"}, //分词字段默认配置,不分词字段:默认{"enable":false},存储长度因子和索引时boost,建议对需要参与评分字段使用 ,会额外增加内存消耗量
"null_value":"NULL", //设置一些缺失字段的初始化值,只有string可以使用,分词字段的null值也会被分词
"position_increament_gap":0, //影响距离查询或近似查询,可以设置在多值字段的数据上火分词字段上,查询时可指定slop间隔,默认值是100
"store":false, //是否单独设置此字段的是否存储而从_source字段中分离,默认是false,只能搜索,不能获取值
"search_analyzer":"ik", //设置搜索时的分词器,默认跟ananlyzer是一致的,比如index时用standard+ngram,搜索时用standard用来完成自动提示功能
"similarity":"BM25"//默认是TF/IDF算法,指定一个字段评分策略,仅仅对字符串型和分词类型有效
"term_vector":"no"//默认不存储向量信息,支持参数yes(term存储),with_positions(term+位置),with_offsets(term+偏移量),with_positions_offsets(term+位置+偏移量) 对快速高亮fast vector highlighter能提升性能,但开启又会加大索引体积,不适合大数据量用
}
1、添加Mapping
$esService = new EsService();
$params = [
'index' => env('ES_INDEX'),
'body' => [
'mappings' => [
'stat_user_show' => [ //类型
'properties' => [
'id' => ['type' => 'integer'],//ID
'user_id' => ['type' => 'integer'],//用户ID
'user_unique' => ['type' => 'string', 'index' => 'not_analyzed'],//用户唯一码
'app_client' => ['type' => 'integer'],//产品端线
'time' => ['type' => 'date', 'format' => 'epoch_second', 'index' => 'not_analyzed'],//展示时间
]
]
],
]
];
$result = $esService->create($params);
2、查看Mapping
$esService = new EsService();
$result = $esService->getMapping();
3、追加Mapping中的字段
$esService = new EsService();
$body = [
'properties' => [
'status' => ['type' => 'integer'],//状态
]
];
$result = $esService->setType('stat_user_click')->putMapping($body);