# Good mapping for recursively nested documents

**URL:** <https://discuss.elastic.co/t/good-mapping-for-recursively-nested-documents/184043>\
**Category:** Elasticsearch\
**Created:** [June 3, 2019, 7:28pm UTC](https://discuss.elastic.co/t/good-mapping-for-recursively-nested-documents/184043 "2019-06-03T19:28:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![flow](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@flow](https://discuss.elastic.co/u/flow)\
**Post date:** [June 3, 2019, 7:28pm UTC](https://discuss.elastic.co/t/good-mapping-for-recursively-nested-documents/184043/1 "2019-06-03T19:28:04Z")

</div>

Hello all,

I need advice to find a good design for my documents. The data I want to ingest comes from a big, recursively nested XML file (I could translate it into JSON fwiw) which looks similar to this:

```
<Data>
<Element type="a" value="abc">
    <Element type="b" value="foo">
        <Element type="c" value="bar"></Element>
    </Element>
</Element>
<Element type="a" value="def">
</Element>
</Data>

```

- Apart from the global Data root tag, there are only Element tags.
- Apart from a few select Elements, every Element has exactly one parent. (The exceptions are the Elements with type="a")
- The individual Elements can be arbitrarily deeply nested (but bounded in all cases)
- The nesting depth is usually between 5 and 20, there is a minimum at 4.
- There are no cycles.

I want to do aggregations and searches on the individual Elements, so every Element tag should become a distinct document. However, there are two types of queries that are neccesary:

- Find (recursively) all parents of a given Element
- Search and filter on all (recursive) children of a given Element

From what I understand, join is not possible here, because a single Element can be parent and child at the same time. Maybe nested is the correct way to go, but I'd like feedback before I reimplement what I have so far. (which is: Add a primary\_key and parent\_key field to all Elements, but this does not support recursive search).

---

<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 1, 2019, 7:28pm UTC](https://discuss.elastic.co/t/good-mapping-for-recursively-nested-documents/184043/2 "2019-07-01T19:28:09Z")

</div>

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