美文网首页
ElasticSearch之CURL操作

ElasticSearch之CURL操作

作者: simonsgj | 来源:发表于2018-11-29 17:21 被阅读0次
  • CURL的操作
    curl是利用URL语法在命令行方式下工作的开源文件传输工具,使用curl可以简单实现常见的get/post请求。简单的认为是可以在命令行下面访问url的一个工具。在centos的默认库里面是有curl工具的,如果没有请yum安装即可。
  • 命令格式:
curl -X指定http请求的方法(如HEAD GET POST PUT DELETE)-d  '指定要传输的数据'

例子:

建立索引库company(PUT和POST都可以,索引库名必须小写):

curl -XPUT 'http://localhost:9200/company'

索引库名称必须要全部小写,不能以下划线开头,也不能包含逗号

创建索引,其中employee是type,1是document,-d是指定要传输的数据(遵循JSON格式):

curl -XPOST http://localhost:9200/company/employee/1 -d 
'{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}'

创建索引

a.put创建
curl -XPUT http://localhost:9200/shb01/student/1 -d '{"name":"jack","age":30,"info":"hello elasticsearch"}'

返回:{"_index":"shb01","_type":"student","_id":"1","_version":1,"created":true}
执行put后有返回值
_index索引名称
_type类型名
_version版本号
created:true表示是新创建的。
上面的命令每执行一次version就会加1,-XPUT必须制定id。

b.post创建
curl -XPOST http://localhost:9200/shb01/student -d'{"name":"tom","age":21,"info":"tom"}'

返回:{"_index":"shb01","_type":"student","_id":"AVadzuNgxskBS1Rg2tdp","_version":1,"created":true}

http://127.0.0.1:9200/_cat/health?v  #查看es集群状态 
http://127.0.0.1:9200/_cat/nodes?v #集群节点健康查看
curl -XGET '127.0.0.1:9200/_cat/indices?v&pretty' #查询所有索引,pretty:格式化

查询返回最近10条

curl -XPOST 'localhost:9200/logstash-zhifubao-2018.09.18/_search?pretty' -d '{"query": { "match_all": {} },"from": 10,"size": 10}'

查询索引状态

curl -XGET http://localhost:9200/logstash-zhifubao-2018.08.15/_status

查询某一个索引

curl -XGET http://localhost:9200/logstash-zhifubao-2018.08.15/message/0ea1b2df-caa4-457c-8cc1-294f5e9284c7/_search?pretty

根据business_no查询,多个条件用逗号拼接

curl -XGET http://localhost:9200/logstash-zhifubao-2018.08.15/_search?q=message:0ea1b2df-caa4-457c-8cc1-294f5e9284c7

根据business_no查询,只返回特定字段

curl -XGET http://localhost:9200/logstash-zhifubao-2018.08.15/_search?q=message:0ea1b2df-caa4-457c-8cc1-294f5e9284c7?_source=message

查询集群状态

Curl –XGET http://localhost:9200/_cluster/health?pretty
http://localhost:9200/_cluster/health?pretty

多索引,多类型查询,分页查询,超时

Curl:curl -XGET http://localhost:9200/shb01,shb02/stu,tea/_search?pretty
curl -XGET http://localhost:9200/_all/stu,tea/_search?pretty

分页

curl -XGET http://localhost:9200/shb01/stu/_search?size=2&from=0

更新部分字段

