记录下一些常见操作。。。
删除索引
#删除索引 kibana 方式
DELETE /my_index
#删除索引 curl 方式
curl -XDELETE 'http://127.0.0.1:9200/my_index'
删除索引部分数据
-
SCROLL_SIZE
这个参数是执行删除的时候,每次每个线程会查询的数据量,然后进行删除。
默认是100,就是说每个线程每次都会查询出100条数据然后再删除。 -
SLICES
这个就相当于开启的线程数,同时会有多少个线程执行查询然后删除程序。
默认是1,就是同时只会有一个线程进行查询删除操作。 -
CONFLICTS
如果不想版本冲突引起操作失败,可以在请求中加入参数:conflicts=proceed
#删除索引部分数据
POST my_index/_delete_by_query?scroll_size=3000&slices=5&conflicts=proceed
{
"query": {
"bool": {
"filter": [{
"range": {
"time": {
"from": "2022-01-17T22:00:00.000Z",
"to": "2022-01-18T00:00:00.000Z"
}
}
}]
}
}
}
修该es刷新间隔
#修该es刷新间隔
PUT /my_index/_settings
{
"index" : {
"refresh_interval" : "1s"
}
}
修该es副本数
#修该es副本数
PUT /my_index/_settings
{
"index" : {
"number_of_replicas" : "1"
}
}
指定(重新分配)分片
#指定(重新分配)分片
POST /_cluster/reroute?pretty
{
"commands" : [
{
"allocate_stale_primary" : {
"index" : "index-2019.11.08", "shard" :4,
"node" : "node-1",
"accept_data_loss" : true
}
}
]
}
迁移分片到其他节点
#迁移分片到其他节点
POST /_cluster/reroute
{
"commands" : [
{
"move" : {
"index" : "index-2019.11.08", "shard" : 4,
"from_node" : "node-1", "to_node" : "node-2"
}
}
]
}
清空指定index/type下的数据
#清空指定index/type下的数据
POST index_name/type_name/_delete_by_query
{
"query": {
"match_all": {}
}
}
重建索引
#重建索引
curl -H "Content-Type:application/json" -XPOST http://127.0.0.1:9200/_reindex -d '{"source":{"index":"index-2021.03.01-others"},"dest":{"index":"myindex-2021.03.01","op_type":"create"}}'
聚合查询
查询时间范围(时间戳,时间根据Mapping自行修改)在 1642751644000 到 1642751645000 之间,字段type 为 1的数据,并且计算字段为count值的和,result为聚合名称
#聚合查询
GET index_name/_search
{
"size": 0,
"query": {
"bool": {
"must": [{
"range": {
"time": {
"gte": 1642751644000,
"lte": 1642751645000
}
}
}, {
"match": {
"type": 1
}
}]
}
},
"aggs": {
"result": {
"sum": {
"field": "count"
}
}
}
}
查询时间范围(时间戳,时间根据Mapping自行修改)在 1658609400000 到 1658609400000 之间,字段type 为 1,且模糊匹配后缀为“baidu.com”的host字段,并且计算字段为count值的和,sum_count为聚合名称,按降序排序。
完整查找如下
GET index_name/_search
{
"size": 0,
"query": {
"bool": {
"must": [{
"range": {
"time": {
"gte": 1658609400000,
"lte": 1658609400000
}
}
}, {
"term": {
"type": {
"value": "1"
}
}
},
{
"wildcard": {
"host": "*.baidu.com"
}
}
]
}
},
"aggs": {
"hosts": {
"terms": {
"field": "host",
"size": 1000,
"order": [{
"sum_count": "desc"
}]
},
"aggs": {
"sum_count": {
"sum": {
"field": "count"
}
}
}
}
}
}
term可以使用match
{
"match": {
"type": "1"
}
}