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

ES之文档创建(入门)

ES之文档创建(入门)

ES之文档创建(入门)

一、生成随机id

发送【POST】请求:http://127.0.0.1:9200/test-index-3/_doc,参数如下

{
    "title":"test",
    "num":1,
    "date":"20211213"
}

会创建一条随机id的数据,返回结果如下,多次执行会生成多条(不可使用PUT请求,因为PUT要规范要保证幂等性)

{
    "_index": "test-index-3",
    "_type": "_doc",
    "_id": "exM0tH0BHlYshFmWH6S7",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 2,
    "_primary_term": 1
}

二、生成自定义id,有两种方式

1.发送【POST】或【PUT】请求:http://127.0.0.1:9200/test-index-3/_doc/1001,参数如下

{
    "title":"test",
    "num":1,
    "date":"20211213"
}

会创建一条自定义id为1001的数据,返回结果如下

{
    "_index": "test-index-3",
    "_type": "_doc",
    "_id": "1001",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 10,
    "_primary_term": 1
}

第二次执行之后为修改操作(全量修改),返回结果如下

{
    "_index": "test-index-3",
    "_type": "_doc",
    "_id": "1001",
    "_version": 2,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 11,
    "_primary_term": 1
}

2.发送【POST】或【PUT】请求:http://127.0.0.1:9200/test-index-3/_create/1002,参数如下

{
    "title":"test",
    "num":1,
    "date":"20211213"
}

会创建一条自定义id为1002的数据,返回结果如下,多次执行会报错,因为只能创建一次

{
    "_index": "test-index-3",
    "_type": "_doc",
    "_id": "1002",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 12,
    "_primary_term": 1
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/663491.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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