# Hierarchical relationships..?

**URL:** https://discuss.elastic.co/t/hierarchical-relationships/302
**Category:** Elasticsearch
**Created:** [May 6, 2015, 3:21pm UTC](https://discuss.elastic.co/t/hierarchical-relationships/302 "2015-05-06T15:21:50Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![squalli2008](https://avatars.discourse-cdn.com/v4/letter/s/aeb1de/32.png) [@squalli2008](https://discuss.elastic.co/u/squalli2008)
#### Post date: [May 6, 2015, 3:21pm UTC](https://discuss.elastic.co/t/hierarchical-relationships/302/1 "2015-05-06T15:21:50Z")

</div>

Hi All, Does anyone have any experience in storing deep, hierarchical relationships in ES?

We are trying to model something along the lines of:

Food -\> Fruit -\> Green -\> Apple Doc  
Food -\> Fruit -\> Round -\> Apple Doc  
Food -\> Healthy -\> Apple Doc  
Food -\> Apple Doc

I want to be able to find the Apple (by some document attributes) but also know that it lives in 2 places (with the hierarchy returned) - ideally with references /snippets to the ancestor documents?

Each of the ancestors in the list are documents in their own rights and I would like to be able to search within any level and find Apple...

Its also worth noting that there are millions of items at each level... so flattening IDs etc is a last resort.

Cheers

---

<div class="post-metadata">

### Author: ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)
#### Post date: [May 6, 2015, 4:20pm UTC](https://discuss.elastic.co/t/hierarchical-relationships/302/2 "2015-05-06T16:20:02Z")

</div>

You can index the categories, like

apple doc -\> category : ["Food/Fruit/Green", "Food/Fruit/Round", "Food/Healthy", "Food"]

---

<div class="post-metadata">

### Author: ![squalli2008](https://avatars.discourse-cdn.com/v4/letter/s/aeb1de/32.png) [@squalli2008](https://discuss.elastic.co/u/squalli2008)
#### Post date: [May 6, 2015, 5:34pm UTC](https://discuss.elastic.co/t/hierarchical-relationships/302/3 "2015-05-06T17:34:01Z")

</div>

Thanks for the suggestion.

We'd thought about using a path tokenizer...

A little more information... 😄

1. It doesn't give me a reference to the "parent" documents eg. Fruit..

2. It presents an ingest / management issue - that if the hierarchy changes (which it will and quite regularly), we may have to update millions of documents.

3. Categories are dynamic in that we would like to link them to documents that meet a set of query criteria / list of IDs.

We could manage 2 and 3 with some application logic and Percolator (I think)..?

---

<div class="post-metadata">

### Author: ![mattweber](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mattweber/32/44940_2.png) [@mattweber](https://discuss.elastic.co/u/mattweber)
#### Post date: [May 6, 2015, 7:07pm UTC](https://discuss.elastic.co/t/hierarchical-relationships/302/4 "2015-05-06T19:07:13Z")

</div>

Why don't you use a separate field for each level when you can use aggregations to generate the hierarchy.

level1 = ["Food"]  
level2 = ["Food/Fruit", "Food/Healthy"]  
level3 = ["Food/Fruit/Green", "Food/Fruit/Round"]

You still have the update issue, but you can easily link to parent documents by performing the same query and using a filter ie. level2 = "Food/Fruit" for all fruit.

---

<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 6, 2017, 12:15am UTC](https://discuss.elastic.co/t/hierarchical-relationships/302/5 "2017-07-06T00:15:19Z")

</div>


