Elasticsearch Document Api

Elasticsearch 提供单文档API和多文档API,API调用分别针对单文档和多文档。


Index Api

当向具有特定映射的相应索引发出请求时,它有助于在索引中添加或更新 JSON 文档。 例如,以下请求将 JSON 对象添加到索引 schoolsschool 映射下

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
}

查看笔记

扫码一下
查看教程更方便