How to delete particular document from multiple indices in elasticsearch using python?


(Akash Talole) #1

Here is sample document.

{
"_index": "mqtt-index-2018.01.22",
"_type": "iot_data",
"_id": "AHWewerdfdfdfdfRTRRfdfgf",
"_score": 1,
"_source": {
"message": "{\"datastream_name\": \"teststream1\", \"value\": 2, \"context\": {\"latitude\": 0, 
\"elevation\": 0, \"longitude\": 0}, \"device_id\": 15}",
"@version": "1",
"@timestamp": "2018-01-22T11:47:52.175Z",
"host": "iot-elk",
"topic": "telemetry/df42e78206d946a3a2863d167af9063b/teststream1",
"parsedMessage": {
"datastream_name": "teststream1",
  "value": 2,
  "context": {
    "latitude": 0,
    "elevation": 0,
    "longitude": 0
  },
  "device_id": 15,
  "tstamp": "2018-01-22T11:47:52.175Z"
}
},
"fields": {
"parsedMessage.tstamp": [
  1516621672175
],
"@timestamp": [
  1516621672175
]
}
}

I want to delete document using parsedMessage.device_id. how to delete it using python client?


(David Pilato) #2

Please format your code using </> icon as explained in this guide and not the citation button. It will make your post more readable.

Or use markdown style like:

```
CODE
```

Please edit your post.

You can use delete by query feature but I don't know how you call it from python.
Easier may be to do that in Kibana Dev Console.


(Akash Talole) #3

It works. Deleting document from multiple indices in elasticsearch using following DELETE API call.

curl -XDELETE 'http://localhost:9200/mqtt-index-*/logs/_query' -d '{
"query" : {
    "match" : {"device_id": 31}
}
}' -i

(Akash Talole) #4

You can also delete documents with matching multiple fields.

curl -XDELETE 'http://localhost:9200/mqtt-index-*/logs/_query' -d '{
"query" : {
    "bool": {
        "must":[
    {"match" : {"device_id":31}}, 
    {"match":  {"datastream_name": "test"}}
    ]
}
}' -i

(system) #5

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.