es的CURL常用命令整理

目录

1、集群相关命令

a、查看集群全部索引健康程度:

b、查看集群的健康信息:

c、查看全部节点:

d、常看集群全部索引

2、索引相关命令

a、新建索引

b、创建索引并指定settings和mapping:

 c、设置索引的读写权限

d、查看索引信息

e、删除索引

f、打开/关闭索引

g、修改索引settings

h、重建索引

3、文档相关命令

a、新建文档

b、批量新增文档

c、获取单个文档

d、获取多个文档

e、默认查询文档

f、复杂查询get方式获取文档

g、复杂查询post方式

 h、删除单个文档

i、删除文档(条件匹配 )

j、删除全部文档

k、批量删除文档

l、文档修改

m、并发修改(es的锁机制)

n、内嵌属性解释说明

o、判断文档是否存在

p、查询有name属性的文档

4、高级查询相关命令(部分案例)

a、一个字段多个值查询

b、多个字段查询

c、返回指定字段的查询

d、区间查询

e、 复合查询

 f、过滤查询

g、其他查询

h、 文档排序

i、字段高亮

j、查看满足条件的数据总量:

k、查询八月七日全天的数据量

l、聚合分组查询获取Top10

m、query_string查询

5、快照相关命令

a、查看所有仓库

b、查看指定仓库信息

c、查看所有快照 

d、查看指定快照信息

e、创建备份仓库

f、修改仓库配置

g、备份指定索引

h、备份全部索引

i、备份满足条件的索引

 j、查看备份情况

k、快照恢复

l、快照恢复并重命名索引

m、查看全部快照恢复情况

n、查看指定索引快照恢复情况

o、删除指定快照

p、删除指定仓库

6、模板常用命令

a、查询全部模板

b、查询以test开头的所有模板

c、查询名为test的模板

d、删除名为test的模板

e、删除以test开头的所有模板

f、创建模板


1、集群相关命令

a、查看集群全部索引健康程度:

curl -XGET http://localhost:9200/_cat/health?pretty

参数说明:

        pretty:友情美化展示json

b、查看集群的健康信息:

curl -XGET http://localhost:9200/_cluster/health

c、查看全部节点:

curl -XGET http://localhost:9200/_cat/indices?v

参数说明:

        v:让返回结果显示表头。

d、常看集群全部索引

curl -XGET http://localhost:9200/_cat/indices?v 

2、索引相关命令

a、新建索引

curl -XPUT http://ip:端口号/索引名称

b、创建索引并指定settings和mapping:

curl -XPUT 'http://192.168.1.110:9200/cnl' -H 'Content-Type: application/json' -d '{"settings": {"number_of_shards": 5,"number_of_replicas" : 1}}' 

创建索引指定mapping和settings配置:

        curl -XPUT 'http://localhost:9200/索引名称' -H 'Content-Type: application/json' -d '{"settings": {"number_of_shards": 5,"number_of_replicas" : 1},"mappings":{"properties": {"time":{"type": "date","format": "yyyy-MM-dd HH:mm:ss"}}}}'

 c、设置索引的读写权限

curl -XPUT -d '{"blocks.read":false}' http://ip:端口号/索引名称/_settings

参数说明:

        _settings:表示的是此索引的settings属性

        blocks.read:true表示开启读限制,false不开启读限制.

d、查看索引信息

查看单个:

        curl -XGET http://ip:端口号/索引名称1

查看多个:

        curl -XGET http://ip:端口号/索引名称,索引名称2

查看全部索引状态:

        curl -XGET http://10.10.2.22:9200/_cat/indices?v

e、删除索引

curl -XDELETE http://ip:端口号/索引名称 

f、打开/关闭索引

关闭:

        curl -XPOST http://ip:端口号/索引名称/_close

打开:

        curl -XPOST http://ip:端口号/索引名称/_open

g、修改索引settings

curl -XPUT 'http://10.10.32.110:9200/索引名/_settings' -H 'Content-Type: application/json' -d '{"index":{"number_of_replicas":0}}' 

h、重建索引

由于mapping要进行修改(修改字段的类型等),但是mapping是不能修改的,索引要新建索引之后进行数据搬迁。 

curl -XPOST -d '{"source":{"index":"old_index"},"dest":{"index":"new_index"}}' http://localhost:9200/_reindex

参数说明:

        source:指定源索引

        dest:指定新索引

3、文档相关命令

a、新建文档

