1.创建一个名为member
的索引数据
PUT member
{
"mappings": {
"properties": {
"id": {
"type": "keyword"
},
"name": {
"type": "keyword"
},
"age": {
"type": "byte"
}
}
}
}
创建成功
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "member"
}
2.基本的CURD
- T1 如果文档存在则是修改,则会删除再创建新的文档
PUT member/_doc/1
{
"id":1,
"age":1,
"name":"this is first."
}
{
"_index" : "member",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
如果再次对 id = 1
这条数据进行以下操作,可以看到原来的数据字段age 和 id
不在了。
PUT member/_doc/1
{
"name":"this is update2"
}
返回结果:
{
"_index" : "member",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 8,
"_primary_term" : 1
}
查询id=1
的数据
{
"_index" : "member",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"_seq_no" : 7,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "this is update2"
}
}
- T2 创建
指定ID
的文档
POST member/_create/2
{
"doc": {
"id": 2,
"name": "name2",
"age": 11
}
}
注:重复插入数据会报错.抛出一个 409 的错误
{
"error" : {
"root_cause" : [
{
"type" : "version_conflict_engine_exception",
"reason" : "[2]: version conflict, document already exists (current version [1])",
"index_uuid" : "tWrrj4lCQ_mT5k7vCRcyvQ",
"shard" : "0",
"index" : "member"
}
],
"type" : "version_conflict_engine_exception",
"reason" : "[2]: version conflict, document already exists (current version [1])",
"index_uuid" : "tWrrj4lCQ_mT5k7vCRcyvQ",
"shard" : "0",
"index" : "member"
},
"status" : 409
}
- T3 创建一个
自动生成ID
的文档
POST member/_doc
{
"doc": {
"id": 10,
"name": "rand-name",
"age": 20
}
}
- T4 获取指定ID的文档数据
GET member/_doc/2
{
"_index" : "member",
"_type" : "_doc",
"_id" : "2",
"_version" : 1,
"_seq_no" : 10,
"_primary_term" : 1,
"found" : true,
"_source" : {
"doc" : {
"id" : 2,
"name" : "name2",
"age" : 11
}
}
}
- T5 修改指定ID的文档数据
POST member/_update/3
{
"doc": {
"name": "name3 is update"
}
}
- T6 删除指定ID的文档数据
DELETE member/_doc/2