es 发表于 2021-03-13 | 分类于 Tools ElasticSearch是用java开发的基于Lucene的提供RestfulAPI的开源搜索引擎,本文主要讲解索引及其模板的相关操作,如果使用默认模板,可能会导致grafna显示问题(比如时间字段格式不兼容)。 索引操作 12345678910111213141. 查看全部索引 curl -X GET 'http://elastic:ipanties6@10.226.133.85:9202/_cat/indices?v' 2. 查看索引状态 curl -X GET 'http://elastic:ipanties6@10.226.133.85:9202/_cluster/health?level=indices&pretty=true' 3. 查看索引数据 curl -X GET 'http://elastic:ipanties6@10.226.133.85:9202/pdns-*/_search?pretty=true' 4. 按条件查询数据 curl -H 'Content-Type: application/json' 'http://elastic:ipanties6@10.226.133.85:9202/pdns-private-zone-stats-1s-*/_search?pretty=true' -d '{"query" : { "match" : { "timeSeq" : 1603715829 }}}' 5. 删除匹配索引 curl -X DELETE 'http://elastic:ipanties6@10.226.133.85:9202/pdns-*' 模板操作 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611. 查询 GET _template // 查看所有模板 GET _template/temp* // 查看与通配符相匹配的模板 GET _template/temp1,temp2 // 查看多个模板 GET _template/shop_template // 查看指定模板 2. 删除 DELETE _template/shop_template // 删除上述创建的模板 3. 新增 PUT _template/shop_template { "index_patterns": [ "shop*", "bar*" ], // 可以通过"shop*"和"bar*"来适配, template字段已过期 "order": 0, // 模板的权重, 多个模板的时候优先匹配用, 值越大, 权重越高 "settings": { "number_of_shards": 1 // 分片数量, 可以定义其他配置项 }, "aliases": { "alias_1": {} // 索引对应的别名 }, "mappings": { // ES 6.0开始只支持一种type, 名称为“_doc” "_doc": { "_source": { // 是否保存字段的原始值 "enabled": false }, "properties": { // 字段的映射 "@timestamp": { // 具体的字段映射 "type": "date", "format": "yyyy-MM-dd HH:mm:ss" }, "@version": { "doc_values": true, "index": "false", // 设置为false, 不索引 "type": "text" // text类型 }, "logLevel": { "type": "long" } } } } } 4. 实例 1) 查询 curl -X GET 'http://elastic:ipanties6@10.226.133.85:9202/_template/pdns*?pretty=true' 2) 删除 curl -X DELETE 'http://elastic:ipanties6@10.226.133.85:9202/_template/dev-pdns*' 3) 新增 curl -X PUT 'http://elastic:ipanties6@10.226.133.85:9202/_template/pdns-private-zone' \ -H 'Content-Type: application/json' \ -d '{ "order": 1, "index_patterns": [ "pdns-private-zone-*" ], "settings": { "index": { "routing": { "allocation": { "total_shards_per_node": "1" } }, "refresh_interval": "30s", "number_of_shards": "1", "translog": { "flush_threshold_size": "200mb", "sync_interval": "300s", "durability": "async" }, "number_of_replicas": "0" } }, "mappings": { "data": { "_field_names": { "enabled": true }, "properties": { "vpcid": { "type": "keyword" }, "zoneid": { "type": "keyword" }, "metrics": { "properties": { "answers": { "properties": { "min": { "index": false, "type": "double" }, "max": { "index": false, "type": "double" }, "cnt": { "index": false, "type": "double" }, "sum": { "index": false, "type": "double" } } }, "answers_bits": { "properties": { "min": { "index": false, "type": "double" }, "max": { "index": false, "type": "double" }, "cnt": { "index": false, "type": "double" }, "sum": { "index": false, "type": "double" } } }, "querys": { "properties": { "min": { "index": false, "type": "double" }, "max": { "index": false, "type": "double" }, "cnt": { "index": false, "type": "double" }, "sum": { "index": false, "type": "double" } } } } }, "timestamp": { "format": "epoch_second", "type": "date" } } } } }'