Elasticsearch bulk operation: insert if not existing, otherwise update or add a nested element

In elasticsearch 7.11.0 I have products as described.

{
  "_index" : "products",
  "_type" : "_doc",
  "_id" : "1234567890123",
  "_source" : {
    "ean" : "1234567890123",
    "title" : "Very nice product",
    "merchants" : [
      {
        "id" : 2,
        "name" : "example.com",
        "product_id" : "22561",
        "description" : "Description of the product",
        "price" : "4.25",
        "url" : "https://example.com/product",
        "imageUrl" : "https://example.com/images/product.jpg",
        "in_stock" : true
      }
    ]
  }
}

I've added this product with a bulk index operation. It will update it if it exists and create it if it doesn't.

I'm receiving products from multiple merchants. A product can exist in one or more merchants. That's why I have the nested element merchants.

What is want to do as a bulk operation is the following:

Import runs:

  • Product doesn't exist: Create a new product with a nested merchant
  • Product exists: Check if the merchant exists. If not, create a new nested element, otherwise update the nested elements

I can do it one by one but due to the number of products it's not very convenient. I'm using the elasticsearch PHP package.