put方式:必须指定document的ID,重复保存是进行数据的修改。

        curl -XPUT  -H "Content-Type: application/json" -d '{"id":"123","ip":"0.0.0.0","create_time":"2022-01-23 14:29:56","organization_code":"1-c-4cd9-809a-123"}' http://localhost:9200/test_data/_doc/123

post方式:不用指定ID,重复保存是形成了多条文档数据。

        curl -XPUST -d '{"id":1,"title":"es简介"}' http://localhost:9200/索引名/_doc

b、批量新增文档

curl -XPOST -H "Content-Type:application/json" http://localhost:9200/_bulk?pretty --data-binary @/home/data/bulk.txt

参数说明:--data-binary 指定文件。 

        bulk.txt文件内容格式:

                示例:

                {"create":{"_index":"student","_id":"1213"}}

                {"id":"1213","age":14,"name":"李四","sex":"男"}

                {"create":{"_index":"student","_id":"1212"}}

                {"id":"1212","age":12,"name":"张三","sex":"男"}

c、获取单个文档

curl -XGET Http://myhost:9200/index/doc/01 

d、获取多个文档

curl -XGET  -d "{"ids":["111","222"]}" http://ip:端口号/index/_doc/_mget 

e、默认查询文档

curl -XGET http://localhost:9200/index/_search 

f、复杂查询get方式获取文档

curl -XGET http://localhost:9200/index/_search?q=age,"20"

参数说明:

        q:代表query的意思。查询该索引下满足age是20的值的所有文档。 

g、复杂查询post方式

curl -XPOET -d "{查询条件编写}" http://localhost:9200/index/_search 

 h、删除单个文档

curl -XDELETE http://ip:端口号/索引名称/文档对象类型/文档对象id

i、删除文档(条件匹配 )

curl -XPOST http://ip:端口号/索引名称/_delete_by_query?conflicts=proceed&scroll_size=10000&wait_for_completion=false  -d '{"query":{"match_all":{"要删除的字段" : "对应的值"}}}' 

参数说明:

        conflicts=proceed : 有冲突不中断继续执行

        scroll_size=2000 : 删除文档数

        wait_for_completion=false : 开启异步

        slices=2 : 线程数量

j、删除全部文档

curl -POST http://ip:端口号/索引名称/文档对象类型/_delete_by_query  {"query": {"match_all": {}}}

k、批量删除文档

curl -XPOST -H "Content-Type:application/json" http://localhost:9200/_bulk?pretty --data-binary @/home/data/bulk.txt

参数说明:--data-binary 指定文件。

        bulk.txt文件内容格式:

        {"delete":{"_index":"test_data","_id":"f0c201bf-b47e-4680-8aed-e3aeb489a255-1111"}} 

l、文档修改

curl -XPOST -d "{"name":"haah","id":"111"}" http://localhost:9200/索引名/_doc/索引id

m、并发修改(es的锁机制)

es的乐观锁机制:

        默认的锁机制为乐观锁(默认不会有同时修改的操作)。可以用es的默认字段来控制锁:_seq_no字段(该文档修改的次数)和_primary_term字段(修改的轮数)控制锁的机制。(两个字段结合的理解就是第几轮的第几次修改。)

        假设es的原始文档数据:{"id":"1","name":"hah","age":1},该文档当前的_primary_term字段为1,_primary_term字段为2

        请求1:修改age为10

        curl -XPOST -d "{"id":"1","name":"hah","age":10}" http://localhost:9200/索引名/_doc/索引id

        请求2:修改name为xixi

        curl -XPOST -d "{"id":"1","name":"xixi","age":1}" http://localhost:9200/索引名/_doc/索引id

如果不进行锁的控制:那么第二次请求会覆盖第一次请求的值。(age会修改不成功。)

进行锁控制的操作:

        请求1:

        curl -XPOST -d "{"id":"1","name":"hah","age":10}" http://localhost:9200/索引名/_doc/索引id?if_primary_term=1&if_seq_no=2

        请求2:

        curl -XPOST -d "{"id":"1","name":"xixi","age":1}" http://localhost:9200/索引名/_doc/索引id?if_primary_term=1&if_seq_no=2

        进行了锁控制之后,第一次请求会修改成功,修改完之后_primary_term和_primary_term字段会变成1和3,那么请求2就不会修改成功,只能进行重新查询数据,查看那俩字段然后重新进行修改发送请求。

        curl -XPOST -d "{"id":"1","name":"xixi","age":10}" http://localhost:9200/索引名/_doc/索引id?if_primary_term=1&if_seq_no=3

        这样才能修改成功。而且也读取到了请求一修改的age为10,这样不会造成数据错乱。

