elasticsearch常用命令

  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"
    },
    "type":{
    "type":"short"
    },
    "sort": {
    "type": "integer"
    }
    }
    },
    "remarks":{
    "type":"keyword"
    }
    }
    }
    }
    }'

  2. 查询模板

复制代码
curl -X GET "192.1.1.1:9200/_index_template/service-template?pretty"
  1. 删除模板

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

  1. 创建表
复制代码
curl -X PUT "192.1.1.1:9200/service-2025-02?pretty"
  1. 查询表
复制代码
curl -X GET "192.1.1.1:9200/service-2025-02/_mapping?pretty"
  1. 删除表
复制代码
curl -X DELETE "192.1.1.1:9200/service-2025-02/_search?pretty"
  1. 添加普通字段

    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"
    }
    }
    }'

  2. 添加嵌套字段

    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"
  1. 给模板增加自动清理时间(高级属性)

    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"
        }
        ......
        ......
      }
    }
  }
}'
  1. 排序查询

    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"
    }
    }
    ]
    }'

14.分组查询

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
            }
        }
    },
    "sort": [
        {
            "creationTime": {
                "order": "asc"
            }
        }
    ]
}'
  1. 分组聚合查询

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

相关推荐
羱滒10 分钟前
sql调优之数据库开发规范
java·数据库·数据库开发
程序员Android13 分钟前
MTK多帧拍照流程分析
数据库
不剪发的Tony老师14 分钟前
Apache Doris:一款高性能的实时数据仓库
数据库·数据仓库
甜可儿25 分钟前
redis序列化设置
数据库·redis
GGBondlctrl30 分钟前
【SpringBoot】论坛项目中如何进行实现发布文章,以及更新对应数据库的数据更新
数据库·springboot项目·三层设计思想·文章发布项目
jonyleek42 分钟前
「JVS更新日志」低代码、企业会议、智能BI、智能排产2.26更新说明
java·大数据·低代码·数据分析·软件需求
若云止水44 分钟前
Ubuntu 下 nginx-1.24.0 源码分析 - ngx_init_cycle 函数 - 详解(4)
数据库·nginx·ubuntu
jay丿1 小时前
Django模板系统深入
数据库·django·sqlite
kngines1 小时前
【实战 ES】实战 Elasticsearch:快速上手与深度实践-1.3.1单节点安装(Docker与手动部署)
大数据·elasticsearch·docker
小稻草打打打2 小时前
金融项目管理:合规性与风险管理的实战指南
大数据·金融