crul –XPUT http:localhost:9200/shb01/student/1/_update?version=1
–d ‘{“doc”:{“name”:”updatename”}

根据id删除

curl -XDELETE http://localhost:9200/shb01/student/AVad05EExskBS1Rg2tdq

删除所有的索引库中名称为tom的文档

curl -XDELETE http://localhost:9200/_all/_query?q=name:tom

批处理

  • a.在/usr/local/下新建t.txt文件,文件内容为
{"index":{"_index":"shb01","_type":"student","_id":"1"}}
{"name":"st01","age":"10","info":"st01"}
{"create":{"_index":"shb100","_type":"student","_id":"2"}}
{"name":"tea01","age":"10","info":"tea01"}
{"delete":{"_index":"shb01","_type":"student","_id":"AVadzuNgxskBS1Rg2tdp"}
{"update":{"_index":"shb02","_type":"tea","_id":"1"}}
{"doc":{"name":"zszszszs"}}
  • b.执行批处理命令,关键字_bulk
curl -XPUThttp://localhost:9200/_bulk --data-binary @/usr/local/t

_cluster系列

查询设置集群状态

curl  -XGET localhost:9200/_cluster/health?pretty=true
curl -XGET localhost:9200/_cluster/stats?pretty=true  # 显示集群系统信息,包括CPU JVM等等 
curl -XGET localhost:9200/_cluster/pending_tasks?pretty=true  #获取集群堆积的任务

pretty=true表示格式化输出
level=indices 表示显示索引状态
level=shards 表示显示分片信息

索引参数相关

URL 说明

  • /index/_search 不解释
  • /_aliases 获取或操作索引的别名
  • /index/
  • /index/type/ 创建或操作类型
    /index/_mapping 创建或操作mapping
  • /index/_settings 创建或操作设置(number_of_shards是不可更改的)
  • /index/_open 打开被关闭的索引
  • /index/_close 关闭索引
  • /index/_refresh 刷新索引(使新加内容对搜索可见)
  • /index/_flush
    刷新索引
    将变动提交到lucene索引文件中
    并清空elasticsearch的transaction log,
    与refresh的区别需要继续研究
  • /index/_optimize 优化segement,个人认为主要是对segement进行合并
  • /index/_status 获得索引的状态信息
  • /index/_segments 获得索引的segments的状态信息
  • /index/_explain 不执行实际搜索,而返回解释信息
  • /index/_analyze 不执行实际搜索,根据输入的参数进行文本分析
  • /index/type/id 操作指定文档,不解释
  • /index/type/id/_create 创建一个文档,如果该文件已经存在,则返回失败
  • /index/type/id/_update 更新一个文件,如果改文件不存在,则返回失败

集群参数相关

URL 说明

  • /_cluster/nodes 获得集群中的节点列表和信息
  • /_cluster/health 获得集群信息
  • /_cluster/state 获得集群里的所有信息(集群信息、节点信息、mapping信息等)

Nodes参数相关

URL 说明

  • /_nodes/process 我主要看file descriptor 这个信息
  • /_nodes/process/stats 统计信息(内存、CPU能)
  • /_nodes/jvm 获得各节点的虚拟机统计和配置信息
  • /_nodes/jvm/stats 更详细的虚拟机信息
  • /_nodes/http 获得各个节点的http信息(如ip地址)
  • /_nodes/http/stats 获得各个节点处理http请求的统计情况
  • /_nodes/thread_pool
    获得各种类型的线程池
    (elasticsearch分别对不同的操作提供不同的线程池)的配置信息
  • /_nodes/thread_pool/stats 获得各种类型的线程池的统计信息

以上这些操作和可以通过如

  • /_nodes/${nodeId}/jvm/stats
  • /_nodes/${nodeip}/jvm/stats
  • /_nodes/${nodeattribute}/jvm/stats
    的形式针对指定节点的操作。

相关文章

  • ElasticSearch之CURL操作

    CURL的操作curl是利用URL语法在命令行方式下工作的开源文件传输工具,使用curl可以简单实现常见的get/...

  • elasticsearch入门备忘录(curl操作)

    elasticsearch入门备忘录(curl操作) 主要记录在windows下安装 crul 以及windows...

  • 搜索-Elasticsearch基础

    Elasticsearch基本操作 查看集群信息 命令行curl或者浏览器查看es集群节点信息 curlhttp:...

  • elasticsearch使用

    ./bin/elasticsearch 查看服务情况 curl -XGET 'localhost:9200/?pr...

  • ElasticSearch

    ps aux | grep elasticsearch kill -15 pid curl 'http://loc...

  • elasticsearch

    elasticsearch使用 查看集群健康情况curl 'localhost:9200/_cat/health?...

  • Elasticsearch CURL命令

    1、查看集群状态 提示:绿色表示一切正常, 黄色表示所有的数据可用但是部分副本还没有分配,红色表示部分数据因为某些...

  • ElasticSearch之映射常用操作

    本文案例操作,建议先阅读我之前的文章《ElasticSearch之安装及基本操作API》 Mapping (映射)...

  • ElasticSearch 日常小结

    curl操作es . 删除curl -XDELETE http://192.168.80.200:9200/goo...

  • php的curl基本使用方法

    php当中可以使用curl进行http操作。核心使用到4个函数curl_init()对curl操作进行初始化cur...

网友评论

      本文标题:ElasticSearch之CURL操作

      本文链接:https://www.haomeiwen.com/subject/dsbfcqtx.html