创建索引
对比关系型数据库,创建索引就等同于创建数据库。
需要记住的是,创建索引需要使用 PUT
请求方式,且该类型的请求是幂等的,也就意味着,无法多次创建相同名称的索引!
创建一个名称为 test_index
的索引:
# 在 kibana 的 Dev Tools 对话框里,只需要这么写,就可以创建该索引了
PUT test_index
# 返回:
{
# 响应结果
"acknowledged" : true,
# 分片结果
"shards_acknowledged" : true,
# 索引名称
"index" : "test_index"
}
# 如果重复添加索引,会返回错误信息
{
"error" : {
"root_cause" : [
{
"type" : "resource_already_exists_exception",
"reason" : "index [test_index/c6nJovSRSjSTTkpIgFy4-g] already exists",
"index_uuid" : "c6nJovSRSjSTTkpIgFy4-g",
"index" : "test_index"
}
],
"type" : "resource_already_exists_exception",
"reason" : "index [test_index/c6nJovSRSjSTTkpIgFy4-g] already exists",
"index_uuid" : "c6nJovSRSjSTTkpIgFy4-g",
"index" : "test_index"
},
"status" : 400
}
题外话,使用 PostMan工具或者在 Head插件上都是可以正常操作的(后续都在 kibana中操作演示)。如:
# 选择 PUT请求方式
http:192.168.137.223:9200/test_index
查看索引
可以 Elasticsearch Head插件页面上看到:
也可以向 ES 服务器发 GET 请求,查看索引信息:
# 查看指定名称的索引,如 test_index
GET test_index
# 结果为
{
# 索引名
"test_index" : {
# 别名
"aliases" : { },
# 映射
"mappings" : { },
# 设置
"settings" : {
# 设置 - 索引
"index" : {
"routing" : {
"allocation" : {
"include" : {
"_tier_preference" : "data_content"
}
}
},
# 设置 - 索引 - 主分片数量
"number_of_shards" : "1",
# 设置 - 索引 - 名称
"provided_name" : "test_index",
# 设置 - 索引 - 创建时间
"creation_date" : "1621175229003",
# 设置 - 索引 - 副分片数量
"number_of_replicas" : "1",
设置 - 索引 - 编号
"uuid" : "bRJGP3RSQIaYxqLxmZRkYg",
# 设置 - 索引 - 版本
"version" : {
"created" : "7120099"
}
}
}
}
}
# 查看 ES下所有的索引,_cat 表示查看的意思,indices 表示索引
GET _cat/indices?v
# 结果为
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open test_index bRJGP3RSQIaYxqLxmZRkYg 1 1 0 0 208b 208b
上述查看所有索引返回的列表具体含义是:
表头 | 含义 |
---|---|
health | 当前服务器健康状态: green(集群完整), yellow(单点正常、集群不完整), red(单点不正常) |
status | 索引打开、关闭状态 |
index | 索引名 |
uuid | 索引统一编号 |
pri | 主分片数量 |
rep | 副本数量 |
docs.count | 可用文档数量 |
docs.deleted | 文档删除状态(逻辑删除) |
store.size | 主分片和副分片整体占空间大小 |
pri.store.size | 主分片占空间大小 |
删除索引
删除索引也很简单,遵循 RestFul风格,只需要将请求类型改为 DELETE
即可。
DELETE test_index</pre>
示例,创建一个索引:
curl -XPUT 'http://10.123.7.3:9200/test_index' -H 'Content-Type: application/json' -d '{
"settings": {
"index": {
"number_of_shards": "5",
"number_of_replicas": "1",
"max_result_window" :"60000"
}
},
"mappings": {
"wellenRoute": {
"properties": {
"wellenId": {
"type": "integer"
},
"wellenCode": {
"type": "keyword"
},
"nationalLineType": {
"type": "integer"
},
"wellenType": {
"type": "integer"
},
"wellenStatus": {
"type": "integer"
},
"issueTime": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||epoch_millis"
},
"issueUser": {
"type": "keyword"
},
"pickType": {
"type": "integer"
},
"priorityLevel": {
"type": "integer"
},
"operatorGroupName": {
"type": "keyword"
},
"batchNum": {
"type": "integer",
"index": false
},
"packageNum": {
"type": "integer",
"index": false
},
"taskNum": {
"type": "long",
"index": false
},
"shouldPickNum": {
"type": "long",
"index": false
},
"realPickNum": {
"type": "long",
"index": false
},
"isCombine": {
"type": "boolean"
},
"packType": {
"type": "boolean"
},
"combineWellenType": {
"type": "keyword"
},
"combineAddress": {
"type": "integer"
},
"wellenMark": {
"type": "integer"
},
"wellenMarkStr": {
"type": "keyword"
},
"downwindAreaId": {
"type": "integer"
},
"collectTips": {
"type": "keyword"
},
"isAbc": {
"type": "integer"
},
"wellenConfigId": {
"type": "integer"
},
"wellenConfigName": {
"type": "keyword"
},
"coWellenCode": {
"type": "keyword"
},
"warehouseId": {
"type": "integer"
},
"subWarehouseIdList": {
"type": "integer",
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
}
},
"createTime": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||epoch_millis"
},
"lastUpdateTime": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||epoch_millis"
},
"packageLabelNumMark": {
"type": "boolean"
},
"createType": {
"type": "integer"
},
"mainCollectionUser": {
"type": "keyword"
},
"mainCollectionTime": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||epoch_millis"
},
"firstSowingStartTime": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||epoch_millis"
},
"firstSowingEndTime": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||epoch_millis"
},
"secondSowingStartTime": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||epoch_millis"
},
"secondSowingEndTime": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||epoch_millis"
},
"firstSowingUserName": {
"type": "keyword"
},
"operateSubWarehouseId": {
"type": "integer"
},
"areaId": {
"type": "keyword"
},
"isDelete": {
"type": "boolean"
},
"paramIntegerOne": {
"type": "integer"
},
"paramIntegerTwo": {
"type": "integer"
},
"paramLongOne": {
"type": "long"
},
"paramLongTwo": {
"type": "long"
},
"paramStringOne": {
"type": "keyword"
},
"paramStringTwo": {
"type": "keyword"
},
"paramStringThree": {
"type": "keyword"
},
"paramStringFour": {
"type": "keyword"
},
"paramDateOne": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||epoch_millis"
},
"paramDateTwo": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||epoch_millis"
}
}
}
}
}'