一、生成随机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 }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)