# Data modeling: nested vs parent/child vs two indices

**URL:** https://discuss.elastic.co/t/data-modeling-nested-vs-parent-child-vs-two-indices/60792
**Category:** Elasticsearch
**Created:** [September 17, 2016, 7:10pm UTC](https://discuss.elastic.co/t/data-modeling-nested-vs-parent-child-vs-two-indices/60792 "2016-09-17T19:10:29Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![dangnhdev](https://avatars.discourse-cdn.com/v4/letter/d/90ced4/32.png) [@dangnhdev](https://discuss.elastic.co/u/dangnhdev)
#### Post date: [September 17, 2016, 7:10pm UTC](https://discuss.elastic.co/t/data-modeling-nested-vs-parent-child-vs-two-indices/60792/1 "2016-09-17T19:10:29Z")

</div>

Hi all, I'm facing a problem with "order - product" data  
Denormalized data may look like:  
A product:

```javascript
{
    "_id" : productId,
    "name" : name,
    "price" : price,
    "desc" : description
}

```

An order:

```javascript
{
    "_id" : orderId,
    "user" : userInfo,
    "products" : [
        {
            "_id" : productId1,
            "name" : name1,
            "price" : price1
        },
        {
            "_id" : productId2,
            "name" : name2,
            "price" : price2
        },
        {
            "_id" : productId3,
            "name" : name3,
            "price" : price3
        }
    ]
}

```

- With this design we're having two problems:

1. In many case products in the order must update frequently because we can't receive full order once at a time (business domain)

- With the parent-child model, you know, I'm aware of **join**. I haven't explicitly tested it yet, how about grouping order-product by order or get full order-product in one document ?

- We can create two indices for this: one for product and one for order (with full nested product) and additional data for aggregation too. But this require much of work and duplicate data too!

Can you give me some advice? Thank you very much!

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [September 19, 2016, 8:02am UTC](https://discuss.elastic.co/t/data-modeling-nested-vs-parent-child-vs-two-indices/60792/2 "2016-09-19T08:02:21Z")

</div>

> [@dangnhdev](#):
>
> it is very slow (about 85% slower in our benchmark) compared to the similar query in the product index only. For example: Show the most popular product or show all product group by total revenue

I'm not sure the example comparison makes sense either as a raw benchmark or as alternative methods of deriving the same stat. "Total revenue" and "most popular" are derived from _sales_ data with many records per unique product whereas the product catalogue should only have one record per product.

> [@dangnhdev](#):
>
> In many case products in the order must update frequently because we can't receive full order once at a time

The ideal solution really depends on what questions you want to ask of the data.

If you aren't interested in answering "order-centric" questions e.g. "what products are commonly ordered with product X?" then your documents could just be separate line-item docs with a common "orderID" if the whole order arrives in stages. That would still allow you to answer product-centric questions like "most popular/highest revenue product".

---

<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: [July 5, 2017, 10:19pm UTC](https://discuss.elastic.co/t/data-modeling-nested-vs-parent-child-vs-two-indices/60792/3 "2017-07-05T22:19:22Z")

</div>


