ES索引自定义类型

ES可以自动检测字段并设置映射类型。如果设置的索引类型不是我们所需要的,我们可以自行定义。

Rest API设置自定义索引

首先通过ES自动映射一个IP地址的字段的类型:

1
curl -XPUT http://localhost:9200/test_index/test_type/1 -d  '{"ip":"127.0.0.1"}'
APACHE

查看映射结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{

"test_index": {
"mappings": {
"test_type": {
"properties": {
"ip": {
"type": "string"
}
}
}
}
}
}
JSON

可以看出IP的类型为string类型,而并非我们想要的IP类型。
映射一旦建好,不能修改,所以必须删除再重新创建并自定义索引。
重新索引设置IP字段的类型:

1
curl -XPUT localhost:9200/test_index  ?pretty -d '{"mappings":{"_default_":{"properties":{"ip":{"type":"ip"}}}}}'
SCILAB

查看映射,发现ip字段已经被设置为ip。


ES索引自定义类型
https://leehoward.cn/2019/10/16/ES索引自定义类型/
作者
lihao
发布于
2019年10月16日
许可协议