zoukankan      html  css  js  c++  java
  • Elasticsearch REST API小记

    Elasticsearch REST API小记


    集群健康状态

    [root@elastic1 ~]# curl 'http://192.168.8.101:9200/_cat/health?v'

    epoch      timestamp cluster               status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent 

    1462196271 21:37:51  elasticsearch_cluster green                                                                                   100.0% 


    列出所有目录

    [root@elastic1 ~]# curl 'http://192.168.8.101:9200/_cat/indices?v'

    health status index pri rep docs.count docs.deleted store.size pri.store.size 


    创建Index

    [root@elastic1 ~]# curl -XPUT 'http://192.168.8.101:9200/customer?pretty'

    {

      "acknowledged" : true

    }

    [root@elastic1 ~]# curl 'http://192.168.8.101:9200/_cat/indices?v'

    health status index    pri rep docs.count docs.deleted store.size pri.store.size 

    yellow open   customer                               650b           650b 

    提示:health为yellow, 是因为目前是单节点,等加入了replica之后就会转为green


    查询Document

    [root@elastic1 ~]# curl -XPUT 'http://192.168.8.101:9200/customer/external/1?pretty' -d '

    {

      "name": "John Doe"

    }'

    {

      "_index" : "customer",

      "_type" : "external",

      "_id" : "1",

      "_version" : 1,

      "_shards" : {

        "total" : 2,

        "successful" : 1,

        "failed" : 0

      },

      "created" : true

    }

    [root@elastic1 ~]# curl -XGET 'http://192.168.8.101:9200/customer/external/1?pretty'

    {

      "_index" : "customer",

      "_type" : "external",

      "_id" : "1",

      "_version" : 1,

      "found" : true,

      "_source" : {

        "name" : "John Doe"

      }

     

    }


    删除Index

    https://www.elastic.co/guide/en/elasticsearch/reference/current/_delete_an_index.html

    [root@elastic1 ~]# curl -XDELETE 'http://192.168.8.101:9200/customer?pretty'

    {

      "acknowledged" : true

    }

    [root@elastic1 ~]# curl 'http://192.168.8.101:9200/_cat/indices?v'

     

    health status index pri rep docs.count docs.deleted store.size pri.store.size


    修改数据

    https://www.elastic.co/guide/en/elasticsearch/reference/current/_modifying_your_data.html

    [root@elastic1 ~]# curl -XPUT 'http://192.168.8.101:9200/customer/external/1?pretty' -d '

    {

      "name": "John Doe"

    }'

    {

      "_index" : "customer",

      "_type" : "external",

      "_id" : "1",

      "_version" : 1,

      "_shards" : {

        "total" : 2,

        "successful" : 1,

        "failed" : 0

      },

      "created" : true

    }

    [root@elastic1 ~]# curl -XPUT 'http://192.168.8.101:9200/customer/external/1?pretty' -d '

    {

      "name": "Michal Doe"

    }'

    {

      "_index" : "customer",

      "_type" : "external",

      "_id" : "1",

      "_version" : 2,

      "_shards" : {

        "total" : 2,

        "successful" : 1,

        "failed" : 0

      },

      "created" : false

    }

    ID部分(上面标红的部分)是可选的,上面两条只会修改内容,不会新增,要新增需要不同的ID,如下

    [root@elastic1 ~]# curl -XPUT 'http://192.168.8.101:9200/customer/external/2?pretty' -d '

    {

      "name": "Michal Doe"

    }'

    {

      "_index" : "customer",

      "_type" : "external",

      "_id" : "2",

      "_version" : 1,

      "_shards" : {

        "total" : 2,

        "successful" : 1,

        "failed" : 0

      },

      "created" : true

    }

    采用POST方式时,没有指定ID,Elasticsearch会随机生成一个ID

    [root@elastic1 ~]# curl -XPOST 'http://192.168.8.101:9200/customer/external?pretty' -d '

    {

      "name": "Jane Doe"

    }'

    {

      "_index" : "customer",

      "_type" : "external",

      "_id" : "AVRx1bl1UyCnWsdaj6t4",

      "_version" : 1,

      "_shards" : {

        "total" : 2,

        "successful" : 1,

        "failed" : 0

      },

      "created" : true

    }

    [root@elastic1 ~]# curl 'http://192.168.8.101:9200/_cat/indices?v'

    health status index    pri rep docs.count docs.deleted store.size pri.store.size 

     

    yellow open   customer   5                         6.3kb          6.3kb 


    更新Document

    [root@elastic1 ~]# curl -XPOST 'http://192.168.8.101:9200/customer/external/1/_update?pretty' -d '

    {

      "doc": { "name": "Jane Doe", "age": 20 }

    }'

    {

      "_index" : "customer",

      "_type" : "external",

      "_id" : "1",

      "_version" : 3,

      "_shards" : {

        "total" : 2,

        "successful" : 1,

        "failed" : 0

      }

    }

    https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html

    也可以通过script来更新,前提是要先启用script

    config/elasticsearch.yml中加入最简的两行即可,更严格的动作限制见官网说明

    script.inline: true

     

    script.indexed: true


    [root@elastic1 ~]# curl -XPOST 'http://192.168.8.101:9200/customer/external/1/_update?pretty' -d '

    {

      "script" : "ctx._source.age += 5"

    }'

    {

      "_index" : "customer",

      "_type" : "external",

      "_id" : "1",

      "_version" : 4,

      "_shards" : {

        "total" : 2,

        "successful" : 1,

        "failed" : 0

      }

     

    }


    删除Document

    [root@elastic1 ~]# curl -XDELETE 'http://192.168.8.101:9200/customer/external/2?pretty'

    {

      "found" : true,

      "_index" : "customer",

      "_type" : "external",

      "_id" : "2",

      "_version" : 2,

      "_shards" : {

        "total" : 2,

        "successful" : 1,

        "failed" : 0

      }

     

    }

    提示:delete-by-query插件能删除所有匹配查询到的Document


    批处理

    [root@elastic1 ~]# curl -XPOST 'http://192.168.8.101:9200/customer/external/_bulk?pretty' -d '

    {"index":{"_id":"1"}}

    {"name": "John Doe" }

    {"index":{"_id":"2"}}

    {"name": "Jane Doe" }

    '

    {

      "took" : 13,

      "errors" : false,

      "items" : [ {

        "index" : {

          "_index" : "customer",

          "_type" : "external",

          "_id" : "1",

          "_version" : 5,

          "_shards" : {

            "total" : 2,

            "successful" : 1,

            "failed" : 0

          },

          "status" : 200

        }

      }, {

        "index" : {

          "_index" : "customer",

          "_type" : "external",

          "_id" : "2",

          "_version" : 1,

          "_shards" : {

            "total" : 2,

            "successful" : 1,

            "failed" : 0

          },

          "status" : 201

        }

      } ]

     

    }

    [root@elastic1 ~]# curl -XPOST 'http://192.168.8.101:9200/customer/external/_bulk?pretty' -d '

    {"update":{"_id":"1"}}

    {"doc": { "name": "John Doe becomes Jane Doe" } }

    {"delete":{"_id":"2"}}

    '

    {

      "took" : 11,

      "errors" : false,

      "items" : [ {

        "update" : {

          "_index" : "customer",

          "_type" : "external",

          "_id" : "1",

          "_version" : 6,

          "_shards" : {

            "total" : 2,

            "successful" : 1,

            "failed" : 0

          },

          "status" : 200

        }

      }, {

        "delete" : {

          "_index" : "customer",

          "_type" : "external",

          "_id" : "2",

          "_version" : 2,

          "_shards" : {

            "total" : 2,

            "successful" : 1,

            "failed" : 0

          },

          "status" : 200,

          "found" : true

        }

      } ]

     

    }

  • 相关阅读:
    Shiro笔记(三)shiroFilter拦截器配置原则
    Shiro笔记(二)Shiro集成SpringMVC的环境配置
    Shiro笔记(一)Shiro整体介绍
    javaNIO的总结
    Redis的工作流程
    Nginx的配置安装和使用
    Linux下java开发环境配置总结
    php 基础知识 post 和get 两种传输方式的区别
    php 高级 多台web服务器共享session的方法
    php 基础知识 SESSION 和 COOKIE 的区别
  • 原文地址:https://www.cnblogs.com/lixuebin/p/10814100.html
Copyright © 2011-2022 走看看