Elasticsearch - Document APIs - elasticsearch - elasticsearch tutorial - elastic search
What is Elasticsearch Document API
- Elasticsearch provides single document APIs and multi-document APIs, where the API call is targeting single document and multiple documents respectively.

learn elasticsearch tutorials - documents api Example
elasticsearch - elasticsearch tutorial - elastic search - elasticsearch sort - elasticsearch list indexes - elasticsearch node
Single document APIs
- Index API
- Get API
- Delete API
- Update API
Multi-document APIs
- Multi Get API
- Bulk API
elasticsearch - elasticsearch tutorial - elastic search - elasticsearch sort - elasticsearch list indexes - elasticsearch node
Index API
- It helps to add or updates the JSON document in an index when a request is made to that respective index with specific mapping.
- For example, the below request will add the JSON object to index schools and under school mapping.
POST http://localhost:9200/schools/school/4
Request Body
{
"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"
}
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - elasticsearch - elasticsearch tutorial - elastic - elastic search - elasticsearch docker team
Response
{
"_index":"schools", "_type":"school", "_id":"4", "_version":1,
"_shards":{"total":2, "successful":1,"failed":0}, "created":true
}
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - elasticsearch - elasticsearch tutorial - elastic - elastic search - elasticsearch docker team
Automatic Index Creation
- When a request is made to add JSON object to a particular index and if that index does not exist then this API automatically creates that index and also the underlying mapping for that particular JSON object.
- This functionality can be disabled by changing the values of following parameters to false, which are present in elasticsearch.yml file.
action.auto_create_index:false
index.mapper.dynamic:false
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - elasticsearch - elasticsearch tutorial - elastic - elastic search - elasticsearch docker team
- You can also restrict the auto creation of index, where only index name with specific patterns are allowed by changing the value of the following parameter −
action.auto_create_index:+acc*,-bank*
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - elasticsearch - elasticsearch tutorial - elastic - elastic search - elasticsearch docker team
- (where + indicates allowed and - indicates not allowed)
elasticsearch - elasticsearch tutorial - elastic search - elasticsearch sort - elasticsearch list indexes - elasticsearch node
Versioning
- Elasticsearch also provides version control facility. We can use a version query parameter to specify the version of a particular document.
For example:
POST http://localhost:9200/schools/school/1?version = 1
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - elasticsearch - elasticsearch tutorial - elastic - elastic search - elasticsearch docker team
Request Body
{
"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"
}
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - elasticsearch - elasticsearch tutorial - elastic - elastic search - elasticsearch docker team
elasticsearch - elasticsearch tutorial - elastic search - elasticsearch sort - elasticsearch list indexes - elasticsearch node
Response
{
"_index":"schools", "_type":"school", "_id":"1", "_version":2,
"_shards":{"total":2, "successful":1,"failed":0}, "created":false
}
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - elasticsearch - elasticsearch tutorial - elastic - elastic search - elasticsearch docker team
- There are two most important types of versioning; internal versioning is the default version that starts with 1 and increments with each update, deletes included.
- The version number can be set externally.
- To enable this functionality, we need to set version type to external.
- Versioning is a real-time process and it is not affected by the real time search operations.
Operation Type
- The operation type is used to force a create operation, this helps to avoid the overwriting of existing document.
POST http://localhost:9200/tutorials/chapter/1?op_type = create
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - elasticsearch - elasticsearch tutorial - elastic - elastic search - elasticsearch docker team
Request Body
{
"Text":"this is chapter one"
}
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - elasticsearch - elasticsearch tutorial - elastic - elastic search - elasticsearch docker team
Response
{
"_index":"tutorials", "_type":"chapter", "_id":"1", "_version":1,
"_shards":{"total":2, "successful":1, "failed":0}, "created":true
}
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - elasticsearch - elasticsearch tutorial - elastic - elastic search - elasticsearch docker team
elasticsearch - elasticsearch tutorial - elastic search - elasticsearch sort - elasticsearch list indexes - elasticsearch node
Automatic ID generation
- When ID is not specified in index operation, then Elasticsearch automatically generates id for that document.
Parents and Children
- You can define the parent of any document by passing the id of parent document in parent URL query parameter.
POST http://localhost:9200/tutorials/article/1?parent = 1
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - elasticsearch - elasticsearch tutorial - elastic - elastic search - elasticsearch docker team
Request Body
{
"Text":"This is article 1 of chapter 1"
}
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - elasticsearch - elasticsearch tutorial - elastic - elastic search - elasticsearch docker team
- Note − If you get exception while executing this example, please recreate the index by adding the following in the index.
{
"mappings": {
"chapter": {},
"article": {
"_parent": {
"type": "chapter"
}
}
}
}
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - elasticsearch - elasticsearch tutorial - elastic - elastic search - elasticsearch docker team
Timeout
POST http://localhost:9200/tutorials/chapter/2?timeout = 3m
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - elasticsearch - elasticsearch tutorial - elastic - elastic search - elasticsearch docker team
Request Body
{
"Text":"This is chapter 2 waiting for primary shard for 3 minutes"
}
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - elasticsearch - elasticsearch tutorial - elastic - elastic search - elasticsearch docker team
Get API
- API helps to extract type JSON object by performing a get request for a particular document.
For example:
GET http://localhost:9200/schools/school/1
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - elasticsearch - elasticsearch tutorial - elastic - elastic search - elasticsearch docker team
Response
{
"_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"
}
}
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - elasticsearch - elasticsearch tutorial - elastic - elastic search - elasticsearch docker team
- This operation is real time and does not get affected by the refresh rate of Index.
- You can also specify the version, then Elasticsearch will fetch that version of document only.
- You can also specify the _all in the request, so that the Elasticsearch can search for that document id in every type and it will return the first matched document.
- You can also specify the fields you want in your result from that particular document.
GET http://localhost:9200/schools/school/1?fields = name,fees
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - elasticsearch - elasticsearch tutorial - elastic - elastic search - elasticsearch docker team
Response
"fields":{
"name":["Central School"], "fees":[2200]
}
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - elasticsearch - elasticsearch tutorial - elastic - elastic search - elasticsearch docker team
- You can also fetch the source part in your result by just adding _source part in your get request.
GET http://localhost:9200/schools/school/1/_source
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - elasticsearch - elasticsearch tutorial - elastic - elastic search - elasticsearch docker team
Response
{
"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"
}
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - elasticsearch - elasticsearch tutorial - elastic - elastic search - elasticsearch docker team
- You can also refresh the shard before doing get operation by set refresh parameter to true.
Delete API
- You can delete a particular index, mapping or a document by sending a HTTP DELETE request to Elasticsearch. For example,
DELETE http://localhost:9200/schools/school/4
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - elasticsearch - elasticsearch tutorial - elastic - elastic search - elasticsearch docker team
Response
{
"found":true, "_index":"schools", "_type":"school", "_id":"4", "_version":2,
"_shards":{"total":2, "successful":1, "failed":0}
}
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - elasticsearch - elasticsearch tutorial - elastic - elastic search - elasticsearch docker team
- Version of the document can be specified to delete that particular version.
- Routing parameter can be specified to delete the document from a particular user and the operation fails if the document does not belong to that particular user.
- In this operation, you can specify refresh and timeout option same like GET API.
elasticsearch - elasticsearch tutorial - elastic search - elasticsearch sort - elasticsearch list indexes - elasticsearch node
Update API
- Script is used for performing this operation and versioning is used to make sure that no updates have happened during the get and re-index. For example, update the fees of school using script −
Request Body
{
"script":{
"inline": "ctx._source.fees+ = inc", "params":{
"inc": 500
}
}
}
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - elasticsearch - elasticsearch tutorial - elastic - elastic search - elasticsearch docker team
Response
{
"_index":"schools_gov", "_type":"school", "_id":"1", "_version":2,
"_shards":{"total":2, "successful":1, "failed":0}
}
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - elasticsearch - elasticsearch tutorial - elastic - elastic search - elasticsearch docker team
- Note − If you get script exception, it is recommended to add the following lines in elastcisearch.yml
script.inline: on
script.indexed: on
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - elasticsearch - elasticsearch tutorial - elastic - elastic search - elasticsearch docker team
- You can check the update by sending get request to the updated document.
GET http://localhost:9200/schools_gov/school/1
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - elasticsearch - elasticsearch tutorial - elastic - elastic search - elasticsearch docker team
Multi Get API
- It possesses same functionality like GET API, but this get request can return more than one document.
- We use a doc array to specify the index, type and id of all the documents that need to be extracted.
POST http://localhost:9200/_mget
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - elasticsearch - elasticsearch tutorial - elastic - elastic search - elasticsearch docker team
Request Body
{
"docs":[
{
"_index": "schools", "_type": "school", "_id": "1"
},
{
"_index":"schools_gev", "_type":"school", "_id": "2"
}
]
}
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - elasticsearch - elasticsearch tutorial - elastic - elastic search - elasticsearch docker team
Response
{
"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"
}
}
]
}
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - elasticsearch - elasticsearch tutorial - elastic - elastic search - elasticsearch docker team
Bulk API
- This API is used to upload or delete the JSON objects in bulk by making multiple index/delete operations in a single request.
- We need to add “_bulk” keyword to call this API. The example of this API is already performed in populate Elasticsearch article.
- All other functionalities are same as of GET API.