您当前的位置: 首页 > 

java持续实践

暂无认证

  • 2浏览

    0关注

    746博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

es 7.x 文档的创建

java持续实践 发布时间:2022-01-09 12:34:33 ,浏览量:2

文章目录
      • 创建文档
      • 创建文档的非幂等性
      • 创建文档自定义id
        • 创建文档的url中, 使用_create创建索引

创建文档

由于7.x的es中已经没有了type的概念, 因此添加数据是直接在索引中添加数据,

post请求 url: http://127.0.0.1:9200/shopping/_doc _doc代表创建文档. 前面的代表给shopping这个索引创建文档 请求体中的数据如下

{
    "title": "小米手机",
    "category": "小米",
    "images": "http://xiaomi.com",
    "price": 3999.00
}

响应结果如下

{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "ex0KPX4BPkzBbPhZJmdM",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}

代表创建成功.

创建文档的非幂等性

由于es创建文档的post请求是非幂等性的, 如果上一步的相同的请求内容再执行一次, 得到的响应如下:

{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "fB0NPX4BPkzBbPhZNmcn",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 1,
    "_primary_term": 1
}

可以看到返回的id是fB0NPX4BPkzBbPhZNmcn , 与第一次执行返回的id不同, 代表在es中又创建了一个新的文档. 如果此时执行put请求, 会提示报错如下, 该请求url不支持put请求, 因为put请求是幂等性的. 在这里插入图片描述

创建文档自定义id

在上面创建文档的结果中, 可以看到返回的id是类似一个uuid的, 如果想用数据自己的id也是可行的.

使用post请求 uri: http://127.0.0.1:9200/shopping/_doc/1 请求体:

{
    "title": "小米手机",
    "category": "小米",
    "images": "http://xiaomi.com",
    "price": 3999.00
}

返回结果如下:

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

如果再次执行相同的请求, 返回结果如下.

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

可以看到result是updated, version 变成了2. 执行了更新的操作.

如果用put请求方式, url后面是带id 的, http://127.0.0.1:9200/shopping/_doc/1 返回结果如下, 也是更新操作.

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

如果put请求, 用新的id url如下 http://127.0.0.1:9200/shopping/_doc/2 请求体:

{
    "title": "小米手机",
    "category": "小米",
    "images": "http://xiaomi.com",
    "price": 3999.00
}

响应: 代表创建了一个id为2的文档

{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "2",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 5,
    "_primary_term": 1
}
创建文档的url中, 使用_create创建索引

使用put请求, url路径如下 http://127.0.0.1:9200/shopping/_create/3

请求体如下 :

{
    "title": "小米手机",
    "category": "小米",
    "images": "http://xiaomi.com",
    "price": 3999.00
}

响应结果如下 : 成功创建索引, type为 “_doc”

{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "3",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 6,
    "_primary_term": 1
}

注意: _create操作是幂等性的, 只支持成功一次, 如果重复_create操作则会报错. 报错信息如下. 而不会像多次执行_doc操作那样, 执行更新数据

{
    "error": {
        "root_cause": [
            {
                "type": "version_conflict_engine_exception",
                "reason": "[3]: version conflict, document already exists (current version [2])",
                "index_uuid": "mrNaEayCTDaE7VB9sq-zUQ",
                "shard": "0",
                "index": "shopping"
            }
        ],
        "type": "version_conflict_engine_exception",
        "reason": "[3]: version conflict, document already exists (current version [2])",
        "index_uuid": "mrNaEayCTDaE7VB9sq-zUQ",
        "shard": "0",
        "index": "shopping"
    },
    "status": 409
}
关注
打赏
1658054974
查看更多评论
立即登录/注册

微信扫码登录

0.0439s