elasticsearch Curl 基本操作

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/TH_NUM/article/details/89367816

创建文档:
curl -X PUT “localhost:9200/website/blog/2” -H ‘Content-Type: application/json’ -d’
{
“title”: “My first blog entry”,
“text”: “Just trying this out…”,
“date”: “2014/01/01”
}

取回文档:
curl -X GET “localhost:9200/website/blog/123?pretty”
在请求的查询串参数中加上 pretty 参数, 正如前面的例子中看到的,这将会调用 Elasticsearch 的 pretty-print 功能,该功能 使得 JSON 响应体更加可读。但是, _source字段不能被格式化打印出来。相反,我们得到的 _source 字段中的 JSON 串,刚好是和我们传给它的一样。

返回文档的一部分:
curl -X GET “localhost:9200/website/blog/123?_source=title,text”

检查文档是不是存在:
curl -i -XHEAD http://localhost:9200/website/blog/123

删除文档:
DELETE /website/blog/123

更新文档
curl -X POST “localhost:9200/website/blog/123/_update” -H ‘Content-Type: application/json’ -d’
{
“doc” : {
“tags” : [ “testing” ],
“views”: 0
}
}

使用脚本更新文档
curl -X POST “localhost:9200/website/blog/123/_update” -H ‘Content-Type: application/json’ -d’
{
“script” : “ctx._source.views+=1”
}

更新文档的字段可能不存在,使用upsert 关键字,并且具有初始化的值
curl -X POST “localhost:9200/website/blog/123/_update” -H ‘Content-Type: application/json’ -d’
{
“script” : “ctx._source.views+=1”,
“upsert”: {
“views”: 1
}
}

更新和冲突,使retry_on_conflict 多次尝试更新
curl -X POST “localhost:9200/website/blog/123/_update?retry_on_conflict=5” -H ‘Content-Type: application/json’ -d’
{
“script” : “ctx._source.views+=1”,
“upsert”: {
“views”: 0
}
}

取回多个文档
curl -X GET “localhost:9200/_mget” -H ‘Content-Type: application/json’ -d’
{
“docs” : [
{
“_index” : “website”,
“_type” : “blog”,
“_id” : 2
},
{
“_index” : “website”,
“_type” : “blog”,
“_id” : 1,
“_source”: “views”
}
]
}

代价较小的批量操作
curl -X POST “localhost:9200/_bulk” -H ‘Content-Type: application/json’ -d’
{ “delete”: { “_index”: “website”, “_type”: “blog”, “_id”: “1” }}
{ “create”: { “_index”: “website”, “_type”: “blog”, “_id”: “123” }}
{ “title”: “My first blog post” , “blog”: “测试内容,测试内容” }
{ “index”: { “_index”: “website”, “_type”: “blog” }}
{ “title”: “My second blog post” }
{ “update”: { “_index”: “website”, “_type”: “blog”, “_id”: “123”, “_retry_on_conflict” : 3} }
{ “doc” : {“title” : “My updated blog post”} }

猜你喜欢

转载自blog.csdn.net/TH_NUM/article/details/89367816