# Accessing fields in last\_doc scripted metric aggregation in a transform

**URL:** <https://discuss.elastic.co/t/accessing-fields-in-last-doc-scripted-metric-aggregation-in-a-transform/345403>\
**Category:** Elasticsearch\
**Tags:** painless, transforms\
**Created:** [October 19, 2023, 2:40pm UTC](https://discuss.elastic.co/t/accessing-fields-in-last-doc-scripted-metric-aggregation-in-a-transform/345403 "2023-10-19T14:40:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Duncan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_duncan/32/46467_2.png) [@Mark\_Duncan](https://discuss.elastic.co/u/Mark_Duncan)\
**Post date:** [October 19, 2023, 2:40pm UTC](https://discuss.elastic.co/t/accessing-fields-in-last-doc-scripted-metric-aggregation-in-a-transform/345403/1 "2023-10-19T14:40:17Z")

</div>

If I have implemented this last\_doc scripted metric aggregation in a transform example, how could I access fields within the returned doc (let's imagine there is a field "price" in the last\_doc), in bucket script metric in the same transform, for instance,

```auto

          "example": {
            "bucket_script": {
              "buckets_path": {
                "last_price": "last_doc.price",
[...]

```

### scripted metric aggregation[edit](https://github.com/elastic/elasticsearch/edit/7.17/docs/reference/transform/painless-examples.asciidoc)

thanks  
Mark

---

<div class="post-metadata">

**Author:** ![Mark\_Duncan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_duncan/32/46467_2.png) [@Mark\_Duncan](https://discuss.elastic.co/u/Mark_Duncan)\
**Post date:** [October 19, 2023, 2:41pm UTC](https://discuss.elastic.co/t/accessing-fields-in-last-doc-scripted-metric-aggregation-in-a-transform/345403/2 "2023-10-19T14:41:05Z")

</div>

here is the last\_doc scripted metric example/documentation

> **[Painless examples for transforms | Elasticsearch Guide \[7.17\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/transform-painless-examples.html)**

---

<div class="post-metadata">

**Author:** ![przemekwitek](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/przemekwitek/32/79526_2.png) [@przemekwitek](https://discuss.elastic.co/u/przemekwitek)\
**Post date:** [October 25, 2023, 11:39am UTC](https://discuss.elastic.co/t/accessing-fields-in-last-doc-scripted-metric-aggregation-in-a-transform/345403/3 "2023-10-25T11:39:07Z")

</div>

Not exactly on the topic, but have you tried using `latest` transform instead of `pivot` transform for your use-case?

[https://www.elastic.co/blog/how-to-use-transforms-to-track-your-most-recent-customer-orders](https://www.elastic.co/blog/how-to-use-transforms-to-track-your-most-recent-customer-orders)

---

<div class="post-metadata">

**Author:** ![Mark\_Duncan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_duncan/32/46467_2.png) [@Mark\_Duncan](https://discuss.elastic.co/u/Mark_Duncan)\
**Post date:** [October 25, 2023, 1:02pm UTC](https://discuss.elastic.co/t/accessing-fields-in-last-doc-scripted-metric-aggregation-in-a-transform/345403/4 "2023-10-25T13:02:51Z")

</div>

Yes the latest transform is a helpful tool, for sure.

But do you know it you can add additional metrics like bucket scripts to do further calculations against the latest doc?

And also, I use the scripted metrics to pull \>1 indexes data into a single transform destination doc... Do you know if you can do that with the latest transform, i.e. have the transform combine the docs from \>1 index in the transform output? That would definitely be helpful to me.

---

<div class="post-metadata">

**Author:** ![przemekwitek](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/przemekwitek/32/79526_2.png) [@przemekwitek](https://discuss.elastic.co/u/przemekwitek)\
**Post date:** [November 21, 2023, 1:29pm UTC](https://discuss.elastic.co/t/accessing-fields-in-last-doc-scripted-metric-aggregation-in-a-transform/345403/5 "2023-11-21T13:29:10Z")

</div>

> But do you know it you can add additional metrics like bucket scripts to do further calculations against the latest doc?

Not directly within transform config, but maybe you could implement ingest pipeline that does it and attach it to the destination index using [`dest.pipeline`]([https://But](https://But) do you know it you can add additional metrics like bucket scripts to do further calculations against the latest doc?) setting?

> i.e. have the transform combine the docs from \>1 index in the transform output?

Yes, you can specify more than 1 source index in the transform configuration (see `source.index`).  
`latest` transform then "combines" them by taking the latest document (from one of the indices) with the given `unique_key`.

---

<div class="post-metadata">

**Author:** ![Mark\_Duncan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_duncan/32/46467_2.png) [@Mark\_Duncan](https://discuss.elastic.co/u/Mark_Duncan)\
**Post date:** [November 21, 2023, 4:12pm UTC](https://discuss.elastic.co/t/accessing-fields-in-last-doc-scripted-metric-aggregation-in-a-transform/345403/6 "2023-11-21T16:12:13Z")

</div>

Hi thanks. Yeah an ingest pipeline could suffice for some simple calcs. Thanks!

---

<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:** [December 19, 2023, 4:12pm UTC](https://discuss.elastic.co/t/accessing-fields-in-last-doc-scripted-metric-aggregation-in-a-transform/345403/7 "2023-12-19T16:12:27Z")

</div>

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