# Transform - List the unique values

**URL:** <https://discuss.elastic.co/t/transform-list-the-unique-values/311132>\
**Category:** Elasticsearch\
**Created:** [August 1, 2022, 3:09pm UTC](https://discuss.elastic.co/t/transform-list-the-unique-values/311132 "2022-08-01T15:09:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bianca6](https://avatars.discourse-cdn.com/v4/letter/b/85e7bf/32.png) [@bianca6](https://discuss.elastic.co/u/bianca6)\
**Post date:** [August 1, 2022, 3:09pm UTC](https://discuss.elastic.co/t/transform-list-the-unique-values/311132/1 "2022-08-01T15:09:19Z")

</div>

Hi,

I'm using a Transform to group logs related to the same event.

An event can have multiple IP.

A simple aggregation like this:

```auto
"test_agg": {
  "terms": {
    "field": "vm_ip"
  }
},

```

Will list all the possible IP and count them. Later, the IP will be a field name and its count the value. Something like this:

```auto
test_agg: {
  XX.XX.XX.XX: 4,
  YY.YY.YY.YY: 7,
}

```

How can I list all the possible values in an array just like this:

```auto
my_agg: {
  [XX.XX.XX.XX, YY.YY.YY.YY]
}

```

Thanks a lot for your time!

---

<div class="post-metadata">

**Author:** ![Hendrik\_Muhs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hendrik_muhs/32/25802_2.png) [@Hendrik\_Muhs](https://discuss.elastic.co/u/Hendrik_Muhs)\
**Post date:** [August 2, 2022, 7:09am UTC](https://discuss.elastic.co/t/transform-list-the-unique-values/311132/2 "2022-08-02T07:09:19Z")

</div>

There is no out of the box functionality that can do that as far as I know, but you can use scripting:

Option A: Write a `scripted_metric` aggregation to collect and write the results as flat list.

Option B: Use `terms` and re-map the data in an ingest pipeline with a `script` processor to drop the counts.

---

<div class="post-metadata">

**Author:** ![bianca6](https://avatars.discourse-cdn.com/v4/letter/b/85e7bf/32.png) [@bianca6](https://discuss.elastic.co/u/bianca6)\
**Post date:** [August 2, 2022, 8:34am UTC](https://discuss.elastic.co/t/transform-list-the-unique-values/311132/4 "2022-08-02T08:34:37Z")

</div>

Painless is like Java, so we can remove the duplicate like this:

```auto
"ips": {
  "scripted_metric": {

    "init_script": "state.docs = []",

    "map_script": """
       state.docs.add(doc['hostname'].value)
    """,

    "combine_script": "return state.docs;",

    "reduce_script": """
      def all_docs = [];
      for (s in states) {
        for (span in s) {
          all_docs.add(span);
        }
      }
      return all_docs.stream().distinct().collect(Collectors.toList());
    """
    }
 }

```

I don't know if it's efficient, but it does return the expected result.

---

<div class="post-metadata">

**Author:** ![Hendrik\_Muhs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hendrik_muhs/32/25802_2.png) [@Hendrik\_Muhs](https://discuss.elastic.co/u/Hendrik_Muhs)\
**Post date:** [August 2, 2022, 1:02pm UTC](https://discuss.elastic.co/t/transform-list-the-unique-values/311132/5 "2022-08-02T13:02:54Z")

</div>

If you use a set you can deduplicate on the fly:

```auto
    "ips": {
      "scripted_metric": {
        "init_script": "state.docs = new HashSet()",
        "map_script": """
       state.docs.add(doc['hostname'].value)
    """,
        "combine_script": "return state.docs;",
        "reduce_script": """
      def all_docs = new HashSet();
      for (s in states) {
        all_docs.addAll(s);
      }
      return all_docs;
    """
      }
    }

```

---

<div class="post-metadata">

**Author:** ![bianca6](https://avatars.discourse-cdn.com/v4/letter/b/85e7bf/32.png) [@bianca6](https://discuss.elastic.co/u/bianca6)\
**Post date:** [August 2, 2022, 1:30pm UTC](https://discuss.elastic.co/t/transform-list-the-unique-values/311132/6 "2022-08-02T13:30:50Z")

</div>

Thanks a lot!

---

<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:** [August 30, 2022, 1:31pm UTC](https://discuss.elastic.co/t/transform-list-the-unique-values/311132/7 "2022-08-30T13:31:27Z")

</div>

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