English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Elasticsearch provides single document API and multi-document API, where API calls are made for individual documents and multiple documents respectively.
It helps to add or update JSON documents in an index with a specific mapping when a request is made to the corresponding index with a specific mapping. For example, the following request will add a JSON object to the index "schools" and the corresponding mapping.-
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" }
When running the above code, we get the following results-
{ "_index": "schools", "_type": "_doc", "_id": ""5, "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 2, "_primary_term": 1 }
When the request adds a JSON object to a specific index, if the index does not exist, this API will automatically create the index and the basic mapping of the specific JSON object. This feature can be disabled by changing the value of the following parameters in the elasticsearch.yml file to false.
action.auto_create_index:false index.mapper.dynamic:false
You can also limit the automatic creation of indexes by changing the value of the following parameters to allow only index names with specific patterns.-
action.auto_create_index:+acc*,-bank*
Note:Here + means allowed, while – means not allowed.
Elasticsearch also provides version control tools. We can use version query parameters to specify the version of a specific document.
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":22"00", "tags":["Senior Secondary", "beautiful campus"], "rating":""3.3" }
When running the above code, we get the following results-
{ "_index": "schools", "_type": "_doc", "_id": ""5, "_version": 7, "result": "updated", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 3, "_primary_term": 1 }
Version control is a real-time process and is not affected by real-time search operations.
There are two most important types of version control-
Internal version control is the default version, starting from1Start, and increment with each update (including deletion).
This feature is used when the document version control is stored in an external system (such as a third-party version control system). To enable this feature, we need to set the version_type to external. Here, Elasticsearch will store the version number specified by the external system and will not automatically increment it.
The operation type is used to enforce the creation operation. This helps to avoid overwriting existing documents.
PUT chapter/_doc/1?op_type=create { "Text": "this is chapter one" }
When running the above code, we get the following results-
{ "_index": "chapter", "_type": "_doc", "_id": ""1, "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 0, "_primary_term": 1 }
If an ID is not specified in the index operation, Elasticsearch will automatically generate an ID for the document.
POST chapter/_doc/ { "user": "tpoint", "post_date": ""2018-12-25T14:12:12, "message": "Elasticsearch Tutorial" }
When running the above code, we get the following results-
{ "_index": "chapter", "_type": "_doc", "_id": "PVghWGoB",7LiDTeV6LSGu", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 1, "_primary_term": 1 }
The API helps extract type JSON objects by executing a get request on a specific document.
pre class="prettyprint notranslate" > GET schools/_doc/5
When running the above code, we get the following results-
{ "_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" } }
This operation is real-time and is not affected by the index refresh rate.
You can also specify the version, and then Elasticsearch will only retrieve the version of the document.
You can also specify _all in the request so that Elasticsearch can search the document ID for each type, and it will return the first matching document.
You can also specify the required fields in the results of a specific document.
GET schools/_doc/5?_source_includes=name,fees
When running the above code, we get the following results-
{ "_index": "schools", "_type": "_doc", "_id": ""5, "_version": 7, "_seq_no": 3, "_primary_term": 1, "found": true, "_source": { "fees": "" 2200, "name": "Central School" } }
You can also retrieve the source part of the results by adding the _source part to the get request.
GET schools/_doc/5?_source
When running the above code, we get the following results-
{ "_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" } }
You can also refresh the shard by setting the refresh parameter to true before performing the get operation.
You can delete a specific index, mapping, or document by sending an HTTP DELETE request to Elasticsearch.
DELETE schools/_doc/4
When running the above code, we get the following results-
{ "found": true, "_index": "schools", "_type": "school", "_id": "4, "_version":2, "_shards": {"total":2, "successful":1, "failed": 0} }
You can specify the document version to delete the specific version. You can specify route parameters to delete the document from a specific user, and if the document does not belong to the specific user, the operation will fail. In this operation, you can specify refresh and timeout options like GET API.
The script is used to perform this operation, version control is used to ensure that no updates have occurred during the retrieval and re-indexing process. For example, you can use the script to update tuition fees-
POST schools/_update/4 { "script": { "source": "ctx._source.name = params.sname", "lang": "painless", "params": { "sname": "City Wise School" } } }
When running the above code, we get the following results-
{ "_index": "schools", "_type": "_doc", "_id": ""4, "_version": 3, "result": "updated", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 4, "_primary_term": 2 }
You can check for updates by sending a GET request to the updated document.