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

通过 API 访问实例

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

首次发布时间2021.11.02 17:52:23

Elasticsearch 提供了功能全面的 RESTful API 与实例交互。火山引擎云搜索服务提供 VPC 的接入地址,您可以通过与该地址在同一 VPC 下的 ECS 访问实例。本文介绍如何通过 API 访问 ES 实例。

注意事项

访问前请确认安全组对应端口是否已经放开。

查看访问地址

  1. 登录云搜索服务控制台
  2. 实例列表 V2页面,单击目标实例名称。
  3. 实例信息页面的服务访问区域,获取实例访问地址。

测试访问

您可通过 curl 命令测试实例是否可以正常访问,不支持通过 ping 测试连通性。

  • 对于已开启 ES 实例用户登录验证的实例,登录时需要用户名和密码认证,具体命令如下:
curl action -u user:password host

user:登录实例的访问用名。 
password:登录实例用户名的密码。
host:实例的内网或公网访问地址,可在实例信息页面获取。
  • 对于已启用 HTTPS 协议传输的实例,如果想快速访问测试,可以在 curl 命令中加入 --insecure 参数。

本文中以内网地址访问为例,说明访问命令。

注意

以下命令以 7.10.2 版本 ES 实例示例,部分命令低版本不支持。

输入以下命令:

# 默认开启 ES 用户登录认证,请注意输入用户名密码。
curl -XGET  -u user:password http://192.168.*.*:9200

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

{
  "name" : "es-master-wq83vr6wv6w0****-2",
  "cluster_name" : "wq83vr6wv6w0d1hu",
  "cluster_uuid" : "97uFBD01TZuA_pyDT6****",
  "version" : {
    "number" : "7.10.2",
    "build_flavor" : "oss",
    "build_type" : "tar",
    "build_hash" : "2f32220",
    "build_date" : "2019-04-02T15:59:27.961366Z",
    "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 的文档。

# 默认开启 ES 用户登录认证,请注意输入用户名密码。
curl -XPUT -u admin:P_ssw0rd 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
}

创建多个文档

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

# 默认开启 ES 用户登录认证,请注意输入用户名密码。
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 的文档。
更新文档后,返回如下类似代码:

{
"_index":"customer",
"_type":"_doc",
"_id":"1",
"_version":1,
"result":"updated",
"_shards":{
    "total":2,
    "successful":1,
    "failed":0
},
"_seq_no":9,
"_primary_term":1
}

查询文档

查询指定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" : "黄金会员"
        }
      }
    ]
  }
} 

复杂查询

您可以构建命令,实现更为复杂的查询。
例如,一般较为熟悉的 SQL 查询语句select * from customer where level.viplevel=5。该语句便可以通过构建以下命令来实现相同的查询结果。

curl -XGET -u admin:P_ssw0rd  'https://192.168.*.*:9200/customer/_doc/_search?pretty' -H 'Content-Type: application/json' -d '
{
  "query" : {
      "constant_score" : { 
          "filter" : {
              "term" : { 
                  "level.viplevel" : 5
              }
          }
      }
  }
}'

返回信息如下:

{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "customer",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "name" : "王五",
          "address" : "浙江省内杭州市滨江区",
          "city" : "杭州",
          "age" : "30",
          "birthday" : "1991-05-05",
          "level" : {
            "viplevel" : 5,
            "viptype" : "钻石会员"
          }
        }
      }
    ]
  }
}

聚合查询

您可以构建命令,实现复杂的聚合查询。
例如,一般较为熟悉的 SQL 查询语句select level.viplevel, count(1) from customer group by level.viplevel。该语句便可以通过构建以下命令来实现相同的查询结果。

curl -XGET -u admin:P_ssw0rd  'https://192.168.*.*:9200/customer/_doc/_search?pretty' -H 'Content-Type: application/json' -d'
{
  "size" : 0,
  "aggs" : { 
      "vip_level" : { 
          "terms" : { 
            "field" : "level.viplevel"
          }
      }
  }
}'

返回信息如下:

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "vip_level" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : 3,
          "doc_count" : 2
        },
        {
          "key" : 5,
          "doc_count" : 1
        }
      ]
    }
  }
}

删除文档

删除单个文档

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

返回信息如下:

{
  "_index" : "customer",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 7,
  "result" : "deleted",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 10,
  "_primary_term" : 1
}

删除索引

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

返回信息如下:

{
    "acknowledged" : true
}