You need to enable JavaScript to run this app.
导航

通过 Curl 命令行连接实例

最近更新时间2024.04.07 19:15:43

首次发布时间2023.08.24 17:53:23

火山引擎 ES 实例默认提供私有网络访问地址,您可以登录与实例在相同 VPC 的 ECS,通过 Curl 命令行访问实例。本文介绍通过 Curl 命令行连接 ES 实例,并给出简单操作示例。

访问实例

  1. 登录相同 VPC 的 ECS 实例。相关文档,请参见登录 ECS

  2. 在 ECS 中执行以下命令,访问 ES 实例。

    • 访问 HTTP 协议实例

      curl -u <user>:<password> <host>
      
    • 访问 HTTPS 协议实例
      对于 HTTPS 协议实例,如果想快速访问,可以在 curl 命令中加入-k--insecure 参数忽略证书。

      curl -k -u <user>:<password> <host>
      

      变量

      描述

      user

      登录实例的访问用名,默认为 admin。

      password

      登录实例用户名的密码。

      host

      实例的内网或公网访问地址,可在实例信息页面获取。

    访问示例如下:

    # 默认开启用户登录认证,请注意输入用户名密码。
    curl -k -u <user>:<password> https://elasticsearch-o-00w**93s1.escloud.ivolces.com:9200
    

    返回如下类似信息,表示实例访问正常,具体参数的值会根据实例的版本有所不同。

    {
      "name" : "es-master-00**3s1-2",
      "cluster_name" : "00**3s1",
      "cluster_uuid" : "ocvwXJT**KAbAw",
      "version" : {
        "number" : "7.10.2",
        "build_flavor" : "default",
        "build_type" : "tar",
        "build_hash" : "747e1cc71def077*a59143c1f785afa92b9",
        "build_date" : "2021-01-13T00:42:12.435326Z",
        "build_snapshot" : false,
        "lucene_version" : "8.7.0",
        "minimum_wire_compatibility_version" : "6.8.0",
        "minimum_index_compatibility_version" : "6.0.0-beta1"
      },
      "tagline" : "You Know, for Search"
    }
    

创建文档

创建单个文档

输入以下命令,创建一个名为customer的索引,并在索引中添加id为 1 的文档。

# 默认开启用户登录认证,请注意输入用户名密码。
curl -XPUT -u admin:password http://192.168.*.*:9200/customer/_doc/1 -H 'Content-Type: application/json' -d '
{
  "name": "张三",
  "address":"四川省成都市武侯区",
  "city":"成都",
  "age":28,
  "birthday":"1993-08-30",
  "level.viplevel":3,
  "level.viptype":"黄金会员"
}
'

返回信息如下:

{
"_index":"customer",
"_type":"_doc",
"_id":"1",
"_version":1,
"result":"created",
"_shards":{
    "total":2,
    "successful":1,
    "failed":0
},
"created":true
}

创建多个文档

输入以下命令,在索引中批量添加文档。

# 默认开启用户登录认证,请注意输入用户名密码。
curl -XPOST -u admin:P_ssw0rd http://192.168.*.*:9200/_bulk -H 'Content-Type: application/json' -d '
{"index" : { "_index": "customer","_id":2} }
{"name":"李四","address":"北京市海淀区","city":"北京","age":"23","birthday":"1998-01-01","level":{"viplevel":3,"viptype":"黄金会员"}}
{"index" : { "_index": "customer","_id":3} }
{"name":"王五","address":"浙江省内杭州市滨江区","city":"杭州","age":"30","birthday":"1991-05-05","level":{"viplevel":5,"viptype":"钻石会员"}}
'

返回信息如下:

{"took":15,"errors":false,"items":[{"index":{"_index":"customer","_type":"_doc","_id":"2","_version":2,"result":"created","_shards":{"total":2,"successful":2,"failed":0},"_seq_no":6,"_primary_term":1,"status":200}},{"index":{"_index":"customer","_type":"_doc","_id":"3","_version":2,"result":"created","_shards":{"total":2,"successful":2,"failed":0},"_seq_no":7,"_primary_term":1,"status":200}}]}

查询文档

查询指定ID文档

curl -XGET -u admin:P_ssw0rd  'http://192.168.*.*:9200/customer/_doc/1?pretty' -H 'Content-Type: application/json'

返回信息如下:

{
  "_index" : "customer",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 6,
  "_seq_no" : 9,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "张三",
    "address" : "四川省成都市武侯区",
    "city" : "成都",
    "age" : 28,
    "birthday" : "1993-08-30",
    "level.viplevel" : 3,
    "level.viptype" : "黄金会员"
  }
}

查询全部文档

curl -XGET -u admin:P_ssw0rd  'https://192.168.*.*:9200/customer/_doc/_search?pretty' -H 'Content-Type: application/json' 

返回信息如下:

{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "customer",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "李四",
          "address" : "北京市海淀区",
          "city" : "北京",
          "age" : "23",
          "birthday" : "1998-01-01",
          "level" : {
            "viplevel" : 3,
            "viptype" : "黄金会员"
          }
        }
      },
      {
        "_index" : "customer",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "name" : "王五",
          "address" : "浙江省内杭州市滨江区",
          "city" : "杭州",
          "age" : "30",
          "birthday" : "1991-05-05",
          "level" : {
            "viplevel" : 5,
            "viptype" : "钻石会员"
          }
        }
      },
      {
        "_index" : "customer",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "张三",
          "address" : "四川省成都市武侯区",
          "city" : "成都",
          "age" : 28,
          "birthday" : "1993-08-30",
          "level.viplevel" : 3,
          "level.viptype" : "黄金会员"
        }
      }
    ]
  }
}