数据输入输出
# 文档元数据
# 三个必须:_index、_type、_id
# 以上三元素可以唯一确定一个文档
# _index名必须小写,不能以下划线开头,不能包含逗号
# _type名不能以下划线或句号开头,不能包含逗号,长度限制为256个字符
# _id可以自己指定,也可以由Elasticsearch自动生成
# 索引文档
# PUT /{_index}/{_type}/{_id}
# {
# "field" : "value",
# ...
# }
PUT /website/blog/123
{
"title" : "My first blog entry",
"text" : "Just try this out...",
"date" : "2021-1-11 14:41"
}
# POST /{_index}/{_type}
# {
# "field" : "value",
# ...
# }
POST /website/blog
{
"title" : "My second blog entry",
"text" : "Still try this out...",
"date" : "2021-1-11 14:48"
}
# 取回一个文档
# GET /{_index}/{_type}/{_id}
GET /website/blog/123
# 取回文档的一部分
GET /website/blog/123?_source=title,text
# 只返回_source
GET /website/blog/123/_source?_source
GET /website/blog/123/_source?_source=title,text,date
# 检查文档是否存在
HEAD /website/blog/123
# 更新整个文档
# 会覆盖更新
# 实际内部执行过程
# 不存在文档,直接创建
# 存在文档,删除原文档,创建新文档
PUT /website/blog/123
{
"title" : "My first blog entry",
"text" : "I'm starting to get the hang of this...",
"date" : "2021-1-11 14:41"
}
# 只创建,不删除
# 方式1:PUT /website/blog/123/_create
# 方式2:PUT /website/blog/123?op_type=create
PUT /website/blog/123/_create
{
"title" : "My first blog entry",
"text" : "I'm starting to get the hang of this...",
"date" : "2021-1-11 14:41"
}
# 删除文档
DELETE /website/blog/123
# 版本控制,乐观并发控制
# 提交修改时,先进行版本比对
# 如果和取文档时的版本不匹配,则更新失败
# 所有更新删除的API,都支持version参数
PUT /website/blog/1/_create
{
"title" : "My first blog entry",
"text" : "Just trying this out..."
}
PUT /website/blog/1?version=1
{
"title" : "My first blog entry",
"text" : "Starting to get the hang of this"
}
# 版本控制,指定版本号
# 当指定的版本号不大于当前版本号时,更新失败
# PUT /{_index}/{_type}/{_id}?version={external_version}&version_type=external
PUT /website/blog/1?version=11&version_type=external
{
"title" : "My first extenal blog entry",
"text" : "Starting to get the hang of this"
}
# 文档部分更新
POST /website/blog/1/_update
{
"doc": {
"tags" : ["testing"],
"views" : 0
}
}
# 使用脚本更新部分文档
POST /website/blog/1/_update
{
"script": "ctx._source.views+=1"
}
POST /website/blog/1/_update
{
"script": {
"params": {
"new_tag" : "util"
},
"source": "ctx._source.tags.add(params.new_tag)"
}
}
POST /website/blog/1/_update
{
"script": {
"params": {
"count" : 1
},
"source" : "ctx.op=ctx._source.views == params.count ? 'delete' : 'none'"
}
}
# 如果索引不存在,可以通过upsert创建索引,并指定views的值为1
# 如果索引存在,并且views存在,则views+1
# 如果索引存在,并且views不存在,则报错
POST /website/blog/1/_update
{
"script": "ctx._source.views+=1",
"upsert": {
"views" : 1
}
}
# 更新失败后重试的次数,默认为0
# 如果版本更新之前文档已经被其他进程修改了
# 如果先后顺序不重要的话,只需要设置重新更新
POST /website/blog/1/_update?retry_on_conflict=5
{
"script": "ctx._source.views+=1",
"upsert": {
"views" : 1
}
}
# 取回多个文档
GET /_mget
{
"docs" : [
{
"_index" : "website",
"_type" : "blog",
"_id" : 1
},
{
"_index" : "website",
"_type" : "blog",
"_id" : 2
}
]
}
# _index和_type相同
GET /website/blog/_mget
{
"docs" : [
{
"_id" : 1
},
{
"_id" : 2
},
{
"_index":"megacorp",
"_type":"employee",
"_id":1
}
]
}
GET /website/blog/_mget
{
"ids" : [1,2,3]
}
# 代价较小的批量操作
# 其中一个执行失败,不影响其他语句
POST /_bulk
{"delete":{"_index":"website","_type":"blog","_id":123}}
{"create":{"_index":"website","_type":"blog","_id":123}}
{"title":"My first blog post"}
{"index":{"_index":"website","_type":"blog"}}
{"title":"My second blog post"}
{"update":{"_index":"website","_type":"blog","_id":123,"_retry_on_conflict":3}}
{"doc":{"title":"My update blog post"}}
# _index和_type相同
# 如果其中_index和_type不同,可单独指定
POST /website/blog/_bulk
{"delete":{"_id":123}}
{"create":{"_id":123}}
{"title":"my first blog post"}
{"index":{"_type":"log"}}
{"title":"override default type"}