elasticsearch document api——迹忆客-ag捕鱼王app官网
elasticsearch 提供单文档api和多文档api,api调用分别针对单文档和多文档。
index api
当向具有特定映射的相应索引发出请求时,它有助于在索引中添加或更新 json 文档。 例如,以下请求将 json 对象添加到索引 schools 和 school 映射下
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 设置为外部。 这里 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"
}
}
- 该操作是实时的,不受 index 刷新率的影响。
- 我们还可以指定版本,然后 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"
}
}
我们还可以通过将 refresh
参数设置为 true 在执行 get 操作之前刷新分片。
delete 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 一样指定刷新和超时选项。
update api
脚本用于执行此操作,版本控制用于确保在获取和重新索引期间没有发生更新。 例如,我们可以使用脚本更新 school 的费用
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
}