curl 命令操作ES

curl介绍

curl其实是一种用URL语法,它是一种传输数据工具,是通过命令来进行工作的。Curl在很多的操作系统中被使用,其中包括Unix、和Linux,除此之外,也有DOS和Win64等的版本。curl 命令是利用 url 在命令行下进行工作的传输工具,它支持包括 file、ftp、ftps、http、https、imap、imaps、ldap、ldaps、mqtt、pop、pop3s、rtmp、rtmps、rtsp、scp、sftp、smb、smbs、smtp、smtps、telnet 和 tftp 等协议。

curl使用

基本语法:curl [option] [url]

option的参数使用我们可以使用 curl -h 命令查看帮助

-o, --output <file> 写入到文件,而不是输出到stdout

-O 写入到文件,文件名和远程文件一样

-L 跟随网站的跳转

-x, --proxy [protocol://][user:pwd@]host[:port] 使用代理

-v 打印过程

--trace <file> debug写入到文件,很详细包括二进制数据交换,file使用 - 表示打印到stdout

-c <file> 将服务器设置的cookie写入到文件

-b <data> 发送cookie,从 string/file 获取

-A <name> 发送 User-Agent <name> 到服务器

-e <url> 指定 Referer : <url> , 仿造referer,服务器会以为你是从 url 点击某个链接过来的

-H <header/@file> 将自定义标头传递到服务器

-X <command> 指定请求方法,不带任何参数的请求默认get方法

-s Silent mode 无声模式

-S Show error even when -s is used 即使使用 -s 也打印错误

-i 打印服务器回应的http标头

-I 只打印标头

-k 使用ssl时,允许不安全的服务器连接。跳过ssl检测

-d <data> http post data,使用post方法发送表单,自动添加标头Content-Type : application/x-www-form-urlencoded

-F <name=content> 指定 multipart MIME data , 可以上传二进制文件,自动添加Content-Type: multipart/form-data

-G 把 post data 放进 url 并使用 get 请求,与-d配合

-u <user:password> 指定服务器用户和密码

-T <file> 上传文件,使用 put 请求

curl命令操作ES

使用curl请求elasticsearch查询信息格式如下:

curl -X<VERB> '<PROTOCOL>://<HOST>:<PORT>/<PATH>?<QUERY_STRING>' -d '<BODY>'
    
VERB
适当的 HTTP 方法 或 谓词 : GET、POST、PUT、HEAD 或者 DELETE。

PROTOCOL
http 或者 https(如果你在 Elasticsearch 前面有一个https 代理)

HOST
Elasticsearch 集群中任意节点的主机名,或者用 localhost 代表本地机器上的节点。

PORT
运行 Elasticsearch HTTP 服务的端口号,默认是 9200 。

PATH
API 的终端路径(例如 _count 将返回集群中文档数量)。Path 可能包含多个组件,例如:_cluster/stats 和 _nodes/stats/jvm 。

QUERY_STRING
任意可选的查询字符串参数 (例如 ?pretty 将格式化地输出 JSON 返回值,使其更容易阅读)

BODY
一个 JSON 格式的请求体 (如果请求需要的话)

检查ES是否启动成功

curl http://localhost:9200
{
  "name" : "Myhost",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "abtEL4GKRfulSwTfJ0wX5Q",
  "version" : {
    "number" : "7.2.0",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "508c38a",
    "build_date" : "2019-06-20T15:54:18.811730Z",
    "build_snapshot" : false,
    "lucene_version" : "8.0.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

命令帮助

curl 'localhost:9200/_cat'

查看所有的 Index (v是用来要求结果返回表头)

curl 'localhost:9200/_cat/indices?v'

检查集群状态

curl 'localhost:9200/_cat/health?v'

查看es集群配置

curl -X GET "localhost:9200/_cluster/settings?pretty"

查看指定索引库下文档数量

curl 'localhost:9200/_cat/count/my_book?v'

通配符查询某类索引库

curl 'localhost:9200/_cat/indices/my_*?v'

查看索引库的别名配置

curl 'localhost:9200/_aliases'

查看索引库的mapping配置

curl 'localhost:9200/my_book/_mapping'

查看索引库的mapping配置(格式化展示,pretty参数表示让结果以json格式输出展示)

curl 'localhost:9200/my_book/_mapping?pretty'

查看索引库的全量数据

curl 'localhost:9200/my_book/_search?pretty'

查看索引库的某一条文档数据

curl 'localhost:9200/my_book/1001?pretty'

根据条件搜索文档

curl -X POST "localhost:9200/my_book*/_search?pretty" -H 'Content-Type:application/json' -d '{"query":{"match":{"book_id":"7623957135287154"}}}'

修改ES最大分片数
curl -XPUT -H "Content-Type:application/json" http://localhost:9200/_cluster/settings -d '{"transient":{"cluster":{"max_shards_per_node":10000}}}'

删除索引库

curl -X DELETE "localhost:9200/my_test_index01"

根据条件删除文索引库数据,以下以id为例

curl -X POST "localhost:9200/my_test_index01/_delete_by_query?pretty" -H 'Content-Type:application/json' -d '{"query":{"match":{"_id":"1001"}}}'
清空索引库

curl -X POST "localhost:9200/my_test_index01/_delete_by_query?pretty" -H 'Content-Type:application/json' -d '{"query":{"match_all":{}}}'
修改索引库字段内容

curl -X POST "localhost:9200/my_test_index01/_update/1001?pretty" -H 'Content-Type:application/json' -d '{"doc":{"name":"蓝闪"}'

列出集群范围的设置(明确定义)

curl "localhost:9200/_cluster/settings"

以平面格式列出集群范围的设置(明确定义)

curl "localhost:9200/_cluster/settings?flat_settings"

列出集群范围的设置(包括默认值)

curl "localhost:9200/_cluster/settings?include_defaults"

以平面格式列出集群范围的设置(包括默认值)

curl "localhost:9200/_cluster/settings?include_defaults&flat_settings"

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值