I've got a document in elasticsearch which is like this:
{
"_index": "user",
"_type": "_doc",
"_id": "20",
"_score": 1,
"_source": {
"id": "20",
"gender": null,
"uuid": "68de0b74-cdf6-4e21-a046-6876e569e4e3",
"first_name": null,
"last_name": null,
"nick_name": null,
"email": null,
"country_code": "98",
"mobile": "9xxx7",
"password": null,
"old_password": null,
"birthdate": null,
"email_verified_at": null,
"created_at": null,
"updated_at": null,
"deleted_at": null,
"old_data": null,
"devices": [
{
"updated_at": "2021-12-14T12:11:26.000000Z",
"serial": "sr_31",
"vendor": "folan",
"created_at": "2021-12-14T12:11:26.000000Z",
"model": "device_model",
"id": 4,
"device_app": {
"device_id": 4,
"updated_at": "2021-12-14T12:11:26.000000Z",
"created_at": "2021-12-14T12:11:26.000000Z",
"id": 4,
"uuid": "f60f7df0-5cd6-11ec-b71d-bdfd039f50b3"
},
"uuid": "f60a2ae0-5cd6-11ec-81cb-0128d6059ef1"
},
{
"updated_at": "2021-12-14T12:13:07.000000Z",
"serial": "sr_32",
"vendor": "folan",
"created_at": "2021-12-14T12:13:07.000000Z",
"model": "device_model",
"id": 5,
"device_app": {
"device_id": 5,
"updated_at": "2021-12-14T12:13:07.000000Z",
"created_at": "2021-12-14T12:13:07.000000Z",
"id": 5,
"uuid": "32481b80-5cd7-11ec-ba02-c33a09f165eb"
},
"uuid": "3242f860-5cd7-11ec-9d54-c3339177d9bb"
}
]
}
}
As you can see, I've got a field named devices
which is an array of objects. I want to delete its first element not by selecting the index number of the array but with the content of the array, meaning that **I want to give the script the serial
field (which is unique for each device) and delete the whole object of the corresponding object.
Let's say, I want some input to be sr_31
and the output would be something like this (only the object containgin sr_31
is omitted):
{
"_index": "user",
"_type": "_doc",
"_id": "20",
"_score": 1,
"_source": {
"id": "20",
"gender": null,
"uuid": "68de0b74-cdf6-4e21-a046-6876e569e4e3",
"first_name": null,
"last_name": null,
"nick_name": null,
"email": null,
"country_code": "98",
"mobile": "9xxx7",
"password": null,
"old_password": null,
"birthdate": null,
"email_verified_at": null,
"created_at": null,
"updated_at": null,
"deleted_at": null,
"old_data": null,
"devices": [
{
"updated_at": "2021-12-14T12:13:07.000000Z",
"serial": "sr_32",
"vendor": "folan",
"created_at": "2021-12-14T12:13:07.000000Z",
"model": "device_model",
"id": 5,
"device_app": {
"device_id": 5,
"updated_at": "2021-12-14T12:13:07.000000Z",
"created_at": "2021-12-14T12:13:07.000000Z",
"id": 5,
"uuid": "32481b80-5cd7-11ec-ba02-c33a09f165eb"
},
"uuid": "3242f860-5cd7-11ec-9d54-c3339177d9bb"
}
]
}
}
I have read the document for updating a doc already, but there seems no example for this situation.
CodePudding user response:
What you need is like this:
POST index/_doc/20/_update
{
"script": {
"lang": "painless",
"source": "ctx._source.devices.removeIf(device -> device.id == 'sr_31')"
}
}