Teng's blog Teng's blog
首页
Java
H5前端
GitHub (opens new window)
首页
Java
H5前端
GitHub (opens new window)
  • 认知

  • 入门

    • 环境准备
    • RESTful&JSON
    • 客户端工具
    • 倒排索引
    • 数据格式
    • RESTful-API

      • 索引-创建
      • 索引-查询
      • 索引-删除
      • 文档-创建
        • POST创建
          • 系统主键
          • 自定义主键
        • PUT创建
          • 系统主键
          • 自定义主键
        • 分析
          • POST和PUT的区别
          • 关于索引
      • 文档-主键查询
      • 文档-全量查询
      • 文档-删除
      • 文档-修改
      • 映射-创建
      • 映射-查询
      • DSL-数据准备
      • DSL-全量查询
      • DSL-匹配查询
      • DSL-关键字查询
      • DSL-指定返回字段
      • DSL-组合查询
      • DSL-范围查询
      • DSL-模糊查询
      • DSL-排序
      • DSL-高亮
      • DSL-分页查询
      • DSL-聚合查询
    • Java-API

  • 环境

  • 进阶

  • 框架集成

  • 优化

  • 面试题

  • Database-Elasticsearch
  • 入门
  • RESTful-API
Shetengteng
2022-01-23

文档-创建

先创建shopping索引后,执行添加文档操作,文档可以类比为关系型数据库中的表数据,添加的数据格式为 JSON 格式

# POST创建

# 系统主键

在 Postman 中,向 ES 服务器发 POST 请求 : http://localhost:9200/shopping/_doc

POST http://localhost:9200/shopping/_doc
1

请求体JSON内容为

{
    "title":"小米手机",
    "category":"小米",
    "images":"http://my-notes.cn/xm.jpg",
    "price":3999.00
}
1
2
3
4
5
6

返回信息

  • 数据创建后,由于没有指定数据唯一性标识(ID),默认情况下,ES 服务器会随机生成一个
{
    "_index": "shopping",	//索引
    "_type": "_doc",		//类型-文档
    "_id": "rUbGj34B0YKnOXrrX_cl",	//唯一标识,类比MySQL中的主键,随机生成
    "_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

此时再次使用POST创建,依然可以创建成功

示例

# 自定义主键

如果要使用自定义主键,那么需要修改请求信息

POST http://localhost:9200/shopping/_doc/1
1

发送JSON

{
    "title":"小米手机",
    "category":"小米",
    "images":"http://xxx.jpg",
    "price":1999.00
}
1
2
3
4
5
6

返回信息

{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "1", // 此时id是自定义的id
    "_version": 2,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 3,
    "_primary_term": 1
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# PUT创建

# 系统主键

使用PUT创建文档,并且使用系统自带主键,则会失败,报错

PUT http://localhost:9200/shopping/_doc
1

请求体

{
    "title":"小米手机",
    "category":"小米",
    "images":"http://xxx.jpg",
    "price":1999.00
}
1
2
3
4
5
6

返回信息

{
    "error": "Incorrect HTTP method for uri [/shopping/_doc] and method [PUT], allowed: [POST]",
    "status": 405
}
1
2
3
4

# 自定义主键

在创建包含自定义主键标识的时候,可以使用put请求

PUT http://localhost:9200/shopping/_doc/1
1

请求体

{
    "title":"小米手机",
    "category":"小米",
    "images":"http://xxx.jpg",
    "price":1999.00
}
1
2
3
4
5
6

返回

{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "1",
    "_version": 4, 			// 注意 version的变化
    "result": "updated", // 注意 返回了updated
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 6,
    "_primary_term": 1
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

主键存在则是更新操作,主键不存在则是创建操作

# 分析

# POST和PUT的区别

POST的语义没有幂等性

  • 使用POST创建文档时,意味着资源会有改变
  • 每次POST都不保证幂等性,因此指定主键id还是不指定主键id都与语义不冲突

PUT有幂等性

  • 每次重复调用PUT请求时,重复执行时不会对资源造成多次更改,只会更改一次
  • 指定主键id进行PUT操作,如果资源不存在,则创建,如果存在则修改,每次对资源操作都有幂等性
  • 如果不指定主键id进行PUT操作,那么与语义冲突,多次执行,ES不知道对哪个资源进行创建和修改

# 关于索引

此时查看之前的索引信息,可以看到详细的schema信息

GET http://localhost:9200/shopping
1

返回

{
    "shopping": {
        "aliases": {},
        "mappings": {
            "properties": {
                "category": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "images": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "price": {
                    "type": "float"
                },
                "title": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                }
            }
        },
        "settings": {
            "index": {
                "creation_date": "1643087559769",
                "number_of_shards": "1",
                "number_of_replicas": "1",
                "uuid": "OM-2CkBbSsyZTri3ZMnRNQ",
                "version": {
                    "created": "7080099"
                },
                "provided_name": "shopping"
            }
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
Last Updated: 2022/02/05, 15:58:51
索引-删除
文档-主键查询

← 索引-删除 文档-主键查询→

Theme by Vdoing | Copyright © 2021-2022 Shetengteng | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式