索引操作
//创建一个索引,pretty表示打印添加状态
curl -X PUT "localhost:9200/customer?pretty"
//获取索引列表
curl -X GET "localhost:9200/_cat/indices?v"
//向索引中添加一个文档,指定ID为1,如果索引不存在es会自动创建
curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
"name": "John Doe"
}'
//向索引中添加一个文档,没有指定ID,es将自动创建
curl -X POST "localhost:9200/customer/_doc?pretty" -H 'Content-Type: application/json' -d'
{
"name": "Jane Doe"
}'
//检索编入索引的文档
curl -X GET "localhost:9200/customer/_doc/1?pretty"
//删除索引
curl -X DELETE "localhost:9200/customer?pretty"
更新文档
//更新ID为1的文档
curl -X POST "localhost:9200/customer/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d'
{
"doc": { "name": "Jane Doe" }
}'
//更新ID为1的文档,并且增加属性
curl -X POST "localhost:9200/customer/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d'
{
"doc": { "name": "Jane Doe", "age": 20 }
}'
//使用脚本更新文档,ctx._source代表当前要更新的文档(此处表示年龄在原来的基础上再加5)
curl -X POST "localhost:9200/customer/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d'
{
"script" : "ctx._source.age += 5"
}'
删除文档
//删除文档指定ID为2
curl -X DELETE "localhost:9200/customer/_doc/2?pretty"
批量处理:
//批量插入两条数据,ID分别为3和4
curl -X POST "localhost:9200/customer/_doc/_bulk?pretty" -H 'Content-Type: application/json' -d'
{"index":{"_id":"3"}}
{"name": "John Doe" }
{"index":{"_id":"4"}}
{"name": "Jane Doe" }
'
//此示例更新第一个文档(ID为1),然后在一个批量操作中删除第二个文档(ID为2):
curl -X POST "localhost:9200/customer/_doc/_bulk?pretty" -H 'Content-Type: application/json' -d'
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}
'