栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

es 7.x 文档的创建

es 7.x 文档的创建

文章目录

创建文档创建文档的非幂等性创建文档自定义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
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/701542.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号