# Transform API update documents already inserted

**URL:** https://discuss.elastic.co/t/transform-api-update-documents-already-inserted/288006
**Category:** Elasticsearch
**Created:** [October 29, 2021, 1:40pm UTC](https://discuss.elastic.co/t/transform-api-update-documents-already-inserted/288006 "2021-10-29T13:40:56Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![marone](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marone/32/87145_2.png) [@marone](https://discuss.elastic.co/u/marone)
#### Post date: [October 29, 2021, 1:40pm UTC](https://discuss.elastic.co/t/transform-api-update-documents-already-inserted/288006/1 "2021-10-29T13:40:56Z")

</div>

Hey I am facing a big issue as we are going to production using Transform API. What we are facing is the aggregation calculated with a transform API are updated once a term aggregation already exist in the destination index.

Here is the transformation:

```auto
POST _transform/_preview
{
  "source": {
    "index": "sourceIndex",
    "query": {
      "bool": {
        "filter": [
          {
            "range": {
              "@timestamp": {
                "gte": "now/d-7d-7d",
                "lt": "now/d-7d"
              }
            }
          }
        ]
      }
    }
  },
  "dest": {
    "index": "destinationIndex",
   "pipeline": "add_ingestedAt"
  },
  "pivot": {
    "group_by": {
      "os": { "terms": { "field":"infos.os"}}
    },
    "aggs": {
      "mac_unique": {
        "cardinality": {
          "field": "mac_adress"
        }
      }
    }
  }
}

```

The following response is:

```auto
"preview" : [
    {
      "os" : "os1",
      "mac_unique" : 9,
      "ingestedAt": "2021-10-19T12:34:25Z"
    },
    {
      "os" : "os2",
      "mac_unique" : 3,
      "ingestedAt": "2021-10-19T12:34:25Z"
    },
    {
      "os" : "os3",
      "mac_unique" : 3,
      "ingestedAt": "2021-10-19T12:34:25Z"
    }
  ]
....

```

The problem is, when I insert a new document, let's say having `os=os3`, what I expect is having a new insertion in the destination index as follow:

```auto
"preview" : [
    {
      "os" : "os1",
      "mac_unique" : 9,
      "ingestedAt": "2021-10-19T12:34:25Z"
    },
    {
      "os" : "os2",
      "mac_unique" : 3,
      "ingestedAt": "2021-10-19T12:34:25Z"
    },
    {
      "os" : "os3",
      "mac_unique" : 3,
      "ingestedAt": "2021-10-19T12:34:25Z"
    },
    {
      "os" : "os3",
      "mac_unique" : 1,
      "ingestedAt": "2021-10-20T12:34:25Z"
    },
  ]
....

```

But it gets updated like this:

```auto
"preview" : [
    {
      "os" : "os1",
      "mac_unique" : 9,
      "ingestedAt": "2021-10-19T12:34:25Z"
    },
    {
      "os" : "os2",
      "mac_unique" : 3,
      "ingestedAt": "2021-10-19T12:34:25Z"
    },
    {
      "os" : "os3",
      "mac_unique" : 4,
      "ingestedAt": "2021-10-20T12:34:25Z"
    }
  ]
....

```

Why does this happens as we expect a new insertion as a raw data in the destination index? any workaround/suggestion please ?

---

<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: [November 26, 2021, 1:41pm UTC](https://discuss.elastic.co/t/transform-api-update-documents-already-inserted/288006/2 "2021-11-26T13:41:54Z")

</div>

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