Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch DeleteByQuery not working, getting 400 bad request

I have the following Nest query to delete all the matching documents, quite straight forward but I am getting 400 bad request on it.

 var client = new ElasticClient();
        var request = new DeleteByQueryRequest<Type>("my-index")
        {
            Query = new QueryContainer(
                    new TermQuery
                    {
                        Field = "versionId",
                        Value = "ea8e517b-c2e3-4dfe-8e49-edc8bda67bad"
                    }
                )
        };
        var response = client.DeleteByQuery(request);
        Assert.IsTrue(response.IsValid);

Thanks for any help.

---------------Update---------------

Request Body

{"query":{"term":{"versionId":{"value":"ea8e517b-c2e3-4dfe-8e49-edc8bda67bad"}}}}

Response Body

{"took":0,"timed_out":false,"_indices":{"_all":{"found":0,"deleted":0,"missing":0,"failed":0}},"failures":[]}

Query in Sense plugin:

GET /my-index/type/_search
{
  "query": {

          "match": {
             "versionId": "ea8e517b-c2e3-4dfe-8e49-edc8bda67bad"
          } 

  }
}

Query Response:

{
   "took": 3,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 116,
      "max_score": 2.1220484,
      "hits": []
...
}}

---------------NEST QUERY--------------

DELETE http://localhost:9200/my-index/component/_query?pretty=true
{
  "query": {
    "term": {
      "versionId": {
        "value": "ea8e517b-c2e3-4dfe-8e49-edc8bda67bad"
      }
    }
  }
}

Status: 200
{
  "took" : 0,
  "timed_out" : false,
  "_indices" : {
    "_all" : {
      "found" : 0,
      "deleted" : 0,
      "missing" : 0,
      "failed" : 0
    }
  },
  "failures" : [ ]
}
like image 825
Chirdeep Tomar Avatar asked Jul 17 '16 18:07

Chirdeep Tomar


People also ask

How does Elasticsearch handle bulk DELETE requests?

While processing a delete by query request, Elasticsearch performs multiple search requests sequentially to find all of the matching documents to delete. A bulk delete request is performed for each batch of matching documents.

How does Elasticsearch know when I delete an index?

When you submit a delete by query request, Elasticsearch gets a snapshot of the index when it begins processing the request and deletes matching documents using internal versioning.

Does Elasticsearch support wait_for_completion?

Unlike the delete API, it does not support wait_for. If the request contains wait_for_completion=false, Elasticsearch performs some preflight checks, launches the request, and returns a task you can use to cancel or get the status of the task. Elasticsearch creates a record of this task as a document at .tasks/task/$ {taskId}.

What is wait_for_active_shards in Elasticsearch?

When you are done with a task, you should delete the task document so Elasticsearch can reclaim the space. wait_for_active_shards controls how many copies of a shard must be active before proceeding with the request.


1 Answers

It sounds like you may be using Elasticsearch 2.x in conjunction with NEST 2.x. As part of Elasticsearch 2.0, Delete by query was moved out of Elasticsearch core and into a separate plugin that needs to be installed. You can install the plugin using the following command within the Elasticsearch bin directory

bin/plugin install delete-by-query

Starting up the node again, Delete by query should now work as expected.

If you ever need to get more details about why a request has failed, you can inspect the .DebugInformation on the response to get the audit trail for the request.

like image 93
Russ Cam Avatar answered Sep 17 '22 11:09

Russ Cam