# One-to-many relationships with recursive relationships

**URL:** https://discuss.elastic.co/t/one-to-many-relationships-with-recursive-relationships/5719
**Category:** Elasticsearch
**Created:** [October 28, 2011, 9:34pm UTC](https://discuss.elastic.co/t/one-to-many-relationships-with-recursive-relationships/5719 "2011-10-28T21:34:23Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Runar\_Myklebust](https://avatars.discourse-cdn.com/v4/letter/r/a6a055/32.png) [@Runar\_Myklebust](https://discuss.elastic.co/u/Runar_Myklebust)
#### Post date: [October 28, 2011, 9:34pm UTC](https://discuss.elastic.co/t/one-to-many-relationships-with-recursive-relationships/5719/1 "2011-10-28T21:34:23Z")

</div>

Hi.

Im doing a pilot for introducing ES as the search-engine in our  
product.

The main entity to index is "content", which is pretty straight  
forward to index.  
The problems arises when having to deal with content-placement  
(categories, menu-items), security and other meta-data.

Lets take categories as an example. A content is resident in one  
category only. Categories are arranged as trees.

A typical query could be like: "Fetch all content in a category X and  
include all sub-categories"

Lets say the category-tree could be e.g 10 levels deep, so we  
potentionally will get a large number of categories to consider.

Things to consider:

1. Response-time
2. Update data, e.g move a category

Approach 1: Include category-id in content index, resolve all  
categories before query and apply them as Filter.  
Pros: Easy to maintain  
Cons: Potentially very large filters, lets say like 1000 category-keys  
-\> how bad is that?

Approach 2: Store category path in content, apply query with main  
category and number of levels to include  
Pros: Simple query  
Cons: Hard to maintain e.g when moving a category

Approach 3: I then though about normalizing the data, storing content  
with category-id, and the store category with path. But I not quite  
shure how to map and query this? My initial thought was to use a child-  
parent approach, with an "inner query" finding matching categories and  
joining them with all matching content.category-ids, but Im not even  
sure this is possible.

Any thoughts on how to approach this? Or any other approaches?

---

<div class="post-metadata">

### Author: ![Sky\_Stebnicki1](https://avatars.discourse-cdn.com/v4/letter/s/ba8739/32.png) [@Sky\_Stebnicki1](https://discuss.elastic.co/u/Sky_Stebnicki1)
#### Post date: [January 24, 2013, 2:30pm UTC](https://discuss.elastic.co/t/one-to-many-relationships-with-recursive-relationships/5719/2 "2013-01-24T14:30:23Z")

</div>

Hi Runar,

I know this is sort of old, but I didn't see a reply. Were you able to  
determine what would be the best solution for this problem? We have a  
similar issue where we are trying to query all sub-folders of the current  
folder (in a file system index).

Thanks,

Sky

On Friday, October 28, 2011 2:34:23 PM UTC-7, Runar Myklebust wrote:

> Hi.
> 
> Im doing a pilot for introducing ES as the search-engine in our  
> product.
> 
> The main entity to index is "content", which is pretty straight  
> forward to index.  
> The problems arises when having to deal with content-placement  
> (categories, menu-items), security and other meta-data.
> 
> Lets take categories as an example. A content is resident in one  
> category only. Categories are arranged as trees.
> 
> A typical query could be like: "Fetch all content in a category X and  
> include all sub-categories"
> 
> Lets say the category-tree could be e.g 10 levels deep, so we  
> potentionally will get a large number of categories to consider.
> 
> Things to consider:
> 
> 1. Response-time
> 2. Update data, e.g move a category
> 
> Approach 1: Include category-id in content index, resolve all  
> categories before query and apply them as Filter.  
> Pros: Easy to maintain  
> Cons: Potentially very large filters, lets say like 1000 category-keys  
> -\> how bad is that?
> 
> Approach 2: Store category path in content, apply query with main  
> category and number of levels to include  
> Pros: Simple query  
> Cons: Hard to maintain e.g when moving a category
> 
> Approach 3: I then though about normalizing the data, storing content  
> with category-id, and the store category with path. But I not quite  
> shure how to map and query this? My initial thought was to use a child-  
> parent approach, with an "inner query" finding matching categories and  
> joining them with all matching content.category-ids, but Im not even  
> sure this is possible.
> 
> Any thoughts on how to approach this? Or any other approaches?

--

---

<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, 2:54am UTC](https://discuss.elastic.co/t/one-to-many-relationships-with-recursive-relationships/5719/3 "2017-07-06T02:54:44Z")

</div>


