(1)新增文档,建立索引
格式:
PUT /index/type/id
{
//json数据
}
示例:
PUT /ecommerce/product/1
{
"name":" 高露洁牙膏",
"price":5,
"tag":["美白","健康"]
}
响应如下:
{
"_index": "ecommerce",
"_type": "product",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
(2)查询文档
格式:
GET /index/type/id
示例,查询刚索引的文档:
GET ecommerce/product/1
响应如下:
{
"_index": "ecommerce",
"_type": "product",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"name": " 高露洁牙膏",
"price": 5,
"tag": [
"美白",
"健康"
]
}
}
(3)修改文档,PUT替换文档
格式(与新增一致):
PUT /index/type/id
{
//json数据
}
示例:
PUT ecommerce/product/1
{
"name":" sheng'ji'ban高露洁牙膏",
"price":5,
"tag":["美白","健康"]
}
响应如下:
{
"_index": "ecommerce",
"_type": "product",
"_id": "1",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": false
}
现在去查看修改后的文档:
GET /ecommerce/product/1
{
"_index": "ecommerce",
"_type": "product",
"_id": "1",
"_version": 2,
"found": true,
"_source": {
"name": " sheng'ji'ban高露洁牙膏",
"price": 5,
"tag": [
"美白",
"健康"
]
}
}
PUT替换方式有一个不好,就是必须带上所有的field,才能去进行信息的修改;如果缺少字段field,在修改后文档中也会缺少字段field。
示例:
PUT ecommerce/product/1
{
"name":"高露洁牙膏",
"price":5
}
响应如下:
{
"_index": "ecommerce",
"_type": "product",
"_id": "1",
"_version": 3,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": false
}
查看修改后的文档:
GET /ecommerce/product/1
发现缺少了tag字段:
{
"_index": "ecommerce",
"_type": "product",
"_id": "1",
"_version": 3,
"found": true,
"_source": {
"name": "高露洁牙膏",
"price": 5
}
}
(4)修改文档,POST 修改
格式:
POST /index/type/id/_update
{
"doc":{
//json字段
}
}
示例:
修改name字段,并新增newTag字段
POST ecommerce/product/1/_update
{
"doc":{
"name":"jia'qiang'ban加强版gao'lu'jie高露洁 ",
"newTag":"this is a new field!"
}
}
响应如下:
{
"_index": "ecommerce",
"_type": "product",
"_id": "1",
"_version": 5,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
查看修改后的文档:
GET /ecommerce/product/1
可以看到name字段已被修改,同时新增了newTag字段:
{
"_index": "ecommerce",
"_type": "product",
"_id": "1",
"_version": 5,
"found": true,
"_source": {
"name": "jia'qiang'ban加强版gao'lu'jie高露洁 ",
"price": 5,
"newTag": "this is a new field!"
}
}
(5)删除文档
格式:
DELETE /index/type/id
示例:
DELETE ecommerce/product/1
响应如下:
{
"found": true,
"_index": "ecommerce",
"_type": "product",
"_id": "1",
"_version": 6,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
再去查看文档,发现已被删除:
GET /ecommerce/product/1
{
"_index": "ecommerce",
"_type": "product",
"_id": "1",
"found": false
}