# Logstash Aggregate multi levels

**URL:** <https://discuss.elastic.co/t/logstash-aggregate-multi-levels/108169>\
**Category:** Logstash\
**Created:** [November 17, 2017, 4:55pm UTC](https://discuss.elastic.co/t/logstash-aggregate-multi-levels/108169 "2017-11-17T16:55:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ythalorossy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ythalorossy/32/24392_2.png) [@ythalorossy](https://discuss.elastic.co/u/ythalorossy)\
**Post date:** [November 17, 2017, 4:55pm UTC](https://discuss.elastic.co/t/logstash-aggregate-multi-levels/108169/1 "2017-11-17T16:55:13Z")

</div>

Hi all,

So, I am trying do a simple aggregation using _plugins-filters-aggregate_. I extract data from relation database using _plugin-jdbc_ and the result is:

```
P_ID	P_DESC PROD_I	P_ID	PROD_DESC PROD_ID SPROD_DESC COUNTRY
700 ELASTIC 1 700 LOAN DESCRIPTION	1 BLOAN Mexico
700 ELASTIC 1 700 LOAN DESCRIPTION	2 CLOAN Chile
700 ELASTIC 2 700 LOAN 2 1 BLOAN Mexico
700 ELASTIC 2 700 LOAN 2 2 CLOAN Chile
|--	project --------|----------- product --------------|----sub product -------|-country -|

```

Where a **project** has **countries** and products and **products** has many **sub\_products**.

I would like this result:

```
{
	"PROD_I"	: 700,
	"PROD_DESC"	: "ELASTIC",
	"PRODUCTS"	: [
		{
			PROD_ID : 1,
			PROD_DESC : "LOAN DESCRIPTION",
			"SUB_PRODUCTS"	: [
				{
					"SPROD_DESC"	: "BLOAN"
				},
				{
					"SPROD_DESC"	: "CLOAN"
				}
			]
		},
		{
			PROD_ID :	2,
			PROD_DESC	:	"LOAN 2"
		}
	],
	"COUNTRIES"	: [
		{ "NAME" : "Chile"}, 
		{ "NAME":	"Mexico"}
	]
}

```

I have tried this:

```
filter {
  aggregate {
    task_id => "%{p_id}"
    code => "
      map['PROD_I'] = event.get('PROD_I')
      map['PROD_DESC'] = event.get('PROD_DESC')

	  map['PRODUCTS'] = []
      map['PRODUCTS'] << {
        'PROD_DESC' => event.get('PROD_DESC')
      }
      event.cancel()
    "
    push_previous_map_as_event => true
    timeout => 3
	}
}	

```

How can I improve my aggregation to be the correct project --\> products --\> sub products?

Best regards,  
Ythalo Rossy

---

<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 15, 2017, 4:55pm UTC](https://discuss.elastic.co/t/logstash-aggregate-multi-levels/108169/2 "2017-12-15T16:55:31Z")

</div>

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