Elasticsearch 入门 - Modifying Your Data
index/update/delete 均有大概1秒的缓存时间
Indexing/Replacing Documents
curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
"name": "John Doe"
}
'
replace/reindex:
curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
"name": "Jane Doe"
}
'
Index a new document with and ID of 2:
curl -X PUT "localhost:9200/customer/_doc/2?pretty" -H 'Content-Type: application/json' -d'
{
"name": "Jane Doe"
}
'
Index a document without an explicit ID:
curl -X POST "localhost:9200/customer/_doc?pretty" -H 'Content-Type: application/json' -d'
{
"name": "Jane Doe"
}
'
Updating Documents
Elasticsearch 的更新操作并非原位更新数据,不管什么时候更新操作都是删除旧文档后再索引新的文档。
将ID为1的文档name字段更新为 "Jane Doe":
curl -X POST "localhost:9200/customer/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d'
{
"doc": { "name": "Jane Doe" }
}
'
将ID为1的文档name字段更新为 "Jane Doe"的同时添加一个age字段:
curl -X POST "localhost:9200/customer/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d'
{
"doc": { "name": "Jane Doe", "age": 20 }
}
'
更新操作能够执行简单的脚本,例如使用脚本将age加5:
curl -X POST "localhost:9200/customer/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d'
{
"script" : "ctx._source.age += 5"
}
'
其中 ctx._source
指向当前需要被更新的文档。
Deleting Documents
删除ID为2的文档:
curl -X DELETE "localhost:9200/customer/_doc/2?pretty"