n、内嵌属性解释说明

json数据:

        {"name":"周杰伦","song":[{"name":"稻香","data":"2004"},{"name":"双截棍","data":"2013"},{"name":"娃娃脸","data":"2014"}]}

        存入es数据库,es默认处理song属性字段存储是进行扁平化处理储存的,song.name:["稻香","双截棍","娃娃脸"] song.data:["2004","2013","2014"],这样我们想精确查找一条数据,song.name=稻香&song.data=2013,如果不进行设置song属性为内嵌类型,那么这条数据是匹配上的数据,如果设置song属性为内嵌类型的,那么这条数据是匹配不上的。把song设置成内嵌属性的话,es存储会把song属性下的内容进行单条存储,不会进行扁平化存储,这样song属性下的每个对象是一个完整数据。

        设置song属性为内嵌类型的:

        curl -XPUT -d "{"mappings":{"properties":{"song":{"type":"nested"}}}}" http://localhost:9200/index

o、判断文档是否存在

curl -XHEAD http://localhost:9200/index/_doc/id 

p、查询有name属性的文档

curl -XPOST -d "{"query":{"exists":{"field":"name"}}}" http://localhost:9200/index/_search

参数说明:exists (用来查询字段是否存在。) 

4、高级查询相关命令(部分案例)

a、一个字段多个值查询

查询name为hah或者xixi的文档:

        curl -XPOST -d "{"query":{"terms":{"name":["hah","xixi"]}}}" http://localhost:9200/index/_search

b、多个字段查询

指定在name和nickname字段中查询为hah的值的所有文档:

        curl -XPOST -d "{"query":{"multi_match":{"query":"hah","fields":["name","nicename"]}}}" http://ip:9200/index/_search

c、返回指定字段的查询

查询name为hah的多有文档,返回只要age字段。其他字段不要。  

        方式一:curl -XPOST -d "{"query":{"match":{"name":"hah"}},"_source":["age"]}" http://localhost:9200/index/_search

        方式二:curl -XPOST -d "{"query":{"match":{"name":"hah"}},"_source":{"includes":["name"],"excludes":[""]}}" http://localhost:9200/index/_search

        参数说明:includes指定想要返回值包含的字段,excludes指定不需要返回值包含的字段。

d、区间查询

查询年龄在10到20(包含10和20)之间的全部文档:

        curl -XPOST -d "{"query":{"range":{"age":{"gte":10,"lte":20}}}}" http://localhost:9200/index/_search

e、 复合查询

        bool查询中可以包含多个查询条件,must表示应该满足的条件,should表示模糊匹配条件,mush_not表示不应该满足的条件。每个条件里边都可以包含多个条件,比如must条件中,包含了name必须是hah和age必须在10到20之间。

        curl -XPOST -d "{"query":{"bool":{"must":[{"match":{"name":"hah"}},{"range":{"age":{"gte":20,"lte":30}}}],"should":[{}],"must_not":[{}]}}}" http://localhost:9200/index/_search

 f、过滤查询

        filter查询的must查询效率基本一样,区别是filter查询出来的文档是没有评分的,must有评分。

        一般filter里边写非text字段类型的过滤条件,不用参与评分,must里边写text字段类型的条件,进参与评分。should里边的条件是非强制满足的条件查询,满足的会进行加分,不满足不进行加分。

        查询到name为hah的所有人之后再过滤出来age满足10到20之间的文档。

        {"query":{"bool":{"must":[{"match":{"name":"hah"}}],"filter":[{"range":{"age":{"gte":10,"lte":20}}},{}]}}}

g、其他查询

fuzzy(近似匹配)

match_phrase(短句匹配)

h、 文档排序

curl -XPOST -d "{"query":{},"sort":[{},{}]}" http://localhost:9200/index/_search

i、字段高亮

设置字段name满足张三条件的内容高亮,设置被span标签包含

        curl -XPOST -d "{"query":{"match":{"name":"张三"}},"highlight":{"fields":{"name":{"pre_tags":"<span style='color:red'> ","post_tags":"</span>"}}}}" http://localhost:9200/index/_search

j、查看满足条件的数据总量:

curl -XPOST http://10.10.10.10:9200/索引名称/_count -H 'Content-Type: application/json' -d '{"query": {"range": {"time": {"gte": "2022-01-01 00:00:00","lt": "2022-01-02 00:00:00"}}}}'

k、查询八月七日全天的数据量

