elasticsearch 8.x常用命令

1. 创建模板

curl -X PUT "192.1.1.1:9200/_index_template/service-template?pretty" -H 'Content-Type: application/json' -d'
{
  "index_patterns":[
    "service-*"
  ],
  "template":{
    "settings":{
      "number_of_shards":2,
      "number_of_replicas":1,
      "index.max_result_window":"10000"
    },
    "mappings":{
     "dynamic_templates": [
        {
            "strings_as_keywords": {
                "match_mapping_type": "string",
                "mapping": {
                    "type": "keyword"
                }
            }
        }
       ],
      "properties":{
        "id":{
          "type":"keyword"
        },
        "value":{
          "type":"short"
        },
        "customerFields":{
          "type":"nested",
          "include_in_parent":true,
          "properties":{
            "fieldId":{
              "type":"keyword"
            },
            "fieldType":{
              "type":"short"
            },
            "sort": {
              "type": "integer"
            }
          }
        },
        "remarks":{
          "type":"keyword"
        }
      }
    }
  }
}'

2. 查询模板

curl -X GET "192.1.1.1:9200/_index_template/service-template?pretty"

3. 删除模板

curl -X DELETE "192.1.1.1:9200/_index_template/service-template?pretty"

4. 创建表

curl -X PUT "192.1.1.1:9200/service-2025-02?pretty"

5. 查询表

curl -X GET "192.1.1.1:9200/service-2025-02/_mapping?pretty"

6. 删除表

curl -X DELETE "192.1.1.1:9200/service-2025-02/_search?pretty"

7. 添加普通字段

curl -X POST "192.1.1.1:9200/service-2025-01,service-2025-02/_mapping?pretty" -H 'Content-Type: application/json' -d'
{
  "properties":{
    "remark": {
      "type": "integer"
    }
  }
}'

8. 添加嵌套字段

curl -X POST "192.1.1.1:9200/service-2025-01,service-2025-02/_mapping?pretty" -H 'Content-Type: application/json' -d'
{
  "properties":{
    "customerFields": {
        "type":"nested",
        "include_in_parent":true,
        "properties":{
            "sort":{
              "type":"integer"
            }
        }
    }
  }
}'

9.查询表结构

curl -X GET "192.1.1.1:9200/service-2025-02/_mapping?pretty"

10. 给模板增加自动清理时间(高级属性)

curl -X PUT "192.1.1.1:9200/_ilm/policy/service_polic?pretty" -H 'Content-Type: application/json' -d'
{
  "policy": {
    "phases": {
      "delete": {
        "min_age": "30d",
        "actions": {
          "delete": {}
        }
      }
    }
  }
}'

11. 给模板增加setting(高级属性)

curl -X PUT "192.1.1.1:9200/_index_template/service-template?pretty" -H 'Content-Type: application/json' -d'
{
  "index_patterns":[
    "service-*"
  ],
  "template":{
    "settings":{
      "number_of_shards":2,
      "number_of_replicas":1,
      "index.max_result_window":"10000",
      "index.lifecycle.name": "service_polic",
      "index.lifecycle.rollover_alias": "service_polic_alias",
      "index.refresh_interval": "2s",
      "index.translog.flush_threshold_size": "1gb",
      "index.translog.flush_threshold_period": "1h"
    },
    "mappings":{
      "dynamic_templates": [
                        {
                            "strings_as_keywords": {
                                "match_mapping_type": "string",
                                "mapping": {
                                    "type": "keyword"
                                }
                            }
                        }
                    ],
      "properties":{
        "id": {
          "type": "keyword"
        }
        ......
        ......
      }
    }
  }
}'

12. 查询指定字段

curl -X GET "192.1.1.1:9200/service-2025-01/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "size" : 10,
  "_source" : ["id","name"],
  "query" : {
     "query_string" : {
        "query" : "id:123abc"
     }
  }
}'

13. 条件查询

curl -X GET "192.1.1.1:9200/service-2025-01/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "size" : 5,
  "query" : {
     "query_string" : {
        "query" : "id:123abc AND customerFields.name:\"hello world\" AND createTime:[1735660800000 TO 1738339199000] AND sort:1 AND createTime:(>=1735660800000 AND <1738339199000) AND sort:[1 TO 2]"
     }
  }
}'

14. 查找某个字段,同时字段值是空字符串

curl -X GET "192.1.1.1:9200/service-2025-01/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "size" : 5,
  "query" : {
     "query_string" : {
        "query" : "_exists_:name AND name:\"\""
     }
  }
}'

15. 字段存在,值存在,不包含字符串

curl -X GET "192.1.1.1:9200/service-2025-01/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "size" : 5,
  "query" : {
     "query_string" : {
        "query" : "_exists_:name AND !name:\"\""
     }
  }
}'

16. 排序查询

curl -X GET "192.1.1.1:9200/service-2025-02/_search?pretty" -H 'Content-Type: application/json' -d' 
{
  "size" : 2,
  "query" : {
    "query_string" : {
      "query" : "field:hello world"
    }
  },
    "sort":[
        {
            "creationTime":{
                "order":"asc"
            }
        }
    ]
}'

17.分组查询

curl -X GET "192.1.1.1:9200/service-2025-02/_search?pretty" -H 'Content-Type: application/json' -d' 
{
    "size": 2,
    "query": {
        "query_string": {
            "query": "field:hello world"
        }
    },
    "aggs": {
        "group_by_userId": {
            "terms": {
                "field": "userId",
                "size": 10
            }
        }
    }
}'

18. 分组聚合查询

curl -X GET "192.1.1.1:9200/service-2025-01/_search" -H 'Content-Type: application/json' -d'
{
  "_source": ["timestamp","field1","field2","sort","userId"],
  "size": 10,
  "query": {
    "bool": {
        "must": [
            {
                "term": {
                    "field1": "hello"
                }
            },
            {
                "term": {
                    "field2": "world"
                }
            }
        ]
    }
  },
  "aggs": {
    "average_per_10_seconds": {
      "date_histogram": {
        "field": "timestamp",
        "fixed_interval": "10s",
        "min_doc_count": 0
      },
      "aggs": {
        "average_sort": {
          "avg": {
            "field": "sort",
            "missing": 0
          }
        },
        "max_sort": {
          "max": {
            "field": "sort",
            "missing": 0
          }
        }
      }
    }
  }
}'

19. 修改string的值

curl -X POST "192.1.1.1:9200/service-2025-01/_update_by_query?pretty" -H 'Content-Type: application/json' -d'
{
    "script":{
        "source":"ctx._source.name= params.name",
        "params":{
            "name":"tom"
        },
        "lang":"painless"
    },
    "query":{
        "query_string":{
            "query":"id:465365"
        }
    }
}' 

20. 修改int/long的值

curl -X POST "http://192.1.1.1:9200/service-2025-01/_update_by_query?pretty" -H 'Content-Type: application/json' -d'
{
    "script":{
        "source":"ctx._source.createTime=1735660800000l;",
        "lang":"painless"
    },
    "query":{
        "query_string":{
            "query":"id:42265"
        }
    }
}'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值