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

索引API

当使用特定映射对相应索引发出请求时,它有助于在索引中添加或更新JSON文档。 例如,以下请求将JSON对象添加到索引为schools和映射为school下。

POST http://localhost:9200/schools/school/4
{
   "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"
}

响应结果如下图所示:
Elasticsearch文档API_Elasticsearch

自动索引创建

当请求将JSON对象添加到特定索引时,如果该索引不存在,那么此API会自动创建该索引以及该特定JSON对象的基础映射。 可以通过将以下参数的值更改为false来禁用此功能,这个值是存在于elasticsearch.yml文件中,打开elasticsearch.yml文件设置如下 。

action.auto_create_index:false   //限制了自动创建索引
index.mapper.dynamic:false          //限制了自动创建基础映射

还可以限制自动创建索引,其中通过更改以下参数的值只允许指定模式的索引名称 (其中+表示允许, - 表示不允许)

action.auto_create_index:+acc*,-bank*

版本控制

Elasticsearch还提供版本控制功能。我们可以使用版本查询参数指定特定文档的版本。 我们举个举例来说明

先构造一条数据出来

PUT /test_index/test_type/1
{
  "test_field": "test test"
}

模拟两个客户端,都获取到了同一条数据

GET test_index/test_type/1

返回结果:

{
   "_index": "test_index",
   "_type": "test_type",
   "_id": "1",
   "_version": 1,   //此时版本号为1
   "found": true,
   "_source": {
      "test_field": "test test"
   }
}

其中一个客户端执行下面的代码,先更新了一下这个数据,带上数据的版本号,确保说,es中的数据的版本号,跟客户端中的数据的版本号是相同的,才能修改。

PUT /test_index/test_type/1?version=1 
{
  "test_field": "test client 1"
}

执行完后,返回结果如下(更新完后,版本号会变成2

{
   "_index": "test_index",
   "_type": "test_type",
   "_id": "1",
   "_version": 2,   //此时版本号已经变成了2
   "result": "updated",
   "_shards": {
      "total": 2,
      "successful": 1,
      "failed": 0
   },
   "created": false
}

另外一个客户端,尝试基于version=1的数据去进行修改,同样带上version版本号,进行乐观锁的并发控制。

PUT /test_index/test_type/1?version=1
{
  "test_field": "test client 2"
}

会发现会返回错误信息:(因为版本号已经变成了2,不是1了,要想修改,就要令version=2才行)

{
  "error": {
    "root_cause": [
      {
        "type": "version_conflict_engine_exception",
        "reason": "[test_type][1]: version conflict, current version [2] is different than the one provided [1]",
        "index_uuid": "ys-Y1QWrRmWTK2JSGuXgww",
        "shard": "3",
        "index": "test_index"
      }
    ],
    "type": "version_conflict_engine_exception",
    "reason": "[test_type][1]: version conflict, current version [2] is different than the one provided [1]",
    "index_uuid": "ys-Y1QWrRmWTK2JSGuXgww",
    "shard": "3",
    "index": "test_index"
  },
  "status": 409
}

版本号可以在外部设置。要启用此功能,我们需要将version_type设置为external。(具体是怎么样的,可以自行百度)
版本控制是一个实时过程,它不受实时搜索操作的影响。

自动生成ID

当在创建索引操作中未指定ID时,Elasticsearch自动为文档生成ID

超时

默认情况下,索引操作将在主分片上最多等待1分钟,超过后就会失败并响应错误。 可以通过将值传递给timeout参数来显式更改这个超时值。

                         //索引名为“tutorials”,映射名为“chapter”
POST http://localhost:9200/tutorials/chapter/2?timeout = 3m
{
   "Text":"This is chapter 2 waiting for primary shard for 3 minutes"
}

获取API

API通过对特定文档执行get请求来帮助提取JSON对象。 例如,

                                      //索引名为schools,映射名为school
GET http://localhost:9200/schools/school/1

响应结果

{
   "_index":"schools", "_type":"school", "_id":"1", "_version":2,
   "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"
   }
}
  • 这个操作是实时的,不受索引刷新率的影响
  • 还可以指定版本version,比如GET http://localhost:9200/schools/school/1?version=3,然后Elasticsearch将仅提取该版本的文档
  • 还可以在请求中指定_all,以便Elasticsearch可以在每种类型中搜索该文档ID,并且它将返回第一个匹配的文档。
  • 还可以从该特定文档的结果中指定所需的字段。比如:GET http://localhost:9200/schools/school/1?fields = name,fees 响应结果为:
