es

ElasticSearch是用java开发的基于Lucene的提供RestfulAPI的开源搜索引擎,本文主要讲解索引及其模板的相关操作,如果使用默认模板,可能会导致grafna显示问题(比如时间字段格式不兼容)。

索引操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
1. 查看全部索引
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-*'      

模板操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
1. 查询
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"
        }
      }
    }
  }
}'