0

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.

1 Answer 1

1

What you need is like this:

POST index/_doc/20/_update
{
  "script": {
    "lang": "painless",
    "source": "ctx._source.devices.removeIf(device -> device.id == 'sr_31')"
  }
}
Sign up to request clarification or add additional context in comments.

5 Comments

I've already seen this part of the document and it just searches for nested fields, the thing is that I want to remove the searched object.
@MajidAlaeinia What you need is delete_by_query
delete_by_query deletes the whole document that matches that query, I just want to delete part of the document (update the document by deleting on matching nested object).
@MajidAlaeinia Check my updated answer.
Thank you so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.