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

**URL:** <https://discuss.elastic.co/t/elasticsearch-bulk-operation-insert-if-not-existing-otherwise-update-or-add-a-nested-element/265034>\
**Category:** Elasticsearch\
**Created:** [February 22, 2021, 7:50am UTC](https://discuss.elastic.co/t/elasticsearch-bulk-operation-insert-if-not-existing-otherwise-update-or-add-a-nested-element/265034 "2021-02-22T07:50:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![corlee](https://avatars.discourse-cdn.com/v4/letter/c/90db22/32.png) [@corlee](https://discuss.elastic.co/u/corlee)\
**Post date:** [February 22, 2021, 7:50am UTC](https://discuss.elastic.co/t/elasticsearch-bulk-operation-insert-if-not-existing-otherwise-update-or-add-a-nested-element/265034/1 "2021-02-22T07:50:24Z")

</div>

In elasticsearch `7.11.0` I have products as described.

```auto
{
  "_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`.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 22, 2021, 7:51am UTC](https://discuss.elastic.co/t/elasticsearch-bulk-operation-insert-if-not-existing-otherwise-update-or-add-a-nested-element/265034/2 "2021-03-22T07:51:22Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