curl -XPOST http://localhost:9200/test_data/_search -d '{"query":{"range":{"time":{"gte":"2021-08-07 23:59:58","lt":"2021-08-08 00:00:00"}}},"from":0,"size":1,"aggs":{"count":{"value_count":{"field":"id.keyword"}}}}'

l、聚合分组查询获取Top10

curl -XPOST http://localhost:9200/test_data/_search -d '{"query":{"match":{"sip":"111.207.231.5 OR 59.252.254.165 OR 106.37.221.188"}},"aggs":{"group":{"terms":{"field":"login_email.keyword","size":10,"order":{"login_count_sum":"desc"}},"aggs":{"login_count_sum":{"sum":{"field":"login_count"}}}}}}'

m、query_string查询

curl -XPOST http://localhost:9200/test_data/_search -d '{"query":{"query_string":{"default_field":"from_email.keyword","query":"huwe@111.com OR li41@111.com OR uimei@111.com OR chnning87@111.com OR shiolin8@111.com OR lianhui89@111.com OR hong74@111.com OR gxyang@111.com OR chnyun1@111.com OR chenui3@111.com"}},"size":2,"aggs":{"group":{"terms":{"field":"subject.keyword","size":10000}}}}'

5、快照相关命令

a、查看所有仓库

curl -XGET -u elastic:123321 http://localhost:9200/_snapshot/_all

b、查看指定仓库信息

curl -XGET -u elastic:123321 http://localhost:9200/_snapshot/仓库名称

c、查看所有快照 

curl -XGET -u elastic:123321 http://localhost:9200/_snapshot/仓库名称/_all

d、查看指定快照信息

curl -XGET -u elastic:123321 http://localhost:9200/_snapshot/仓库名称/快照名称

e、创建备份仓库

curl -XPUT -u elastic:123321 -H 'Content-Type: application/json' http://localhost:9200/_snapshot/esbackup -d '{"type": "fs", "settings": {"location": "/home/esbackup","max_restore_bytes_per_sec":"100mb","max_snapshot_bytes_per_sec":"100mb","compress":true,"chunk_size":"50MB"}}'

f、修改仓库配置

curl -XPOST -u elastic:123321 -H 'Content-Type: application/json' http://localhost:9200/_snapshot/esbackup -d '{"type": "fs", "settings": {"location": "/home/esbackup","max_restore_bytes_per_sec":"100mb","max_snapshot_bytes_per_sec":"100mb","compress":true}}'

参数说明:

        put请求用来创建新仓库,post请求可以新增和修改现有仓库的配置。

        type:fs    表示以快照的方式存储文件

        location:如果为相对路径时(123/aaa),仓库的地址 = es配置文件中path.reop的值 + location的值,如果为绝对路径时( /123),仓库的地址 = location的值

        max_restore_bytes_per_sec:节点恢复速率(当从仓库恢复数据时)。默认20mb/s。

        max_snapshot_bytes_per_sec:每个节点快照速率(当快照数据进入仓库时)。默认20mb/s。

        compress:是否压缩,默认为是。

        chunk_size:块大小,在快照过程中,大文件会被分解成块,该属性指定块的大小。1GB、500MB、5KB、500B,默认为null(不受块大小限制。)

        readonly:让库只读,默认为false

g、备份指定索引

curl -XPUT -u elastic:123321 -H 'Content-Type: application/json' http://localhost:9200/_snapshot/esbackup/snapshot_20220104?wait_for_completion=true -d '{"indices": "test_index,test01_index","include_global_state":false,"ignore_unavailable":"true","partial":"false"}'

参数说名:

        esbackup:表示仓库名称。

        snapshot_20220104:表示快照名称(快照名称必须是小写)

        indices:表示创建哪些索引的快照,空表示所有。

        ignore_unavailable:忽略不存在的索引(不设置会快照请求会失败)

        include_global_state:防止集群的全局状态被作为快照的一部分储存起来

        wait_for_completion=true:参数指定是在初始化快照(默认)后立即返回请求还是等待快照完成,true表示等待快照完成之后返回请求。不设置表示进行后台创建快照,立即返回请求。

        partial:默认为false,默认情况下,如果快照红的1个或多个索引不是全部分片都可用会导致整个请求过程失败,将其设置为 true 可以改变此行为

h、备份全部索引

curl -XPUT -u elastic:123321 http://localhost:9200/_snapshot/仓库名称/创建的快照名称?wait_for_completion=true

i、备份满足条件的索引