……………………..
"fields":{
   "name":["Central School"], "fees":[2200]
}
……………………..

还可以通过在get请求中添加_source字段来获取结果中的源部分。比如:GET http://localhost:9200/schools/school/1/_source 响应结果为:

{
   "name":"Central School", "description":"CBSE Afiliation", "street":"Nagan",
   "city":"paprola", "state":"HP", "zip":"176115", "location":[31.8955385, 76.8380405],
   "fees":2200, "tags":["Senior Secondary", "beatiful campus"], "rating":"3.3"
}

删除API

可以通过向Elasticsearch发送HTTP DELETE请求来删除指定的索引,映射或文档。 例如,

DELETE http://localhost:9200/schools/school/4

响应结果为

{      
   "found":true, "_index":"schools", "_type":"school", "_id":"4", "_version":2,
   "_shards":{"total":2, "successful":1, "failed":0}
}
  • 可以指定文档的版本以删除指定的版本。
  • 可以指定路由参数以删除指定用户的文档,如果文档不属于该特定用户,则操作将失败。
  • 在此操作中,可以像GET API那样指定刷新(refresh)和超时(timeout)选项。

更新API

脚本用于执行此操作,版本控制用于确保在获取和重建索引期间没有发生更新。 例如,使用下面脚本更新学校的费用

                            //URL后面跟了update表示“更新”操作
POST http://localhost:9200/schools_gov/school/1/_update
{
   "script":{
      "inline": "ctx._source.fees+ = inc", "params":{
         "inc": 500
      }
   }
}

响应结果

{
   "_index":"schools_gov", "_type":"school", "_id":"1", "_version":2,
   "_shards":{"total":2, "successful":1, "failed":0}
}

可以通过向更新的文档发送获取请求来检查更新。

GET http://localhost:9200/schools_gov/school/1

多获取API

它具有相同的功能,如GET API,但此get请求可以返回多个文档。使用doc数组来指定需要提取的所有文档的索引,类型和ID。

POST http://localhost:9200/_mget
{
   "docs":[
      {
         "_index": "schools", "_type": "school", "_id": "1"
      },

      {
         "_index":"schools_gev", "_type":"school", "_id": "2"
      }
   ]
}

响应结果

{
   "docs":[
      {
         "_index":"schools", "_type":"school", "_id":"1",
         "_version":1, "found":true, "_source":{
            "name":"Central School", "description":"CBSE Afiliation",
            "street":"Nagan", "city":"paprola", "state":"HP", "zip":"176115",
            "location":[31.8955385,76.8380405], "fees":2000, 
            "tags":["Senior Secondary", "beatiful campus"], "rating":"3.5"
         }
      },

      {
         "_index":"schools_gev", "_type":"school", "_id":"2", "error":{

            "root_cause":[{
               "type":"index_not_found_exception", "reason":"no such index", 
               "index":"schools_gev"
            }],

            "type":"index_not_found_exception", "reason":"no such index", 
            "index":"schools_gev"
         }
      }
   ]
}

批量API

此API用于通过在单个请求中进行多个索引/删除操作来批量上传或删除JSON对象。 需要添加“_bulk”关键字来调用此API。此API的示例已在Elasticsearch填充文章中执行。所有其他功能与GET API相同。

本文转载自:https://www.yiibai.com/elasticsearch/elasticsearch_document_apis.html