# Creating an array of ids using Elasticsearch

**URL:** <https://discuss.elastic.co/t/creating-an-array-of-ids-using-elasticsearch/221332>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [February 27, 2020, 10:05pm UTC](https://discuss.elastic.co/t/creating-an-array-of-ids-using-elasticsearch/221332 "2020-02-27T22:05:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![EZprogramming](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ezprogramming/32/57291_2.png) [@EZprogramming](https://discuss.elastic.co/u/EZprogramming)\
**Post date:** [February 27, 2020, 10:05pm UTC](https://discuss.elastic.co/t/creating-an-array-of-ids-using-elasticsearch/221332/1 "2020-02-27T22:05:09Z")

</div>

**Background information:**

I'm using Elastic APM to collect API data and send it to Elasticsearch. Now I would like to run a watcher that searches the data for latency. I would like to group all the trace.id of all the documents that pass the 1-second threshold, and then include them in a link that once the user clicks on it, it redirects them to Kibana. I'm having problems with grouping the trace.id, I don't know how it is done in Elasticsearch query or painless script.

Here is the logic I would like to accomplish in python:

**Python code:**

```
arr = []
if service_name == 'TEST-B2-UI' and transaction_duration >= 1000000:
    arr.append(trace_id)

```

**Current Elasticsearch Code (Fails to execute):**

```
PUT _watcher/watch/prod-b2-ui-latency
{
  "trigger": {
"schedule": {
  "interval": "6h"
}
  },
  "input": {
"search": {
  "request": {
    "search_type": "query_then_fetch",
    "indices": [
      "apm-*"
    ],
    "rest_total_hits_as_int": true,
    "body": {
      "query": {
        "bool": {
          "filter": [
            {
              "script": {
                "script": {
                  "source": "doc['service.name'].value == {{ctx.metadata.service_name}} && doc['transaction.duration.us'].value >= {{ctx.metadata.latency_threshold}}"
                }
              }
            },
            {
              "range": {
                "@timestamp": {
                  "from": "now-6h/h",
                  "to": "now"
                }
              }
            }
          ]
        }
      },
      "script": {
        "inline": "doc['tags'].value += doc['trace.id].value",
        "lang": "painless"
      },
      "sort": {
        "transaction.duration.us": "desc",
        "@timestamp": "desc"
      },
      "_source": [
        "@timestamp",
        "transaction.duration.us",
        "transaction.name",
        "service.name",
        "trace.id"
      ]
    }
  }
}
  },
  "condition": {
"compare": {
  "ctx.payload.hits.total": {
    "gt": 0
  }
}
}

```

The issue comes from this script code in the above code:

```
"script": {
    "inline": "doc['tags'].value += doc['trace.id].value",
    "lang": "painless"
  },

```

You can see I'm trying to execute the exact same logic here, but not sure if this is the way it is done.

Is there any way I can write Java code directly in this search query? Maybe it will be easier, unless you have a better idea in mind. I appreciate any feedback/reference.

---

<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 26, 2020, 10:05pm UTC](https://discuss.elastic.co/t/creating-an-array-of-ids-using-elasticsearch/221332/2 "2020-03-26T22:05:16Z")

</div>

Closed on March 26, 2020, 10:05pm UTC

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