前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Elasticsearch简单API调用

Elasticsearch简单API调用

作者头像
零月
发布2018-04-25 16:19:44
1.2K0
发布2018-04-25 16:19:44
举报
文章被收录于专栏:从零开始的linux从零开始的linux
代码语言:javascript
复制
#集群健康状态:
[root@localhost elasticsearch-5.4.1]# curl -XGET '128.0.0.101:9200/_cat/health?v&pretty'
epoch      timestamp cluster       status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1516792174 19:09:34  elasticsearch yellow          1         1      5   5    0    0        5             0                  -                 50.0%
#集群节点列表:
[root@localhost elasticsearch-5.4.1]# curl -XGET '128.0.0.101:9200/_cat/nodes?v&pretty'
ip          heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
128.0.0.101            6          82   0    0.00    0.00     0.00 mdi       *      u9dcelG
#查看索引列表:
[root@localhost elasticsearch-5.4.1]# curl -XGET '128.0.0.101:9200/_cat/indices?v&pretty'
health status index uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   test  93bbE_eXTtm_mVvujlxxVA   5   1          0            0       650b           650b
#创建索引
[root@localhost elasticsearch-5.4.1]# curl -XPUT '128.0.0.101:9200/test?pretty&pretty'
{
  "acknowledged" : true,
  "shards_acknowledged" : true
}
#创建索引并添加文档
[root@localhost elasticsearch-5.4.1]# curl -XPUT '128.0.0.101:9200/test/external/1?pretty&pretty' -d'
> {
> "name":"elk test"
> }'
{
  "_index" : "test",
  "_type" : "external",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "created" : true
}
代码语言:javascript
复制
#查看索引
[root@localhost elasticsearch-5.4.1]# curl -XGET '128.0.0.101:9200/test/external/1?pretty&pretty'
{
  "_index" : "test",
  "_type" : "external",
  "_id" : "1",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "name" : "elk test"
  }
}
#删除索引
[root@localhost elasticsearch-5.4.1]# curl -XDELETE '128.0.0.101:9200/test/external/1?pretty&pretty'
{
  "found" : true,
  "_index" : "test",
  "_type" : "external",
  "_id" : "1",
  "_version" : 2,
  "result" : "deleted",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  }
}

#总结格式
<REST Verb>/<Index>/<Type>/<ID>
#替换文档
[root@localhost elasticsearch-5.4.1]# curl -XPUT '128.0.0.101:9200/test/external/1?pretty&pretty' -d'
{
"name":"elk test5"
}'
{
  "_index" : "test",
  "_type" : "external",
  "_id" : "1",
  "_version" : 9,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "created" : false
}

#没创建索引并添加文档(不显示指定ID)
[root@localhost elasticsearch-5.4.1]# curl -XPOST '128.0.0.101:9200/test/external?pretty&pretty' -d'
{
"name":"elk test3"
}'
{
  "_index" : "test",
  "_type" : "external",
  "_id" : "AWEpYQSdAcj1E5z65ZJt",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "created" : true
}
#update更新
[root@localhost elasticsearch-5.4.1]# curl -XPOST '128.0.0.101:9200/test/external/1/_update?pretty&pretty' -d'
{
"doc":{"name":"elk test5"}
}'
{
  "_index" : "test",
  "_type" : "external",
  "_id" : "1",
  "_version" : 3,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  }
}
#添加字段
[root@localhost elasticsearch-5.4.1]# curl -XPOST '128.0.0.101:9200/test/external/1/_update?pretty&pretty' -d'
{
"doc":{"name":"elk test5","age":18}
}'
{
  "_index" : "test",
  "_type" : "external",
  "_id" : "1",
  "_version" : 4,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  }
}
[root@localhost elasticsearch-5.4.1]# curl -XGET '128.0.0.101:9200/test/external/1?pretty&pretty'
{
  "_index" : "test",
  "_type" : "external",
  "_id" : "1",
  "_version" : 4,
  "found" : true,
  "_source" : {
    "name" : "elk test5",
    "age" : 18
  }
}
#用脚本
[root@localhost elasticsearch-5.4.1]# curl -XPOST '128.0.0.101:9200/test/external/1/_update?pretty&pretty' -d'
{
"script":"ctx._source.age+=5"
}'
{
  "_index" : "test",
  "_type" : "external",
  "_id" : "1",
  "_version" : 5,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  }
}
#批量处理
[root@localhost elasticsearch-5.4.1]# curl -XPOST '128.0.0.101:9200/test/external/_bulk?pretty&pretty' -d'
> {"index":{"_id":"1"}}
> {"name":"elk bulk1"}
> {"index":{"_id":"2"}}
> {"name":"elk bulk2"}'
{
  "took" : 9,
  "errors" : false,
  "items" : [
    {
      "index" : {
        "_index" : "test",
        "_type" : "external",
        "_id" : "1",
        "_version" : 6,
        "result" : "updated",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "created" : false,
        "status" : 200
      }
    }
  ]
}
#更新删除
[root@localhost elasticsearch-5.4.1]# curl -XPOST '128.0.0.101:9200/test/external/_bulk?pretty&pretty' -d'
{"update":{"_id":"1"}}
{"doc":{"name":"elk bulk1 is becoming"}}
{"delete":{"_id":"2"}}'
{
  "took" : 8,
  "errors" : false,
  "items" : [
    {
      "update" : {
        "_index" : "test",
        "_type" : "external",
        "_id" : "1",
        "_version" : 7,
        "result" : "updated",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "status" : 200
      }
    }
  ]
}
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-01-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 从零开始的linux 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
Elasticsearch Service
腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档