NEST and parent/child relation

Hi!
I try to use parent/child relation following by this document.

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):

         protected void IndexData<T>(IReadOnlyCollection<T> data, int partSize = 0) where T: class 
     {
         Exception exception = null;
    
         var waitHandle = new ManualResetEvent(false);
    
         var bulkObserver = new BulkAllObserver(
             onError: e =>
             {
                 exception = e;
                 waitHandle.Set();
                 throw e;
             },
             onCompleted: () => waitHandle.Set(),
             onNext: b => { }
         );
    
         IBulkAllRequest<T> Selector(BulkAllDescriptor<T> x)
         {
             var result = x.Index("a_test_index").MaxDegreeOfParallelism(4).RefreshOnCompleted();
             if (partSize != 0)
             {
                 result = result.Size(partSize);
             }
    
             return result;
         }
    
         var cancellationTokenSource = new CancellationTokenSource();
         var bulkAll = ElasticClient.BulkAll(data, Selector, cancellationTokenSource.Token);
         bulkAll.Subscribe(bulkObserver);
    
         waitHandle.WaitOne();
    
         if (exception != null)
         {
             throw exception;
         }
     }

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

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.

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