curl -XPUT -u elastic:123321 -H 'Content-Type: application/json' http://localhost:9200/_snapshot/esbackup/snapshot_20220104 -d '{"indices": "test*","include_global_state":false,"ignore_unavailable":"true","partial":"false"}'

 j、查看备份情况

curl -XGET -u elastic:123321 http://localhost:9200/_snapshot/esbackup/snapshot_20220104/_status

k、快照恢复

curl -XPOST -u elastic:123321 http://localhost:9200/_snapshot/esbackup/snapshot_20220104/_restore?wait_for_completion=true

l、快照恢复并重命名索引

curl -XPOST http://localhost:9200/_snapshot/esbackup/snapshot_20220104/_restore?wait_for_completion=true -d '{"indices": "test01_index,test02_index","ignore_unavailable": "true","include_global_state": false, "rename_pattern": "index_(.+)","rename_replacement": "restored_index_$1" }'

参数说明:

        rename_pattern:查找所提供的模式能匹配上的正在恢复的索引。

        rename_replacement:然后把它们重命名成替代的模式。

        wait_for_completion=true:参数指定是在恢复快照后立即返回请求还是等待快照完成,true表示等待快照完成之后返回请求。不设置表示立即返回请求,然后后台恢复索引。

m、查看全部快照恢复情况

curl -XGET -u elastic:123321 http://localhost:9200/_recovery

n、查看指定索引快照恢复情况

curl -XGET -u elastic:123321 http://localhost:9200/test_index/_recovery

o、删除指定快照

curl -XDELETE -u elastic:123321 http://localhost:9200/_snapshot/仓库名称/快照名称

p、删除指定仓库

curl -XDELETE -u elastic:123321 http://localhost:9200/_snapshot/仓库名称

6、模板常用命令

a、查询全部模板

curl -XGET -u 'elastic:123123' http://localhost:9200/_template

b、查询以test开头的所有模板

curl -XGET -u 'elastic:1231223' http://localhost:9200/_template/test*

c、查询名为test的模板

curl -XGET -u 'elastic:1231223' http://localhost:9200/_template/test

d、删除名为test的模板

curl -XDELETE -u 'elastic:1231223' http://localhost:9200/_template/test

e、删除以test开头的所有模板

curl -XDELETE -u 'elastic:1231223' http://localhost:9200/_template/test*

f、创建模板

curl -XPUT -u 'elastic:1231223' -H 'Content-Type: application/json' http://localhost:9200/_template/default_index -d '{"index_patterns":["test_*"],"settings":{"number_of_replicas":1,"refresh_interval":"10s","index.search.slowlog.threshold.query.warn":"10s","index.search.slowlog.threshold.query.info":"5s","index.search.slowlog.threshold.query.debug":"2s","index.search.slowlog.threshold.query.trace":"500ms"},"mappings":{"properties":{"time":{"type":"date","format":"yyyy-MM-dd HH:mm:ss"}}},"order":0}'

参数说明:

        refresh_interval: "10s" #刷新间隔,当数据添加到索引后并不能马上被查询到,等到索引刷新后才会被查询到。如果对实时性要求不高,可以增加该值提高写入性能

        dynamic: false #是否关闭动态字段映射,默认为true,这里选择个人选择禁用

        number_of_shards: 1 #分片数量

        max_result_window: 100000 #查询最大返回值1万条

        number_of_replicas: 1, #索引副本数量,推荐副本数为1

        order:0 #表示该模板的优先等级,数字越大优先级越高。0-10

        index.search.slowlog.threshold.query.warn: 10s  #超过10秒的query产生1个warn日志

        index.search.slowlog.threshold.query.info: 5s   #超过5秒的query产生1个info日志

        index.search.slowlog.threshold.query.debug: 2s   #超过2秒的query产生1个debug日志

        index.search.slowlog.threshold.query.trace: 500ms #超过500毫秒的query产生1个trace日志

        #fetch阶段的配置

        index.search.slowlog.threshold.fetch.warn: 1s  #

        index.search.slowlog.threshold.fetch.info: 800ms

        index.search.slowlog.threshold.fetch.debug: 500ms

        index.search.slowlog.threshold.fetch.trace: 200ms

        #Index Slow log:索引慢日志配置

        index.indexing.slowlog.threshold.index.warn: 10s   #索引阶段的配置

        index.indexing.slowlog.threshold.index.info: 5s  

        index.indexing.slowlog.threshold.index.debug: 2s

        index.indexing.slowlog.threshold.index.trace: 500ms

  • 4
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值