Elasticsearch - 文档 API
Elasticsearch 提供单文档 API 和多文档 API,其中 API 调用分别针对单个文档和多个文档。
索引 API
当使用特定映射向相应索引发出请求时,它有助于在索引中添加或更新 JSON 文档。例如,以下请求将 JSON 对象添加到索引学校和学校映射下 −
PUT schools/_doc/5
{
name":"City School", "description":"ICSE", "street":"West End",
"city":"Meerut",
"state":"UP", "zip":"250002", "location":[28.9926174, 77.692485],
"fees":3500,
"tags":["fully computerized"], "rating":"4.5"
}
运行上述代码,我们得到以下结果 −
{
"_index" : "schools",
"_type" : "_doc",
"_id" : "5",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1
}
自动创建索引
当请求将 JSON 对象添加到特定索引时,如果该索引不存在,则此 API 会自动创建该索引以及该特定 JSON 对象的底层映射。可以通过将以下参数的值更改为 false(存在于 elasticsearch.yml 文件中)来禁用此功能。
action.auto_create_index:false index.mapper.dynamic:false
您还可以通过更改以下参数 − 的值来限制索引的自动创建,其中仅允许具有特定模式的索引名称
action.auto_create_index:+acc*,-bank*
注意 − 此处 + 表示允许,- 表示不允许。
版本控制
Elasticsearch 还提供版本控制功能。我们可以使用版本查询参数来指定特定文档的版本。
PUT schools/_doc/5?version=7&version_type=external
{
"name":"Central School", "description":"CBSE Affiliation", "street":"Nagan",
"city":"paprola", "state":"HP", "zip":"176115", "location":[31.8955385, 76.8380405],
"fees":2200, "tags":["Senior Secondary", "beautiful campus"], "rating":"3.3"
}
运行上述代码,我们得到以下结果 −
{
"_index" : "schools",
"_type" : "_doc",
"_id" : "5",
"_version" : 7,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 3,
"_primary_term" : 1
}
版本控制是一个实时过程,不受实时搜索操作的影响。
有两种最重要的版本控制类型 −
内部版本控制
内部版本控制是默认版本,从 1 开始,每次更新(包括删除)时都会递增。
外部版本控制
当文档的版本存储在外部系统(如第三方版本控制系统)中时,会使用它。要启用此功能,我们需要将 version_type 设置为 external。Elasticsearch 将存储外部系统指定的版本号,并且不会自动递增它们。
操作类型
操作类型用于强制执行创建操作。这有助于避免覆盖现有文档。
PUT chapter/_doc/1?op_type=create
{
"Text":"this is chapter one"
}
运行上述代码,我们得到以下结果 −
{
"_index" : "chapter",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
自动 ID 生成
当索引操作中未指定 ID 时,Elasticsearch 会自动为该文档生成 ID。
POST chapter/_doc/
{
"user" : "tpoint",
"post_date" : "2018-12-25T14:12:12",
"message" : "Elasticsearch Tutorial"
}
运行上述代码,我们得到以下结果 −
{
"_index" : "chapter",
"_type" : "_doc",
"_id" : "PVghWGoB7LiDTeV6LSGu",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}
GET API
API 通过对特定文档执行获取请求来帮助提取 JSON 类型对象。
pre class="prettyprint notranslate" > GET schools/_doc/5
运行上述代码,我们得到以下结果 −
{
"_index" : "schools",
"_type" : "_doc",
"_id" : "5",
"_version" : 7,
"_seq_no" : 3,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "Central School",
"description" : "CBSE Affiliation",
"street" : "Nagan",
"city" : "paprola",
"state" : "HP",
"zip" : "176115",
"location" : [
31.8955385,
76.8380405
],
"fees" : 2200,
"tags" : [
"Senior Secondary",
"beautiful campus"
],
"rating" : "3.3"
}
}
此操作是实时的,不受索引刷新率的影响。
您还可以指定版本,然后 Elasticsearch 将仅获取该版本的文档。
您还可以在请求中指定 _all,以便 Elasticsearch 可以在每种类型中搜索该文档 id,并返回第一个匹配的文档。
您还可以从该特定文档中指定您想要的结果中的字段。
GET schools/_doc/5?_source_includes=name,fees
运行上述代码,我们得到以下结果 −
{
"_index" : "schools",
"_type" : "_doc",
"_id" : "5",
"_version" : 7,
"_seq_no" : 3,
"_primary_term" : 1,
"found" : true,
"_source" : {
"fees" : 2200,
"name" : "Central School"
}
}
您还可以通过在获取请求中添加 _source 部分来获取结果中的源部分。
GET schools/_doc/5?_source
运行上述代码,我们得到以下结果 −
{
"_index" : "schools",
"_type" : "_doc",
"_id" : "5",
"_version" : 7,
"_seq_no" : 3,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "Central School",
"description" : "CBSE Affiliation",
"street" : "Nagan",
"city" : "paprola",
"state" : "HP",
"zip" : "176115",
"location" : [
31.8955385,
76.8380405
],
"fees" : 2200,
"tags" : [
"Senior Secondary",
"beautiful campus"
],
"rating" : "3.3"
}
}
您还可以通过将刷新参数设置为 true 来在执行获取操作之前刷新分片。
删除 API
您可以通过向 Elasticsearch 发送 HTTP DELETE 请求来删除特定索引、映射或文档。
DELETE schools/_doc/4
运行上述代码,我们得到以下结果 −
{
"found":true, "_index":"schools", "_type":"school", "_id":"4", "_version":2,
"_shards":{"total":2, "successful":1, "failed":0}
}
可以指定文档的版本以删除该特定版本。可以指定路由参数以从特定用户中删除文档,如果文档不属于该特定用户,则操作失败。在此操作中,您可以指定与 GET API 相同的刷新和超时选项。
更新 API
脚本用于执行此操作,版本控制用于确保在获取和重新索引期间没有发生任何更新。例如,您可以使用脚本更新学校的费用 −
POST schools/_update/4
{
"script" : {
"source": "ctx._source.name = params.sname",
"lang": "painless",
"params" : {
"sname" : "City Wise School"
}
}
}
运行上述代码,我们得到以下结果 −
{
"_index" : "schools",
"_type" : "_doc",
"_id" : "4",
"_version" : 3,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 4,
"_primary_term" : 2
}
您可以通过向更新的文档发送获取请求来检查更新。

