# Aggregate fields to an array fields

**URL:** <https://discuss.elastic.co/t/aggregate-fields-to-an-array-fields/291024>\
**Category:** Logstash\
**Created:** [December 6, 2021, 9:39am UTC](https://discuss.elastic.co/t/aggregate-fields-to-an-array-fields/291024 "2021-12-06T09:39:42Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![RichYaNa](https://avatars.discourse-cdn.com/v4/letter/r/ba8739/32.png) [@RichYaNa](https://discuss.elastic.co/u/RichYaNa)\
**Post date:** [December 6, 2021, 9:39am UTC](https://discuss.elastic.co/t/aggregate-fields-to-an-array-fields/291024/1 "2021-12-06T09:39:42Z")

</div>

Hi,

I have some output here from my logstash filter:

```auto
{
   "taskid" => "123",
   "host" => "ubuntu"
},
{
  "taskid" => "123",
  "host" => "centos"
}

```

is it possible to aggregate that field to be like this:

```auto
{
  "taskid" => "123",
  "host" => ["ubuntu", "centos"]
}

```

Please for the answer.  
Thanks.

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [December 6, 2021, 7:01pm UTC](https://discuss.elastic.co/t/aggregate-fields-to-an-array-fields/291024/2 "2021-12-06T19:01:23Z")

</div>

> [@RichYaNa](#):
>
> is it possible to aggregate that field to be like this

Yes, that could be done with an aggregate filter. You might use something like [example 4](https://www.elastic.co/guide/en/logstash/current/plugins-filters-aggregate.html#plugins-filters-aggregate-example4) in the documentation, but depending on your data you might need something more like example 3.

---

<div class="post-metadata">

**Author:** ![RichYaNa](https://avatars.discourse-cdn.com/v4/letter/r/ba8739/32.png) [@RichYaNa](https://discuss.elastic.co/u/RichYaNa)\
**Post date:** [December 7, 2021, 6:45am UTC](https://discuss.elastic.co/t/aggregate-fields-to-an-array-fields/291024/3 "2021-12-07T06:45:19Z")

</div>

Hi Badger,

Thanks for the reply.

I have tried that and it's work. But now the problem is,

When the data is like this,

```auto
{
   "taskid" => "123",
   "host" => "ubuntu"
},
{
  "taskid" => "123",
  "host" => "centos"
},
{
  "taskid" => "123",
  "host" => "ubuntu"
},
{
  "taskid" => "123",
  "host" => nil,
  "ip" => "1.2.3.4"
}

```

The output become like this,

```auto
{
  "taskid" => "123",
  "host" => ["ubuntu", "centos", "ubuntu", nil],
  "ip" => "1.2.3.4"
}

```

is there any way to prevent the multiple value and the empty value?

I can't drop the empty host field because i need the ip field.

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [December 7, 2021, 6:10pm UTC](https://discuss.elastic.co/t/aggregate-fields-to-an-array-fields/291024/4 "2021-12-07T18:10:28Z")

</div>

If you are currently doing

```
map["host"] << event.get("host")

```

you could change that to

```
h = event.get("host")
if h
   map["host"] << h
end
```

---

<div class="post-metadata">

**Author:** ![RichYaNa](https://avatars.discourse-cdn.com/v4/letter/r/ba8739/32.png) [@RichYaNa](https://discuss.elastic.co/u/RichYaNa)\
**Post date:** [December 8, 2021, 2:08am UTC](https://discuss.elastic.co/t/aggregate-fields-to-an-array-fields/291024/5 "2021-12-08T02:08:13Z")

</div>

Thanks for the answers.

I've tried that and it works for the empty field, but not for the multiple values.

still getting output like this,

```auto
{
  "taskid" => "123",
  "host" => ["ubuntu", "centos", "ubuntu"],
  "ip" => "1.2.3.4"
}

```

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [December 8, 2021, 2:12am UTC](https://discuss.elastic.co/t/aggregate-fields-to-an-array-fields/291024/6 "2021-12-08T02:12:34Z")

</div>

I do not understand what you mean about multiple values. What do you want the event to look like?

---

<div class="post-metadata">

**Author:** ![RichYaNa](https://avatars.discourse-cdn.com/v4/letter/r/ba8739/32.png) [@RichYaNa](https://discuss.elastic.co/u/RichYaNa)\
**Post date:** [December 8, 2021, 2:23am UTC](https://discuss.elastic.co/t/aggregate-fields-to-an-array-fields/291024/7 "2021-12-08T02:23:34Z")

</div>

oh wait,  
i mean duplicate value? is that right?

what i want is, the host field does not have any duplicate value.  
the output should be like this,

```auto
"host" => ["ubuntu", "centos"]

```

not like this,

> [@RichYaNa](#):
>
> `"host" => ["ubuntu", "centos", "ubuntu"],`

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [December 8, 2021, 3:18am UTC](https://discuss.elastic.co/t/aggregate-fields-to-an-array-fields/291024/8 "2021-12-08T03:18:45Z")

</div>

Yes, you can remove duplicates. How to do that will depend on what your existing aggregate looks like. Either not add them or remove them at the end.

You might need

```
if h && ! maps["host"].include? h
    map["host"] << h

```

If your aggregate is currently doing `event.set("host", maps["host"])` then you could change it to

```
event.set("host", maps["host"].uniq)
```

---

<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:** [January 5, 2022, 3:19am UTC](https://discuss.elastic.co/t/aggregate-fields-to-an-array-fields/291024/9 "2022-01-05T03:19:20Z")

</div>

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