Pkzzz
Pkzzz

Reputation: 111

Elasticsearch how to delete data without id from an index?

There is an item have no id, any way to delete it unless using _delete_by_query?

the one I wanna delete like this:

"hits": {
    "total": 230,
    "max_score": 1,
    "hits": [
      {
        "_index": "info",
        "_type": "default",
        "_id": "",
        "_score": 1,
        "_source": {
          "city_id": 1,
        }
      },

I tried python by:

query ={
   "query":{
     "match":{
       "city_id":1
     }
    }
}
es.delete_by_query(index,type,query)

And doesn't work, the bad data still in the index. {u'_type': u'default', u'created': False, u'_shards': {u'successful': 2, u'failed': 0, u'total': 2}, u'_version': 8, u'_index': u'info', u'_id': u'_delete_by_query'}

Upvotes: 1

Views: 1416

Answers (3)

Sahil Gupta
Sahil Gupta

Reputation: 2166

Please try the below query. You have set _id as an empty string, which seems valid. Hence delete using id where-in id is "".

DELETE <indexName>/_doc/""

I tried reproducing locally and it worked for me.

Upvotes: 0

Lunatic
Lunatic

Reputation: 1916

I do believe that elastic search always generates an _id for all indexed documents and probably in indexing stage you set it as null, Either way you can simply use the delete_by_query Api and delete your document with following script

POST /info/_delete_by_query
{
  "query": {
    "match": {
      "city_id": "1"
    }
  }
}

Upvotes: 0

dadoonet
dadoonet

Reputation: 14492

An elasticsearch document has always an _id field. It can be automatically generated by Elasticsearch or provided by you.

So the first thing to do is to find your document, with a _search call. Then extract the _id and run:

DELETE INDEX/_doc/ID

Which is basically what the delete by query is doing behind the scene.

Upvotes: 3

Related Questions