How to Delete a Document by Id in Elasticsearch using NodeJS
Introduction
If you’re interacting with Elasticsearch using a NodeJS application, there may be occasions when you need to delete a specific document. Fortunately, it’s easy to use the npm Elasticsearch module to communicate with Elasticsearch and issue this command. In this tutorial, we’ll learn how to delete a document by id in Elasticsearch using NodeJS. If you’d prefer to skip the explanations and dive into the sample code, feel free to skip ahead to Just the Code.
Prerequisites
Before we attempt to delete a document by ID in Elasticsearch using NodeJS, it’s important to confirm that a few prerequisites are in place. For this task, the system requirements are minimal: NodeJS needs to be installed, Elasticsearch needs to be installed and running, and a NodeJS application needs to be connected to Elasticsearch.
Note: In our example, we’re assuming that Elasticsearch is running locally on the default port; therefore, you’ll see us communicate with Elasticsearch at
"localhost:9200"
. If your installation of Elasticsearch is running on a different server, your calls to it will take a slightly different format:"YOURDOMAIN.com:9200"
.
Use the delete function
Let’s look at an example of deleting a document by ID using NodeJS. For this example, we’ll create a sample index called store
, which represents a small grocery store. Our store
index contains the type products
which lists all of the store’s products. We’ll keep our dataset simple by including just a handful of products and a small number of fields: id, price, quantity, and department. The JSON shown below can be used to create our dataset:
id | name | price | quantity | department | |
---|---|---|---|---|---|
1 | Multi-Grain Cereal | 4.99 | 4 | Packaged Foods | |
2 | 1lb Ground Beef | 3.99 | 29 | Meat and Seafood | |
3 | Dozen Apples | 2.49 | 12 | Produce | |
4 | Chocolate Bar | 1.29 | 2 | Packaged Foods | Checkout |
5 | 1 Gallon Milk | 3.29 | 16 | Dairy | |
6 | 0.5lb Jumbo Shrimp | 5.29 | 12 | Meat and Seafood | |
7 | Wheat Bread | 1.29 | 5 | Bakery | |
8 | Pepperoni Pizza | 2.99 | 5 | Frozen | |
9 | 12 Pack Cola | 5.29 | 6 | Packaged Foods | |
10 | Lime Juice | 0.99 | 20 | Produce | |
11 | 12 Pack Cherry Cola | 5.59 | 5 | Packaged Foods | |
12 | 1 Gallon Soy Milk | 3.39 | 10 | Dairy | |
13 | 1 Gallon Vanilla Soy Milk | 3.49 | 9 | Dairy | |
14 | 1 Gallon Orange Juice | 3.29 | 4 | Juice |
The following code contains the mapping:
1 2 3 4 5 6 7 8 9 10 11 12 13 | { "mappings": { "products": { "properties" : { "name": { "type": "text"}, "price": { "type": "double"}, "quantity": { "type": "integer"}, "department": { "type": "keyword"} } } } } ' |
Let’s say we want to delete the “Lime Juice” product, which has an id of “10”, from this index. We’ll be adding code to accomplish this to our main application file: index.js
. In this file, we connect to Elasticsearch with the following code:
1 2 3 4 5 | var elasticsearch = require("elasticsearch"); var client = new elasticsearch.Client({ hosts: ["http://localhost:9200"] }); |
We’ll call the delete
function on our client, specifying the product id “10”, with the following code:
1 2 3 4 5 | await client.delete({ index: "store", type: "products", id: "10" }); |
Let’s take a closer look at what’s happening in this code. We called the delete
function on our client
, supplying the various parameters needed to delete a single document: the index, the type, and the id.
The delete
function definition is as follows:
1 | client.delete([params, [callback]]); |
Since our example showed a fairly simple case of deleting a document by id, it doesn’t illustrate the full set of parameters that are available for use. For more information on these parameters, consult Elasticsearch’s documentation.
It would be helpful to know whether our code worked, so let’s add a callback to our code to verify that the delete occurred successfully:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | var elasticsearch = require("elasticsearch"); var client = new elasticsearch.Client({ hosts: ["http://localhost:9200"] }); client .delete({ index: "store", type: "products", id: "10" }) .then( function(resp) { console.log(resp); }, function(err) { console.trace(err.message); } ); |
At this point, we can run our Node application from the terminal like this:
1 | $ node index.js |
The response you receive will look something like this:
1 2 3 4 5 6 7 8 | { _index: 'store', _type: 'products', _id: '10', _version: 2, result: 'deleted', _shards: { total: 2, successful: 1, failed: 0 }, _seq_no: 5, _primary_term: 1 } |
You can see from the successful: 1
response that our deletion was a success. If you need further confirmation, try querying your index for “Lime Juice”, and you’ll see that it’s no longer there.
Conclusion
Deleting a document is permanent, so it’s important to understand how to execute the operation correctly. In this tutorial, we explained how to delete a document by id within a NodeJS application. With the step-by-step instructions in this guide, you’ll be ready to handle any delete tasks needed in your application. For information on all the delete options available in the API, see Elasticsearch’s documentation.
Just the Code
If you’re already comfortable with the concepts explained in this tutorial, here’s all the code you’ll need to delete a document by id in Elasticsearch using NodeJS:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | var elasticsearch = require("elasticsearch"); var client = new elasticsearch.Client({ hosts: ["http://localhost:9200"] }); client .delete({ index: "store", type: "products", id: "10" }) .then( function(resp) { console.log(resp); }, function(err) { console.trace(err.message); } ); |
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started