I'm trying to append data to an array inside my document without success. Here is my code :
doc = {
"field_1":"1",
"field_2":"2",
"tool_result":[{
"my_array":["data","data2"],
}]
}
es.index(index="my_index", id=1, document=doc)
update_query = {
"script": {
"source": "ctx._source.tool_result.addAll(params.tool_result)",
"lang": "painless",
"params": {
"tool_result": [{"my_array": ["data3", "data4"]}]
}
}
}
es.update(index="my_index", id=1, body=update_query)
But then the result is :
{
"field_1":"1",
"field_2":"2",
"tool_result":[{
"my_array":["data","data2"]
},
{
"my_array":["data3","data4"],
}]
}
And I'd like to have instead :
{
"field_1":"1",
"field_2":"2",
"tool_result":[{
"my_array":["data","data2","data3","data4"],
}]
}
Thank you for your help !
CodePudding user response:
Data
POST test5/_doc
{
"field_1": "1",
"field_2": "2",
"tool_result": [
{
"my_array": [
"data",
"data2"
]
}
]
}
Query
Below will give you some idea how to resolve.
POST test5/_update_by_query
{
"script": {
"lang": "painless",
"inline": """
for(int i=0;i<params.data.length;i )
{
ctx._source.tool_result[0].my_array.add(params.data[i])
}
""",
"params": {
"data": [
"data3",
"data4"
]
}
}
}
CodePudding user response:
Thank you for your help, with your code I was able to solve my problem with a more simple query:
update_query = {
"script": {
"source": "ctx._source.tool_result[0].my_array.addAll(params.data)",
"lang": "painless",
"params": {
"data": ["data3", "data4"]
}
}
}
es.update(index="my_index", id=1, body=update_query)