2

Using deleteByQuery() in the Javascript API I would like to delete some documents from an index called people.

It might be important to note that this is version 5.1 in AWS Elastic Search Service.

The index mapping looks like this:

"people": {
  "mappings": {
    "person": {
      "properties": {
        "name": {
          "type": "string"
        },
        "parent_id": {
          "type": "long"
        },
        "query": {
          "properties": {
            "match": {
              "properties": {
                "parent_id": {
                  "type": "long"
                }
              }
            },
            "term": {
              "properties": {
                "parent_id": {
                  "type": "long"
                }
              }
            }
          }
        }
      }
    }
  }
}

And a search query that is structured as follows:

query: {
  term: {
    parent_id: 1
  }
}

Responds with:

"hits": {
  "total": 1,
  "max_score": 10.135444,
  "hits": [
    {
      "_index": "people",
      "_type": "person",
      "_id": "AVp6EDRVm933W5mwl4O9",
      "_score": 10.135444,
      "_source": {
        "name": "Bob",
        "parent_id": 1
      }
    }
  ]
}

So then using the documentation for the JS API I have created the following:

const elasticsearch = require('elasticsearch')
function deletePeople (parentId) {
  new elasticsearch
  .Client({ host: 'es.host.domain' })
  .deleteByQuery({
    index: 'people',
    type: 'person',
    body: {
      query: {
        term: {
          parent_id: parentId
        }
      }
    }
  }, function afterDeletingPeople (error, response) {
    if (error) {
      return console.log("deletePeople :: Error :: ", error);
    }
    else {
      return console.log("deletePeople :: Response :: ", response);
    }
  });
};
deletePeople(1);

Which gives a response like:

info: deletePeople :: Response ::  { _index: 'people',
  _type: 'person',
  _id: '_delete_by_query',
  _version: 30,
  _shards: { total: 2, successful: 1, failed: 0 },
  created: false }

And that looks hopeful but no people are actually removed from the index. I tried to use match instead of term in the query but that does the same thing.

Could anyone suggest where I have gone wrong here with this as currently my function deletes no people? TIA!

Craig van Tonder
  • 7,497
  • 18
  • 64
  • 109
  • Looks ok to me, so if you use the exact same body in a search, you get the results, but not in the delete? – paqash Feb 26 '17 at 22:52
  • What version of ES are you using? – Val Feb 27 '17 at 05:27
  • "Delete by Query will be removed in 2.0: it is problematic since it silently forces a refresh which can quickly cause OutOfMemoryError during concurrent indexing, and can also cause primary and replica to become inconsistent. Instead, use the scroll/scan API to find all matching ids and then issue a bulk request to delete them – xecgr Feb 27 '17 at 05:45
  • @paqash That is correct. If i use the HTTP method: https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete-by-query.html it does not delete the items either. – Craig van Tonder Feb 27 '17 at 07:29
  • @Val 5.1 in AWS ElasticSearch Service – Craig van Tonder Feb 27 '17 at 07:29
  • @xecgr I am using 5.1 and its referenced in the the list of AWS endpoints (http://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/es_version_5_1.html) so I don't really understand that, should it not be throwing an error if that was the case? Interesting way to go about it though, thanks for the input! – Craig van Tonder Feb 27 '17 at 07:30

0 Answers0