# Aggregating an index with parent-child runs forever

**URL:** https://discuss.elastic.co/t/aggregating-an-index-with-parent-child-runs-forever/313624
**Category:** Elasticsearch
**Created:** [September 4, 2022, 1:31pm UTC](https://discuss.elastic.co/t/aggregating-an-index-with-parent-child-runs-forever/313624 "2022-09-04T13:31:41Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![t0mer](https://avatars.discourse-cdn.com/v4/letter/t/fbc32d/32.png) [@t0mer](https://discuss.elastic.co/u/t0mer)
#### Post date: [September 4, 2022, 1:31pm UTC](https://discuss.elastic.co/t/aggregating-an-index-with-parent-child-runs-forever/313624/1 "2022-09-04T13:31:41Z")

</div>

Hi,  
I've recently decided to make an attempt to reindex an existing denormalized index to a new index with parent-chid relation.  
I've around 14M parent docs, each parent has up to 400 children. (total of around 270M docs)  
This is a simplified version of my mapping -\>

```auto
{
  "mappings": {
    "_doc": {
      "properties": {
        "product_type": {
          "type": "keyword"
        },
        "relation_type": {
          "type": "join",
          "eager_global_ordinals": true,
          "relations": {
            "product_data": [
              "kpi",
              "customer"
            ]
          }
        },
        "rootdomain": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "rootdomain_sku": {
          "type": "keyword",
          "eager_global_ordinals": true
        },
        "sales_1d": {
          "type": "float"
        },
        "sku": {
          "type": "keyword",
          "eager_global_ordinals": true
        },
        "timestamp": {
          "type": "date",
          "format": "strict_date_optional_time_nanos"
        }
      }
    }
  }
}

```

As you can see I've used eager\_global\_ordinals for the join relation to speed up search performance  
(per my understanding this causes some of the join relation computation in global ordinals to be done in indexing time and not while querying).  
This migration process helped me reduce my index size from around 500GB to just 40GB.  
It has a huge benefit for my use case since I update a lot of data daily.

My current testing environment is using a single node, and the index has only 1 primary shard.  
Trying to run the following aggregation, seems like it runs forever -

```auto
{
  "aggs": {
    "skus_sales": {
      "aggs": {
        "sales1": {
          "children": {
            "type": "kpi"
          },
          "aggs": {
            "sales2": {
              "filter": {
                "range": {
                  "timestamp": {
                    "format": "basic_date_time_no_millis",
                    "gte": "20220601T000000Z",
                    "lte": "20220605T235959Z"
                  }
                }
              },
              "aggs": {
                "sales3": {
                  "sum": {
                    "field": "sales_1d"
                  }
                }
              }
            }
          }
        }
      },
      "terms": {
        "field": "rootdomain_sku",
        "size": 10
      }
    }
  },
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "rootdomain.keyword": "some_domain"
          }
        },
        {
          "term": {
            "product_type": "Rugs"
          }
        }
      ]
    }
  }
}

```

I understand the cons of parent-child relations, but it seems like I'm doing something wrong.  
I would expect to get some result, even after 15 minutes, but it seems to run forever.

I would love to get some help here,  
Thanks.

---

<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: [September 4, 2022, 1:42pm UTC](https://discuss.elastic.co/t/aggregating-an-index-with-parent-child-runs-forever/313624/2 "2022-09-04T13:42:45Z")

</div>

Have a look at the [hot threads API](https://www.elastic.co/guide/en/elasticsearch/reference/8.4/cluster-nodes-hot-threads.html), which might give some clues about what is going on. Be aware that each query executes in a single thread against each shard, so by increasing the number of primary shards you may do more work and processing in parallel. It is possible that this is your bottleneck. I would split the index into e.g. 4 primary shards and see how that affects query latency.

---

<div class="post-metadata">

### Author: ![t0mer](https://avatars.discourse-cdn.com/v4/letter/t/fbc32d/32.png) [@t0mer](https://discuss.elastic.co/u/t0mer)
#### Post date: [September 19, 2022, 4:34pm UTC](https://discuss.elastic.co/t/aggregating-an-index-with-parent-child-runs-forever/313624/3 "2022-09-19T16:34:01Z")

</div>

Thanks, that might be the issue, seems like response time is dramatically high, so i'm trying to migrate this index to use nested docs instead of parent-child relation.

---

<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: [September 19, 2022, 4:45pm UTC](https://discuss.elastic.co/t/aggregating-an-index-with-parent-child-runs-forever/313624/4 "2022-09-19T16:45:52Z")

</div>

Often the best way is to denormalise and fully flatten the structure when working with Elasticsearch so I would recommend looking into that as an option. Nested documents also come with some performance implications, as updates or changes can get very expensive.

---

<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: [October 17, 2022, 4:46pm UTC](https://discuss.elastic.co/t/aggregating-an-index-with-parent-child-runs-forever/313624/5 "2022-10-17T16:46:10Z")

</div>

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