9

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" : [ ]
}
Chirdeep Tomar
  • 4,281
  • 8
  • 37
  • 66

1 Answers1

5

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.

Russ Cam
  • 124,184
  • 33
  • 204
  • 266
  • Russ, just tried after installing the plugin, now response.IsValid is true but the data is not deleted. – Chirdeep Tomar Jul 18 '16 at 18:56
  • Could it be coz I am not explicit about the type name in the query, except the generic parameter class. – Chirdeep Tomar Jul 18 '16 at 19:02
  • what version of NEST are you using? What version of Elasticsearch are you running against? – Russ Cam Jul 18 '16 at 23:15
  • Nest 2.3.2 and current elasticsearch 2.3.4 – Chirdeep Tomar Jul 18 '16 at 23:26
  • What do the request and response json look like (set `.DisableDirectStreaming()` on Connection settings before passing to the `ElasticClient` constructor). – Russ Cam Jul 18 '16 at 23:31
  • How do I get the Json response? APIs have changed. – Chirdeep Tomar Jul 19 '16 at 22:07
  • If you _need_ the json response in addition to the deserialized response then you can set `.DisableDirectStreaming()` on ConnectionSettings before passing it to `ElasticClient` constructor. This will buffer the response bytes in the `.ResponseBodyInBytes` on the response, which you can then get the json string from. See https://www.elastic.co/guide/en/elasticsearch/client/net-api/2.x/connecting.html#complex-logging that uses the settings, along with `OnRequestCompleted(delegate)` to capture all requests/responses – Russ Cam Jul 19 '16 at 23:01
  • The response indicates that no documents were deleted from any indices. Are you sure you have a document with `versionId` `601ccb14-c996-41ab-9783-6864db4fa452` in index `indexName` (which can't be the real index name, as it needs to be lowercase)? The `took` of 0 is a little suspicious, but could be a rounding down. – Russ Cam Jul 28 '16 at 09:11
  • Sorry, indexName was just me being cryptic.Updated the response in Sense – Chirdeep Tomar Jul 28 '16 at 19:20
  • @ChirdeepTomar is what is being sent from NEST the same as what you see in Sense? – Russ Cam Aug 10 '16 at 05:50
  • I can't get the actual JSON query in C# – Chirdeep Tomar Aug 10 '16 at 08:17
  • set `.DisableDirectStreaming()` on `ConnectionSettings`, then pass a delegate to `OnRequestCompleted()` on `ConnectionSettings`. See http://stackoverflow.com/questions/38207159/using-inmemoryconnection-to-test-elasticsearch for an example – Russ Cam Aug 10 '16 at 08:40
  • I think it might know the reason for it to now work coz VersionId is not set as NotAnalyzed and term query wouldn't work properly without that. – Chirdeep Tomar Aug 11 '16 at 10:04