ElasticSearch索引自定义类型

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

Rest API设置自定义索引


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

<pre name="code" class="javascript"> curl -XPUT http://localhost:9200/test_index/test_type/1 -d  '{"ip":"127.0.0.1"}'


查看映射结果:

{

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

可以看出IP的类型为string类型,而并非我们想要的IP类型。


映射一旦建好,不能修改,所以必须删除再重新创建并自定义索引。



重新索引设置IP字段的类型

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


查看映射:


发现ip字段已经被设置为ip。





原文地址:https://www.cnblogs.com/pilihaotian/p/8823140.html