ElasticSearch 文档API
-
文档API
Elasticsearch提供了单文档API和多文档API,其中API调用分别针对单个文档和多个文档。 -
索引API
当对具有指定映射的相应索引进行请求时,它有助于在索引中添加或更新JSON文档。例如,以下请求会将JSON对象添加到索引school和学校映射下-PUT school/_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": "school", "_type": "_doc", "_id": "5", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 2, "_primary_term": 7 }
-
自动索引创建
当请求将JSON对象添加到指定索引时,如果该索引不存在,则此API会自动创建该索引以及该指定JSON对象的基础映射。可以通过将elasticsearch.yml文件中存在的以下参数的值更改为false来禁用此功能。action.auto_create_index:false index.mapper.dynamic:false
您还可以限制索引的自动创建,通过更改以下参数的值,仅允许使用具有指定模式的索引名称-action.auto_create_index:+acc*,-bank*
注意-这里的+表示允许,而–表示不允许。
-
版本控制
Elasticsearch还提供了版本控制工具。我们可以使用版本查询参数来指定指定文档的版本。PUT school/_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": "school", "_type": "_doc", "_id": "5", "_version": 7, "result": "updated", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 3, "_primary_term": 7 }
版本控制是一个实时过程,不受实时搜索操作的影响。有两种最重要的版本控制类型-内部(Internal)版本控制内部版本控制是默认版本,从1开始,并随着每次更新(包括删除)而递增。外部(External)版本控制当文档的版本控制存储在外部系统(例如第三方版本控制系统)中时,将使用此功能。要启用此功能,我们需要将version_type设置为external。在这里,Elasticsearch将存储外部系统指定的版本号,并且不会自动对其进行递增。 -
操作类型
操作类型用于强制执行创建(crate)操作。这有助于避免覆盖现有文档。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对象。GET school/_doc/5
运行上面的代码,我们得到以下结果-{ "_index" : "school", "_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 school/_doc/5?_source_includes=name,fees
运行上面的代码,我们得到以下结果-{ "_index" : "school", "_type" : "_doc", "_id" : "5", "_version" : 7, "_seq_no" : 3, "_primary_term" : 1, "found" : true, "_source" : { "fees" : 2200, "name" : "Central School" } }
您还可以通过在get请求中添加_source部分来获取结果中的源部分。GET school/_doc/5?_source
运行上面的代码,我们得到以下结果-{ "_index" : "school", "_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 school/_doc/4
运行上面的代码,我们得到以下结果-{ "found":true, "_index":"school", "_type":"school", "_id":"4", "_version":2, "_shards":{"total":2, "successful":1, "failed":0} }
可以指定文档的版本以删除该指定版本。可以指定路由参数以从指定用户删除文档,并且如果文档不属于该指定用户,则操作将失败。在此操作中,您可以指定刷新和超时选项,例如GET API。 -
Update API
脚本用于执行此操作,而版本控制则用于确保在获取和重新编制索引期间未发生任何更新。例如,您可以使用script更新-POST school/_update/4 { "script" : { "source": "ctx._source.name = params.sname", "lang": "painless", "params" : { "sname" : "City Wise School" } } }
运行上面的代码,我们得到以下结果-{ "_index" : "school", "_type" : "_doc", "_id" : "4", "_version" : 3, "result" : "updated", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 4, "_primary_term" : 2 }
您可以通过向更新的文档发送Get请求来检查更新。