前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Elasticsearch(二)——Rest API

Elasticsearch(二)——Rest API

作者头像
羊羽shine
发布2019-05-29 22:24:08
1.1K0
发布2019-05-29 22:24:08
举报
文章被收录于专栏:Golang开发Golang开发

RESTful 接口 URL 的格式是: http://cluster的地址: 9200/<index>I<type>I [<id>] 其中,index, type 是必须提供的( index 可以理解为数据库;type 理解为数据表); id 是可选的(相当于数据库表中记 录的主键是唯一的。如果不提供, Elasticsearch 会向动生成。增 、删、改,查分别对应 HTTP 请求的 PUT 、DELETE、POST、GET方法。

Kibana DevTools

image.png

创建数据

创建index是province,type是citys ,当前id为1的document数据

代码语言:javascript
复制
PUT /province/citys/1
{
  "name":"北京",
  "code":"010"
}

返回结果

代码语言:javascript
复制
{
  "_index": "province",
  "_type": "citys",
  "_id": "1",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 0,
  "_primary_term": 1
}

es自动生成id创建数据,自动生成的id 长度是20个字符,采用GUID算法,保证分布式系统,不同节点同一个时间点创建_id`生成不可能发生冲突

代码语言:javascript
复制
POST /province/citys/
{
  "name":"上海",
  "code":"021"
}

返回结果

代码语言:javascript
复制
{
  "_index": "province",
  "_type": "citys",
  "_id": "VV6_G2YBajYJa8_UqerL",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 0,
  "_primary_term": 1
}
修改数据

只想修改id唯一的field是name的值

代码语言:javascript
复制
POST /province/citys/1/_update
{
  "doc":{
    "name":"广州"
  }
}

删除字段

代码语言:javascript
复制
curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
    "script" : "ctx._source.remove(\"name_of_field\")"
}
替换数据
代码语言:javascript
复制
PUT /province/citys/1
{
  "name":"广州"
}

这个行为是替换数据,和修改数据是两种不同形式的数据更新

删除数据
代码语言:javascript
复制
DELETE /province/citys/1

返回结果

代码语言:javascript
复制
{
  "_index": "province",
  "_type": "citys",
  "_id": "1",
  "_version": 7,
  "result": "deleted",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 6,
  "_primary_term": 1
}

curl命令行

创建索引

代码语言:javascript
复制
$ curl -XPUT "http://127.0.0.1:9200/school"
{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "school"
}

删除索引

代码语言:javascript
复制
curl -XDELETE http://localhost:9200/school
{"acknowledged":true}

插入数据

指定id

代码语言:javascript
复制
$ curl -H "Content-Type:application/json"  -XPUT http://localhost:9200/school/student/1 -d '{"name":"xiaoming","age":20}'
{
    "_index": "school",
    "_type": "student",
    "_id": "1",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}

不指定id

代码语言:javascript
复制
curl -H "Content-Type:application/json"  -XPOST http://localhost:9200/school/student/ -d '{"name":"xiaowang","age":18}'
{
    "_index": "school",
    "_type": "student",
    "_id": "kYeii2UBT-VMLOAXEoWx",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}

修改数据

代码语言:javascript
复制
curl -H "Content-Type:application/json"  -XPOST http://localhost:9200/school/student/1/_update -d '{"doc":{"name":"xiaohong","age":21}}'
{
    "_index": "school",
    "_type": "student",
    "_id": "1",
    "_version": 2,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 1,
    "_primary_term": 1
}

查询数据

代码语言:javascript
复制
curl -XGET http://localhost:9200/school/student/1
{
    "_index": "school",
    "_type": "student",
    "_id": "1",
    "_version": 2,
    "found": true,
    "_source": {
        "name": "xiaohong",
        "age": 21
    }
}

删除

删除数据

代码语言:javascript
复制
curl -XDELETE http://localhost:9200/school/student/1
{
    "_index": "school",
    "_type": "student",
    "_id": "1",
    "_version": 3,
    "result": "deleted",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 2,
    "_primary_term": 1
}

删除Type

代码语言:javascript
复制
curl -H "Content-Type:application/json" -XPOST http://localhost:9200/school/student/_delete_by_query  -d '{"query":{"match_all":{}}}'
{
    "took": 40,
    "timed_out": false,
    "total": 1,
    "deleted": 1,
    "batches": 1,
    "version_conflicts": 0,
    "noops": 0,
    "retries": {
        "bulk": 0,
        "search": 0
    },
    "throttled_millis": 0,
    "requests_per_second": -1,
    "throttled_until_millis": 0,
    "failures": []
}

bulk批量导入

代码语言:javascript
复制
POST _bulk
{ "index" : { "_index" : "test", "_type" : "_doc", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_type" : "_doc", "_id" : "2" } }
{ "create" : { "_index" : "test", "_type" : "_doc", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_type" : "_doc", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }

导入结果

代码语言:javascript
复制
{
  "took": 176,
  "errors": false,
  "items": [
    {
      "index": {
        "_index": "test",
        "_type": "_doc",
        "_id": "1",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 0,
        "_primary_term": 1,
        "status": 201
      }
    },
    {
      "delete": {
        "_index": "test",
        "_type": "_doc",
        "_id": "2",
        "_version": 1,
        "result": "not_found",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 0,
        "_primary_term": 1,
        "status": 404
      }
    },
    {
      "create": {
        "_index": "test",
        "_type": "_doc",
        "_id": "3",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 0,
        "_primary_term": 1,
        "status": 201
      }
    },
    {
      "update": {
        "_index": "test",
        "_type": "_doc",
        "_id": "1",
        "_version": 2,
        "result": "updated",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 1,
        "_primary_term": 1,
        "status": 200
      }
    }
  ]
}

reindex数据迁移

代码语言:javascript
复制
POST _reindex
{
  "source": {
    "index": "twitter"
  },
  "dest": {
    "index": "new_twitter"
  }
}

批量导入数据

代码语言:javascript
复制
POST /blog/paper/_bulk
{"index":{"_id":1}}
{"pID":"7ec0e0e5","uID":1,"publish":false,"date":"2018-01-02"}
{"index":{"_id":2}}
{"pID":"69590fe7","uID":2,"publish":true,"date":"2018-10-11"}
{"index":{"_id":3}}
{"pID":"aec30fa2","uID":1,"publish":false,"date":"2018-06-25"}
{"index":{"_id":4}}
{"pID":"a8733527","uID":3,"publish":false,"date":"2017-01-16"}

查看es状态

查看es的健康状态

代码语言:javascript
复制
GET _cat/health?v

查看es的index

代码语言:javascript
复制
GET _cat/indices
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.09.27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Kibana DevTools
    • 修改数据
    • curl命令行
    • 删除索引
    • 插入数据
    • 修改数据
    • 查询数据
    • 删除
    • 删除Type
    • bulk批量导入
    • reindex数据迁移
    • 批量导入数据
    • 查看es状态
    相关产品与服务
    数据库
    云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档