Elasticsearch提供单文档API和多文档API,其中API调用分别针对单个文档和多个文档。html
当使用特定映射对相应索引起出请求时,它有助于在索引中添加或更新JSON文档。 例如,如下请求将JSON对象添加到索引为schools
和映射为school
下。json
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" }
响应结果以下图所示:api
当请求将JSON对象添加到特定索引时,若是该索引不存在,那么此API会自动建立该索引以及该特定JSON对象的基础映射。 能够经过将如下参数的值更改成false
来禁用此功能,这个值是存在于elasticsearch.yml
文件中,打开elasticsearch.yml
文件设置以下 。数组
action.auto_create_index:false //限制了自动建立索引 index.mapper.dynamic:false //限制了自动建立基础映射
还能够限制自动建立索引,其中经过更改如下参数的值只容许指定模式的索引名称 (其中+
表示容许, -
表示不容许)markdown
action.auto_create_index:+acc*,-bank*
Elasticsearch还提供版本控制功能。咱们可使用版本查询参数指定特定文档的版本。 咱们举个举例来讲明并发
先构造一条数据出来app
PUT /test_index/test_type/1 { "test_field": "test test" }
模拟两个客户端,都获取到了同一条数据yii
GET test_index/test_type/1
返回结果:elasticsearch
{ "_index": "test_index", "_type": "test_type", "_id": "1", "_version": 1, //此时版本号为1 "found": true, "_source": { "test_field": "test test" } }
其中一个客户端执行下面的代码,先更新了一下这个数据,带上数据的版本号,确保说,es中的数据的版本号,跟客户端中的数据的版本号是相同的,才能修改。ide
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
时,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经过对特定文档执行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" } }
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" }
能够经过向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} }
refresh
)和超时(timeout
)选项。脚本用于执行此操做,版本控制用于确保在获取和重建索引期间没有发生更新。 例如,使用下面脚本更新学校的费用
//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
它具备相同的功能,如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用于经过在单个请求中进行多个索引/删除操做来批量上传或删除JSON对象。 须要添加“_bulk
”关键字来调用此API。此API的示例已在Elasticsearch填充文章中执行。全部其余功能与GET API相同。
本文转载自:https://www.yiibai.com/elasticsearch/elasticsearch_document_apis.html