Saul's blog Saul's blog
首页
后端
分布式
前端
更多
分类
标签
归档
友情链接
关于
GitHub (opens new window)

Saul.J.Wu

立身之本,不在高低。
首页
后端
分布式
前端
更多
分类
标签
归档
友情链接
关于
GitHub (opens new window)
  • Java入门基础

  • Java核心基础

  • 设计模式

  • Web开发

  • SpringBoot

  • 微服务

  • Elasticsearch

    • ElasticSearch-简介
    • Elasticsearch-安装
    • Elasticsearch-_cat
    • Elasticsearch-新增数据
      • 索引一个文档(保存)
      • PUT请求
        • 第一次PUT请求带ID
        • 第二次PUT请求带ID
        • PUT请求不带ID
      • POST请求
        • POST请求不带ID
        • POST请求带ID
        • POST第二次请求带ID
      • 总结
    • Elasticsearch-查询数据&乐观锁
    • Elasticsearch-更新文档
    • Elasticsearch-删除操作
    • Elasticsearch-Bulk 批量 API
    • Elasticsearch-Search API
    • Elasticsearch-Query DSL
    • Elasticsearch-aggregations聚合分析
    • Elasticsearch-mapping映射
    • Elasticsearch-分词
    • SpringBoot整合Elasticsearch Rest Client
    • 商品上架与ES
  • 运维

  • 后端
  • Elasticsearch
SaulJWu
2020-11-24

Elasticsearch-新增数据

# 索引一个文档(保存)

在ES中,索引就是保存的意思,index一个文档,就是保存一个数据的意思 保存一个数据,ES要指定保存在哪个索引哪个类型下,指定用哪个唯一标识 翻译成MySQL的意思是 保存一个数据,要指定保存在哪个数据库哪张表下

比如:

  • PUT customer/external/1

意思是在customer索引下的external类型下保存数据。

ES里面都是JSON文档,所以我们需要用JSON格式发送PUT请求。

# PUT请求

# 第一次PUT请求带ID

http://192.168.56.10:9200/customer/external/1
1
{
    "name":"Jhon Doe"
}
1
2
3
  • 返回Status为201,Created
{
    "_index": "customer", // 称为元数据
    "_type": "external", // 称为类型
    "_id": "1",          // 是主键
    "_version": 1,      // 是版本
    "result": "created", //第一次为`created`新建
    "_shards": {       //集群分片信息,后期再拓展
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 第二次PUT请求带ID

如果再发送一次请求

返回Status为200,OK

{
    "_index": "customer",
    "_type": "external",
    "_id": "1",
    "_version": 2,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 1,
    "_primary_term": 1
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

发现result字段变成updated,_version变为2

# PUT请求不带ID

http://192.168.56.10:9200/customer/external
1
{
    "name":"Jhon Doe"
}
1
2
3
  • 返回
{
    "error": "Incorrect HTTP method for uri [/customer/external] and method [PUT], allowed: [POST]",
    "status": 405
}
1
2
3
4

# POST请求

# POST请求不带ID

  • 请求
http://192.168.56.10:9200/customer/external
1
{
    "name":"Jhon Doe"
}
1
2
3
  • 返回Status是201,Created
{
    "_index": "customer",
    "_type": "external",
    "_id": "xW59-3UBveHmpGGJvjVA",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 2,
    "_primary_term": 1
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  • 多次发送,发现每次id都不一样,自动生成
{
    "_index": "customer",
    "_type": "external",
    "_id": "xm6A-3UBveHmpGGJbjUo",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 4,
    "_primary_term": 1
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# POST请求带ID

  • 请求
http://192.168.56.10:9200/customer/external/3
1
{
    "name":"Jhon Doe"
}
1
2
3
  • 返回
{
    "_index": "customer",
    "_type": "external",
    "_id": "3",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 7,
    "_primary_term": 1
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# POST第二次请求带ID

{
    "_index": "customer",
    "_type": "external",
    "_id": "3",
    "_version": 2,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 8,
    "_primary_term": 1
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 总结

  • POST和PUT请求都可以新增和更新操作
  • PUT请求必须带上ID,如果数据库中不存在该ID的数据,是新增操作,否则是更新操作。
  • POST请求可以不指定ID,会自动生成,是新增操作
  • POST请求指定ID时,如果数据库中不存在该ID的数据,是新增操作,否则是更新操作。
  • 每次更新操作会刷新版本_version,自动+1
  • 创建资源返回201,Created,result字段为created
  • 更新资源返回200,OK,result字段为OK
帮我改善此页面 (opens new window)
#Elasticsearch#索引文档#保存#新增数据
上次更新: 2020/12/17, 08:38:35
Elasticsearch-_cat
Elasticsearch-查询数据&乐观锁

← Elasticsearch-_cat Elasticsearch-查询数据&乐观锁→

最近更新
01
zabbix学习笔记二
02-28
02
zabbix学习笔记一
02-10
03
Linux访问不了github
12-08
更多文章>
Theme by Vdoing | Copyright © 2020-2022 Saul.J.Wu | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式