# Ingest SQL relationships as nested documents

**URL:** https://discuss.elastic.co/t/ingest-sql-relationships-as-nested-documents/131645
**Category:** Logstash
**Created:** [May 14, 2018, 12:28am UTC](https://discuss.elastic.co/t/ingest-sql-relationships-as-nested-documents/131645 "2018-05-14T00:28:32Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![mwmouawad](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@mwmouawad](https://discuss.elastic.co/u/mwmouawad)
#### Post date: [May 14, 2018, 12:28am UTC](https://discuss.elastic.co/t/ingest-sql-relationships-as-nested-documents/131645/1 "2018-05-14T00:28:32Z")

</div>

Hi everyone, I’m trying to figure out a way to index my Postgres data into elasticsearch. I have a user table, and a purchase table with a user\_id column linking to a user. I want to have a elasticsearch document for each user and within a nested document with each purchase that this user has made.

I’m planning to use the user\_id as the elasticsearch document id reference, however:  
1- I can’t figure a way to create nested objects with logstash input.  
2- Can’t find a way to add multiple purchases fields to a same document. (It’s being overwritten)

---

<div class="post-metadata">

### Author: ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)
#### Post date: [May 14, 2018, 4:55am UTC](https://discuss.elastic.co/t/ingest-sql-relationships-as-nested-documents/131645/2 "2018-05-14T04:55:23Z")

</div>

Have you considered denormalising your model and store each order as a separate document with the relevant customer information added to each document?

---

<div class="post-metadata">

### Author: ![mwmouawad](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@mwmouawad](https://discuss.elastic.co/u/mwmouawad)
#### Post date: [May 17, 2018, 2:37pm UTC](https://discuss.elastic.co/t/ingest-sql-relationships-as-nested-documents/131645/3 "2018-05-17T14:37:48Z")

</div>

Hi i will probably try that. Still I wonder if there isn't another way of doing this.

Thank you very much

---

<div class="post-metadata">

### Author: ![Johnnycc1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/johnnycc1/32/26069_2.png) [@Johnnycc1](https://discuss.elastic.co/u/Johnnycc1)
#### Post date: [May 18, 2018, 11:26pm UTC](https://discuss.elastic.co/t/ingest-sql-relationships-as-nested-documents/131645/4 "2018-05-18T23:26:38Z")

</div>

Hey mwmouawad,

I have not done it myself however I think you can get postgres to create the nested json for you, using row\_to\_json, you might want to hide your query behind a view so logstash just queries one view and gets the nested json back. If this does work please let me know.

Alternatively you can use the jdbc streaming filter. Your logstash input query gets all the users, then using the jdbc filter it queries postgres for all transactions for that user and adds it to the event.

---

<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 15, 2018, 11:26pm UTC](https://discuss.elastic.co/t/ingest-sql-relationships-as-nested-documents/131645/5 "2018-06-15T23:26:44Z")

</div>

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