Deleting a document

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.

Getting ready

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.

How to do it...

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:

  1. If we consider the order indexed in the Indexing a document recipe, the call to delete a document will be:
            curl -XDELETE 
            'http://localhost:9200/myindex/order/2qLrAfPVQvCRMe7Ku8r0Tw'
    
  2. The result returned by Elasticsearch should be:
            { 
              "found" : true, 
              "_index" : "myindex", 
              "_type" : "order", 
              "_id" : "2qLrAfPVQvCRMe7Ku8r0Tw", 
              "_version" : 4, 
              "result" : "deleted", 
              "_shards" : { 
                "total" : 2, 
                "successful" : 1, 
                "failed" : 0 
              } 
            } 
    
  3. If the record is missing, an error 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 
              } 
            } 
    

How it works...

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 operation
  • version allows defining a version of the document to be deleted to prevent modification on this document
  • parent, similar to routing, is required if the document is a child one

Tip

The DELETE operation has to restore functionality. Every document that is deleted is lost forever.

Deleting 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.

See also

  • Refer to the Deleting by query recipe in Chapter 5, Search to delete a bunch of documents that match a query
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.181.89