文档操作
创建文档
发POST请求
http://127.0.0.1:9200/demo/_doc
请求数据:
{
"username":"小白白",
"age":22,
"address":"中国上海"
}
响应:
{
"_index": "demo",
"_type": "_doc",
"_id": "wwKD_nsBW1VSDLfjekOE",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
由于没有指定数据唯一性标识(ID),默认情况下,ES服务器会随机生成一个。
自定义唯一性标识,需要在创建时指定
http://127.0.0.1:9200/demo/_doc/1
http://127.0.0.1:9200/demo/_create/2
响应:
{
"_index": "demo",
"_type": "_doc",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1
}
注意:增加数据,明确数据主键,可以使用POST与PUT请求
查看文档
发GET请求:http://127.0.0.1:9200/demo/_doc/1
响应:
{
"_index": "demo",
"_type": "_doc",
"_id": "1",
"_version": 1,
"_seq_no": 1,
"_primary_term": 1,
"found": true,
"_source": {
"username": "小白2",
"age": 22,
"address": "中国上海2"
}
}
修改文档
发POST请求 :
http://127.0.0.1:9200/demo/_doc/1
请求参数:
{
"username":"修改文档",
"age":22,
"address":"中国上海2"
}
响应:
{
"_index": "demo",
"_type": "_doc",
"_id": "1",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 1
}
修改字段
修改数据时只修改数据的局部信息
发POST请求 :
http://127.0.0.1:9200/demo/_update/1
请求数据:
{
"doc": {
"username":"update field"
}
}
响应:
{
"_index": "demo",
"_type": "_doc",
"_id": "1",
"_version": 3,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 3,
"_primary_term": 2
}
删除文档
删除文档不会立即从磁盘上移除,只是被标记成已删除(逻辑删除)
根据文档的唯一性标识进行删除
根据条件对多条数据进行删除
发DELETE请求 :
http://127.0.0.1:9200/demo/_doc/4
响应:
{
"_index": "demo",
"_type": "_doc",
"_id": "4",
"_version": 2,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 6,
"_primary_term": 2
}
发送POST请求:
http://127.0.0.1:9200/demo/_delete_by_query
请求数据:
{
"query": {
"match": {
"username": "update field"
}
}
}
响应:
{
"took": 81,
"timed_out": false,
"total": 1,
"deleted": 1,
"batches": 1,
"version_conflicts": 0,
"noops": 0,
"retries": {
"bulk": 0,
"search": 0
},
"throttled_millis": 0,
"requests_per_second": -1,
"throttled_until_millis": 0,
"failures": []
}
查询所有
发送Get请求:
http://127.0.0.1:9200/demo/_search
响应:
{
"took": 632,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "demo",
"_type": "_doc",
"_id": "wwKD_nsBW1VSDLfjekOE",
"_score": 1,
"_source": {
"username": "小白白",
"age": 22,
"address": "中国上海"
}
},
{
"_index": "demo",
"_type": "_doc",
"_id": "1",
"_score": 1,
"_source": {
"username": "update field",
"age": 22,
"address": "中国上海2"
}
}
]
}
}
本文是原创文章,采用CC BY-NC-SA 4.0协议,完整转载请注明来自耕田日记
评论