- 创建索引时设置主分片和副本分片
PUT /blogs
request:
{
"settings" : {
"number_of_shards" : 3,
"number_of_replicas" : 1
}
}
- 对运行中的集群调整副本分片数量
PUT /blogs/_settings
request:
{
"number_of_replicas" : 2
}
- 查询文档中指定字段(title,text)
GET /website/blog/123?_source=title,text
response:
{
"_index" : "website",
"_type" : "blog",
"_id" : "123",
"_version" : 1,
"found" : true,
"_source" : {
"title": "My first blog entry" ,
"text": "Just trying this out..."
}
}
- 只获取_source字段
GET /website/blog/123/_source
response:
{
"title": "My first blog entry",
"text": "Just trying this out...",
"date": "2014/01/01"
}
- 创建文档
PUT /website/blog/1/_create
{
"title": "My first blog entry",
"text": "Just trying this out..."
}
- 创建文档是指定版本号
PUT /website/blog/2?version=10&version_type=external
{
"title": "My first external blog entry",
"text": "This is a piece of cake..."
}
- 向文档追加部分内容
POST /website/blog/1/_update
{
"doc" : {
"tags" : [ "testing" ],
"views": 0
}
}
// 请求成功后查看
GET /website/blog/1
{
"_index": "website",
"_type": "blog",
"_id": "1",
"_version": 3,
"found": true,
"_source": {
"title": "My first blog entry",
"text": "Starting to get the hang of this...",
"tags": [ "testing" ],
"views": 0
}
}
- 使用脚本部分更新文档。
脚本可以在 update API中用来改变 _source 的字段内容, 它在更新脚本中称为 ctx._source 。 例如,我们可以使用脚本来增加博客文章中 views 的数量:
POST /website/blog/1/_update
{
"script" : "ctx._source.views+=1"
}
// 或者下面这种方式
POST /website/blog/1/_update
{
"script" : "ctx._source.tags+=new_tag",
"params" : {
"new_tag" : "search"
}
}
- 通过脚本判断是否执行删除操作
POST /website/blog/1/_update
{
"script" : "ctx.op = ctx._source.views == count ? 'delete' : 'none'",
"params" : {
"count": 1
}
}
- 更新文档可能尚不存在,可通过
upsert
参数创建
POST /website/pageviews/1/_update
{
"script" : "ctx._source.views+=1",
"upsert": {
"views": 1
}
}
- 更新的时候存在发生冲突的情况。
当发生冲突后我们可以通过retry_on_conflict
设置尝试再次更新,以及设置尝试的次数,一下设置retry_on_conflict=5的意思是更新失败后应该重试5次。
POST /website/pageviews/1/_update?retry_on_conflict=5
{
"script" : "ctx._source.views+=1",
"upsert": {
"views": 0
}
}
- 从相同的type中取回多个文档
_mget
,类似sql的in查询
GET /website/blog/_mget
{
"ids" : [ "2", "1" ]
}
// 相应数据的顺序和请求顺序相同
- 从不同的type中取回多个文档
GET /website/blog/_mget
{
"docs" : [
{ "_id" : 2 },
{ "_type" : "pageviews", "_id" : 1 }
]
}
// 相应数据的顺序和请求顺序相同