Deleting documents in Elasticsearch is possible in two ways: using the DELETE
call or the delete_by_query
call, which we'll see in the next chapter.
You need an up-and-running Elasticsearch installation, as used in the Downloading and installing Elasticsearch recipe in Chapter 2, Downloading and Setup.
To execute curl
via the command line, you need to install curl
for your operative system.
To correctly execute the following commands, use the indexed document in the Indexing a document recipe.
The REST API URL is the same as the GET
calls, but the HTTP method is DELETE:
http://<server>/<index_name>/<type_name>/<id>
To delete a document, we will perform the following steps:
order
indexed in the Indexing a document recipe, the call to delete a document will be:curl -XDELETE 'http://localhost:9200/myindex/order/2qLrAfPVQvCRMe7Ku8r0Tw'
{ "found" : true, "_index" : "myindex", "_type" : "order", "_id" : "2qLrAfPVQvCRMe7Ku8r0Tw", "_version" : 4, "result" : "deleted", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 } }
404
is returned as status code and the return JSON will be:{ "found" : false, "_index" : "myindex", "_type" : "order", "_id" : "2qLrAfPVQvCRMe7Ku8r0Tw", "_version" : 5, "result" : "not_found", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 } }
Deleting records only hits shards that contain documents, so there is no overhead. If the document is a child, the parent must be set to look for the correct shard.
There are several additional parameters that can be used to control the delete call. The most important ones are:
routing
allows specifying the shard to be used for the delete operationversion
allows defining a version of the document to be deleted to prevent modification on this documentparent
, similar to routing, is required if the document is a child oneDeleting a record is a fast operation, very easy to use if the IDs of the document to delete are available. Otherwise, we must use the delete_by_query
call, which we will see in the next chapter.
18.226.181.89