Elasticsearch提供单文档API和多文档API,其中API调用分别针对单个文档和多个文档.
当向具有特定映射的相应索引发出请求时,它有助于在索引中添加或更新JSON文档.例如,下面的请求会将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" }
{ "_index":"schools", "_type":"school", "_id":"4", "_version":1, "_shards":{"total":2, "successful":1,"failed":0}, "created":true }
当请求添加JSON时对象指向特定索引,如果该索引不存在,则此API会自动创建该索引以及该参数的基础映射特定的JSON对象.可以通过将以下参数的值更改为false来禁用此功能,这些参数存在于elasticsearch.yml文件中.
action.auto_create_index:false index.mapper.dynamic:false
您还可以限制索引的自动创建,其中只允许通过更改具有特定模式的索引名称以下参数的值 :
action.auto_create_index:+acc*,-bank*
(其中+表示允许和 - 表示不允许)
Elasticsearch还提供版本控制工具.我们可以使用版本查询参数来指定特定文档的版本.例如,
POST http://localhost:9200/schools/school/1?version = 1
{ "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":"school", "_id":"1", "_version":2, "_shards":{"total":2, "successful":1,"failed":0}, "created":false }
有两种最重要的版本控制类型;内部版本控制是以1开头的默认版本,随每次更新而增加,包括删除.版本号可以在外部设置.要启用此功能,我们需要将version_type设置为external.
版本控制是一个实时过程,不受实时搜索操作的影响.
操作类型用于强制创建操作,这有助于避免覆盖现有文档.
POST http://localhost:9200/tutorials/chapter/1?op_type = create
{ "Text":"this is chapter one" }
{ "_index":"tutorials", "_type":"chapter", "_id":"1", "_version":1, "_shards":{"total":2, "successful":1, "failed":0}, "created":true }
在索引操作中未指定ID时,Elasticsearch会自动为该文档生成ID .
您可以通过在父URL查询参数中传递父文档的ID来定义任何文档的父级.
POST http://localhost:9200/tutorials/article/1?parent = 1
{ "Text":"This is article 1 of chapter 1" }
注意 : 如果在执行此示例时遇到异常,请通过在索引中添加以下内容来重新创建索引.
{ "mappings": { "chapter": {}, "article": { "_parent": { "type": "chapter" } } } }
默认情况下,索引操作将等待在主要分片上,在失败并响应错误之前最多可用1分钟.通过将值传递给timeout参数,可以显式更改此超时值.
POST http://localhost:9200/tutorials/chapter/2?timeout = 3m
{ "Text":"This is chapter 2 waiting for primary shard for 3 minutes" }
API有助于通过执行特定文档的get请求来提取类型JSON对象.例如,
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" } }
此操作是实时的,不会受到索引刷新率的影响.
您也可以指定版本,然后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], "费":2200年, "标签":["高中","美丽的校园"],"评级":"3.3" }
您还可以刷新通过将set refresh参数设置为true来执行get操作之前的分片.
您可以通过发送删除特定索引,映射或文档对Elasticsearch的HTTP DELETE请求.例如,
DELETE http://localhost:9200/schools/school/4
{ "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" }
可以指定文档版本来删除该特定版本.
可以指定路由参数从特定用户删除文档,如果文档不属于该特定用户,则操作失败.
在此操作中,您可以像GET API一样指定刷新和超时选项.
脚本用于执行此操作,版本控制用于确保在获取和重新索引期间未发生更新.例如,使用脚本 : 更新学校费用;
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} }
注意 : 如果你得到脚本异常,建议在elastcisearch.yml中添加以下几行
script.inline: on script.indexed: on
您可以通过向更新的文档发送获取请求来检查更新.
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相同.