# Questions about mapping a tree structure

**URL:** <https://discuss.elastic.co/t/questions-about-mapping-a-tree-structure/13463>\
**Category:** Elasticsearch\
**Created:** [September 4, 2013, 7:44pm UTC](https://discuss.elastic.co/t/questions-about-mapping-a-tree-structure/13463 "2013-09-04T19:44:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matt\_Schoen](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@Matt\_Schoen](https://discuss.elastic.co/u/Matt_Schoen)\
**Post date:** [September 4, 2013, 7:44pm UTC](https://discuss.elastic.co/t/questions-about-mapping-a-tree-structure/13463/1 "2013-09-04T19:44:46Z")

</div>

Hi there. I have a document type that contains a lot of self-similar data. Here's a little sample to give you an idea:

Root:  
Motif:  
Motif Properties  
Array of sectionInfo  
Children(motif):  
Motif Properties  
Array of sectionInfo  
Other motifs  
Other root properties

So... I hope that makes sense. I can include some sample documents if needed, but they're pretty big and complicated. The two mappings that will come up often are motif and sectionInfo, which are classes in my program.

My question is whether the motif object should be a nested type. The documentation said something about nested objects being stored as separate documents, but since the structure doesn't mirror the structure of the whole document, I'm not sure that's what I want.

My other thought was to just make a variable in my mapping setup script to represent those structures and reuse wherever needed. This still doesn't solve the problem if the motif having children who have children, etc.

Thanks.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [September 4, 2013, 9:30pm UTC](https://discuss.elastic.co/t/questions-about-mapping-a-tree-structure/13463/2 "2013-09-04T21:30:03Z")

</div>

Hi,

It really depends on how recursive your tree structure is. The underlying  
storage of Elasticsearch is flat and doesn't allow for storing deeply  
recursive tree structures. There are two ways to work around this flat  
structure (which brings lots of benefits otherwise): using nested  
documents, which consist of indexing a parent document and its children  
sequentially on disk so that scoring from/to parents/children or faceting  
can be done efficiencly, and parent/child relationships where the mapping  
between parents and children is stored in memory on top of the index  
without any constraint on the on-disk storage. But in both cases, this only  
allows for storing 1-\>n relationships and doesn't allow for recursivity.

So storing a tree structure in Elasticsearch might be challenging. But it  
also depends on how you plan to query your data. For example, if you don't  
need scores to propagate from children to parents or vice-versa, you could  
just index all motifs independently.

On Wed, Sep 4, 2013 at 9:44 PM, Matt Schoen [mtschoen@gmail.com](mailto:mtschoen@gmail.com) wrote:

> Hi there. I have a document type that contains a lot of self-similar data.  
> Here's a little sample to give you an idea:
> 
> Root:  
> Motif:  
> Motif Properties  
> Array of sectionInfo  
> Children(motif):  
> Motif Properties  
> Array of sectionInfo  
> Other motifs  
> Other root properties
> 
> So... I hope that makes sense. I can include some sample documents if  
> needed, but they're pretty big and complicated. The two mappings that will  
> come up often are motif and sectionInfo, which are classes in my program.
> 
> My question is whether the motif object should be a nested type. The  
> documentation said something about nested objects being stored as separate  
> documents, but since the structure doesn't mirror the structure of the  
> whole document, I'm not sure that's what I want.
> 
> My other thought was to just make a variable in my mapping setup script to  
> represent those structures and reuse wherever needed. This still doesn't  
> solve the problem if the motif having children who have children, etc.
> 
> Thanks.

--  
Adrien Grand

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Matt\_Schoen](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@Matt\_Schoen](https://discuss.elastic.co/u/Matt_Schoen)\
**Post date:** [September 4, 2013, 9:41pm UTC](https://discuss.elastic.co/t/questions-about-mapping-a-tree-structure/13463/3 "2013-09-04T21:41:30Z")

</div>

Hm. I haven't heard of faceting. I'll look into that. to be honest I'll probably just be calling these documents up based on an I'd stored in the root, so below that level, the data may as well be a string. Is there a way to just say "don't worry about the structre here"?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [September 5, 2013, 7:07am UTC](https://discuss.elastic.co/t/questions-about-mapping-a-tree-structure/13463/4 "2013-09-05T07:07:41Z")

</div>

Hi,

On Wed, Sep 4, 2013 at 11:41 PM, Matt Schoen [mtschoen@gmail.com](mailto:mtschoen@gmail.com) wrote:

> Hm. I haven't heard of faceting. I'll look into that. to be honest I'll  
> probably just be calling these documents up based on an I'd stored in the  
> root, so below that level, the data may as well be a string. Is there a way  
> to just say "don't worry about the structre here"?

This is what happens by default: documents are flattened. If you look at  
the first example on [1], queries to Elasticsearch won't be able to know  
whether count=4 is associated with blue or green. To Elasticsearch, it will  
be as if there was a field called "name" which has two values "blue" and  
"green" and a field called "count" which has two values "4" and "6".

[1] [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/nested-type/)

--  
Adrien Grand

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:18am UTC](https://discuss.elastic.co/t/questions-about-mapping-a-tree-structure/13463/5 "2017-07-06T02:18:06Z")

</div>


