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.
Request Body
Response
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.
- 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 −
- (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:
Request Body
elasticsearch - elasticsearch tutorial - elastic search - elasticsearch sort - elasticsearch list indexes - elasticsearch node
Response
- 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.
Request Body
Response
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.
Request Body
- Note − If you get exception while executing this example, please recreate the index by adding the following in the index.
Timeout
Request Body
Get API
- API helps to extract type JSON object by performing a get request for a particular document.
For example:
Response
- 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.
Response
- You can also fetch the source part in your result by just adding _source part in your get request.
Response
- 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,
Response
- 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
Response
- Note − If you get script exception, it is recommended to add the following lines in elastcisearch.yml
- You can check the update by sending get request to the updated document.
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.
Request Body
Response
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.