0

I have documents containing nested tables in the following format :

{
"dataId": "dataIdentifier",
"versionId": "versionIdentifier",
"items": [
{
"obj1": "value1",
"obj2": "value2",
"obj3": "value3",
 },
 {
"obj1": "value4",
"obj2": "value5",
"obj3": "value6"
}, 
{
"obj1": "value7",
"obj2": "value8",
"obj3": "value9" 
}        
]
}

With "items" being of nested type. I want to delete one of the elements inside the "items" given than one of the object inside has a certain value. For instance it would be :

if (obj1 == "value4" & dataId == "dataIdentifier" & versionId == versionIdentifier) :
    DELETE TABLE ENTRY 

In this case I want to delete the second entry of "items". I tried to do it with update_by_query, and my attempt was :

q = {
   "query": {
    "match_all": {}
    }
},
"script": {
    "lang" : "painless",
    "inline" : {if (ctx._source.dataId == item.dataId && ctx._source.versionId == item.versionId && ctx._source.items.obj1 == item.obj1) {ctx._source.items.remove()}}",
    "params" : {
        'dataFrame': [{
            "dataId" : 'myDataIdList',
            "versionId" : 'myVersionId',
            "obj1" : 'myValue'
            } ]
    }
 }    
}

es.update_by_query(index=myindex, body=q)

But I do not know how to designate the concerned entry in the "ctx._source.items.remove()" argument. I took a look at the Elasticsearch documentation but could not find what I was looking for. Any help would be greatly appreciated.

SantiStSupery
  • 202
  • 3
  • 14
  • Alright so conceptually, I must update the entire document but to achieve this, is it possible with the update by query ? And if yes how can I delete an element of a nested array based on the content of that element ? – SantiStSupery Jan 11 '19 at 16:10

1 Answers1

0

With update by query you can't remove "a field", you can replace it, for that, you can update it with others two values ​​you want to mantain or delete the document and index a new with that two values

M. Mis
  • 107
  • 1
  • 1
  • 7
  • Based on the following link I am pretty sure I can : https://discuss.elastic.co/t/delete-update-nested-documents-with-elasticsearch-java-api/11523 . My question remains how is this achievable in this precise case – SantiStSupery Jan 14 '19 at 08:39