# NEST and parent/child relation

**URL:** <https://discuss.elastic.co/t/nest-and-parent-child-relation/129450>\
**Category:** Elasticsearch\
**Created:** [April 25, 2018, 10:04am UTC](https://discuss.elastic.co/t/nest-and-parent-child-relation/129450 "2018-04-25T10:04:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Waylander](https://avatars.discourse-cdn.com/v4/letter/w/f4b2a3/32.png) [@Waylander](https://discuss.elastic.co/u/Waylander)\
**Post date:** [April 25, 2018, 10:04am UTC](https://discuss.elastic.co/t/nest-and-parent-child-relation/129450/1 "2018-04-25T10:04:24Z")

</div>

Hi!  
I try to use parent/child relation following by this [document](https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/parent-child-relationships.html).

I have the same classes for test: MyDocument, MyParent, MyChild.  
Index creation:

```
            var createIndexResponse = ElasticClient.CreateIndex("a_test_index", c => c
            .Mappings(ms => ms
                .Map<MyDocument>(m => m
                    .RoutingField(r => r.Required()) 
                    .Properties<MyParent>(p => p
                        .Scalar(x => x.Id)
                        .Text(t => t.Name(x => x.ParentProperty))
                    )
                    .Properties<MyChild>(p => p
                        .Scalar(x => x.Id)
                        .Text(x => x.Name(n => n.ChildProperty))
                    )
                    .Properties(props => props
                        .Join(j => j 
                            .Name(p => p.MyJoinField)
                            .Relations(r => r
                                .Join<MyParent, MyChild>()
                            )
                        )
                    )
                )
            )
        );            

```

Indexing data:

```
            var parent1 = new MyParent
        {
            Id = 1,
            ParentProperty = "Parrent1",
            MyJoinField = JoinField.Root<MyParent>()
        };
        var parent2 = new MyParent
        {
            Id = 2,
            ParentProperty = "Parrent2",
            MyJoinField = JoinField.Root<MyParent>()
        };
        ElasticClient.Index<MyDocument>(parent1, x => x.Index("a_test_index").Refresh(Refresh.True));
        ElasticClient.Index<MyDocument>(parent2, x => x.Index("a_test_index").Refresh(Refresh.True));

            var child1 = new MyChild
        {
            Id = 1,
            ChildProperty = "Child1",
            MyJoinField = JoinField.Link<MyChild>(1)
        };
        var child2 = new MyChild
        {
            Id = 2,
            ChildProperty = "Child2",
            MyJoinField = JoinField.Link<MyChild>(1)
        };
        var child3 = new MyChild
        {
            Id = 3,
            ChildProperty = "Child3",
            MyJoinField = JoinField.Link<MyChild>(2)
        };

        ElasticClient.Index<MyDocument>(child1, x => x.Index("a_test_index").Refresh(Refresh.True));
        ElasticClient.Index<MyDocument>(child2, x => x.Index("a_test_index").Refresh(Refresh.True));
        ElasticClient.Index<MyDocument>(child3, x => x.Index("a_test_index").Refresh(Refresh.True));

```

1. Why indexed only last parent document?

2. How make bulk indexing? When I try like this nothing happens (but I use such indexing without parent/child relations and it works):

---

<div class="post-metadata">

**Author:** ![Waylander](https://avatars.discourse-cdn.com/v4/letter/w/f4b2a3/32.png) [@Waylander](https://discuss.elastic.co/u/Waylander)\
**Post date:** [April 25, 2018, 2:34pm UTC](https://discuss.elastic.co/t/nest-and-parent-child-relation/129450/2 "2018-04-25T14:34:15Z")

</div>

Question 2 is solved:

```
        var list = new List<MyChild> {child1, child2, child3};
        IndexData<MyDocument>(list);

```

But first question is still opened: indexed only last MyParent document((  
If I indexing only parent documents (without childs), then all two docs are indexed. But If with childs, then only last one

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [May 1, 2018, 12:54pm UTC](https://discuss.elastic.co/t/nest-and-parent-child-relation/129450/3 "2018-05-01T12:54:12Z")

</div>

> [@Waylander](#):
>
> indexed only last MyParent document

The Ids of the Child documents (1,2,3) repeat the Ids of the Parent documents (1,2). Sounds like you might be overwriting Parent with Id 2 with Child with Id 2? The Ids for all Parent and Child documents must be unique.

---

<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:** [May 29, 2018, 12:54pm UTC](https://discuss.elastic.co/t/nest-and-parent-child-relation/129450/4 "2018-05-29T12:54:28Z")

</div>

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