Search code examples
javascriptnode.jselasticsearchelastic-stacknode-red

Delete By Query API working as curl but not in Node-Red


Background: What I am trying to achieve is to delete multiple values from elastic using a single API call. Our app uses Node-Red to create the backend API's.

I am using below curl command to delete multiple doc id's and it is working like a charm. It deletes the docs found with id's xxxxx and yyyyy.

POST /tom-access/doc/_delete_by_query
{
  "query": {
    "terms": {
      "_id": [
        "xxxxx",
        "yyyyy"
      ]
    }
  }
}

However, when I try to do the same via Node-Red (using a JavaScript function), I am getting below error.

{"error":{"root_cause":[{"type":"action_request_validation_exception","reason":"Validation Failed: 1: query is missing;"}],"type":"action_request_validation_exception","reason":"Validation Failed: 1: query is missing;"},"status":400}

Here is what I have inside the Node-Red JavaScript function:

if (!msg.headers) msg.headers = {};
msg.req = {
  "query": {
    "terms": {
      "id": [
        "xxxxx",
        "yyyyy"
      ]
    }
  }  
};
msg.headers = {
          "Content-Type": "application/json",
          "Authorization" : "Basic xxxxxxxxxxxxxxxxxxxxx"
          };
msg.method = "POST"

// New elastic
msg.url = "http://elastic.test.com/tom-access/doc/_delete_by_query";
return msg;

The next node makes an HTTP CALL using above msg object but results in the error mentioned above. I am new to Node-Red, JavaScript and Elastic as well. HEEELP!!!


Solution

  • The endpoint is probably expecting the query to be in the body of the requests.

    You should be setting it under msg.payload not msg.req.