新增文档
语法格式:
PUT /Index/Type/id
{
"json数据"
}
Elasticsearch 会自动建立 Index 和 Type,不需要提前创建,而且 Elasticsearch 默认会对 Document 的每个 field 都建立倒排索引,让其可以被搜索。
PUT /store/product/1
{
"name": "gaolujie yagao",
"desc": "gaoxiao meibai",
"price": 30,
"producer": "gaolujie producer",
"tags": [
"meibai",
"fangzhu"
]
}
PUT /store/product/2
{
"name": "jiajieshi yagao",
"desc": "youxiao fangzhu",
"price": 25,
"producer": "jiajieshi producer",
"tags": [
"fangzhu"
]
}
PUT /store/product/3
{
"name": "zhonghua yagao",
"desc": "caoben zhiwu",
"price": 40,
"producer": "zhonghua producer",
"tags": [
"qingxin"
]
}
新增返回结果:
{
"_index": "store",
"_type": "product",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
查询文档
语法格式:
GET /Index/Type/id
执行 GET /store/product/1
,返回结果:
{
"_index": "store",
"_type": "product",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"name": "gaolujie yagao",
"desc": "gaoxiao meibai",
"price": 30,
"producer": "gaolujie producer",
"tags": [
"meibai",
"fangzhu"
]
}
}
修改文档
替换文档
PUT /store/product/1
{
"name": "jiaqiangban gaolujie yagao",
"desc": "gaoxiao meibai",
"price": 30,
"producer": "gaolujie producer",
"tags": [
"meibai",
"fangzhu"
]
}
返回结果:
{
"_index": "store",
"_type": "product",
"_id": "1",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": false
}
查询:GET /store/product/1
,返回结果:
{
"_index": "store",
"_type": "product",
"_id": "1",
"_version": 2,
"found": true,
"_source": {
"name": "jiaqiangban gaolujie yagao",
"desc": "gaoxiao meibai",
"price": 30,
"producer": "gaolujie producer",
"tags": [
"meibai",
"fangzhu"
]
}
}
替换方式有一个不好,即必须带上所有的 field,才能去进行信息的修改。
更新文档(更新 != 替换)
POST /store/product/1/_update
{
"doc": {
"name": "jiaqiangban2 gaolujie yagao"
}
}
返回结果:
{
"_index": "store",
"_type": "product",
"_id": "1",
"_version": 3,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
查询:GET /store/product/1
,返回结果:
{
"_index": "store",
"_type": "product",
"_id": "1",
"_version": 3,
"found": true,
"_source": {
"name": "jiaqiangban2 gaolujie yagao",
"desc": "gaoxiao meibai",
"price": 30,
"producer": "gaolujie producer",
"tags": [
"meibai",
"fangzhu"
]
}
}
删除文档
语法格式:
DELETE /Index/Type/id
执行 DELETE /store/product/1
,返回结果:
{
"found": true,
"_index": "store",
"_type": "product",
"_id": "1",
"_version": 4,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}