# Aggregation through Logstash

**URL:** <https://discuss.elastic.co/t/aggregation-through-logstash/133129>\
**Category:** Logstash\
**Created:** [May 24, 2018, 11:20am UTC](https://discuss.elastic.co/t/aggregation-through-logstash/133129 "2018-05-24T11:20:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alok](https://avatars.discourse-cdn.com/v4/letter/a/a8b319/32.png) [@Alok](https://discuss.elastic.co/u/Alok)\
**Post date:** [May 24, 2018, 11:20am UTC](https://discuss.elastic.co/t/aggregation-through-logstash/133129/1 "2018-05-24T11:20:45Z")

</div>

Dear Experts  
I am using below aggregation to add child table entries to given parent ids -

aggregate {  
task\_id =\> "%{id}"  
code =\> "  
map['id'] = event.get('id')  
map['sizes'] ||= []  
map['sizes'] \<\< {'width' =\> event.get('width'),  
'height' =\> event.get('height'),  
'weight' =\> event.get('weight')}

```
     event.cancel()
   "
   push_previous_map_as_event => true
   timeout => 3
 }

```

This is working fine with output block as under :

output {

```
elasticsearch {
 hosts => "http://localhost:9200"
 index => "products_details"
 document_type => "products_details"
 document_id => "%{id}"
 #action => "update"
 doc_as_upsert => true
 script_type => "inline"
}

```

Problem started when I added the scheduler part in the JDBC input block -

use\_column\_value =\> true  
tracking\_column =\> "size\_id"  
statement\_filepath =\> "C:/Elastic/Schedule\_last\_value\_size/sizes.sql"  
schedule =\> "\*/5 \* \* \* \*"

If there is any new entry in child table for a given parent id, all the previous records are getting replaced with this new entry.

Any pointers where I might be going wrong ? Any work around?

Alok

---

<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:** [May 24, 2018, 12:11pm UTC](https://discuss.elastic.co/t/aggregation-through-logstash/133129/2 "2018-05-24T12:11:47Z")

</div>

> [@Alok](#):
>
> document\_id =\> "%{id}"  
> doc\_as\_upsert =\> true

That tells elasticsearch to create, if necessary, or else overwrite the document with that id. Are you trying to append fields to an existing document in elasticsearch?

---

<div class="post-metadata">

**Author:** ![Alok](https://avatars.discourse-cdn.com/v4/letter/a/a8b319/32.png) [@Alok](https://discuss.elastic.co/u/Alok)\
**Post date:** [May 25, 2018, 4:42am UTC](https://discuss.elastic.co/t/aggregation-through-logstash/133129/3 "2018-05-25T04:42:08Z")

</div>

You are right.

Let me explain . lets say I have following document -

PO number : 101  
Deliver date : Some Date  
Total : Some amount  
items :  
i001 watch  
i002 suitcase

Now I have another item- i003 - boots added into the same PO at my backend table and logstash fetches this new record. As you mentioned correctly , It will replace i001 and i002 and add i003 -boots

But I would like to have all three items together under same PO number.  
How do we achieve this ?

Alok

---

<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:** [May 25, 2018, 3:23pm UTC](https://discuss.elastic.co/t/aggregation-through-logstash/133129/4 "2018-05-25T15:23:13Z")

</div>

> [@Alok](#):
>
> I would like to have all three items together under same PO number.  
> How do we achieve this ?

I would have suggested an approach like [this](https://discuss.elastic.co/t/elasticsearch-filter-giving-typeerror-no-implicit-conversion-of-nil-into-string/123791), but that does not work 🙂

Instead of writing to elasticsearch using logstash, you could write the output to a file in a format suitable for using the [bulk](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html) and [update](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html) apis, then load the file into elasticsearch using curl.

---

<div class="post-metadata">

**Author:** ![Alok](https://avatars.discourse-cdn.com/v4/letter/a/a8b319/32.png) [@Alok](https://discuss.elastic.co/u/Alok)\
**Post date:** [May 25, 2018, 4:11pm UTC](https://discuss.elastic.co/t/aggregation-through-logstash/133129/5 "2018-05-25T16:11:16Z")

</div>

So this gets uglier ☹  
I have just started with Elastic. Thought logstash will help me schedule delta updates too.

Thanks for your quick reply.

---

<div class="post-metadata">

**Author:** ![Alok](https://avatars.discourse-cdn.com/v4/letter/a/a8b319/32.png) [@Alok](https://discuss.elastic.co/u/Alok)\
**Post date:** [May 28, 2018, 11:55am UTC](https://discuss.elastic.co/t/aggregation-through-logstash/133129/6 "2018-05-28T11:55:27Z")

</div>

As a work around, I have modified the select statement and it is now fetching all the related item data records for the given PO and doing aggregation on the whole data.  
Previously I was fetching only the new child record.

---

<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:** [June 25, 2018, 11:55am UTC](https://discuss.elastic.co/t/aggregation-through-logstash/133129/7 "2018-06-25T11:55:28Z")

</div>

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