Trying to follow can't follow instructions on Parent/Child relationships

I'm following the example on this page: Parent/Child relationships | Elasticsearch.Net and NEST: the .NET clients [7.x] | Elastic. My program fails indexing the parent, with: ServerError: Type: routing_missing_exception Reason: "routing is required for [index]/[_doc]/[1]"

I'm using elasticsearch 7.10.2 in a container and NEST 7.10.0, running in a .net 5 command line program. The code is below.
I assume that either: there is something different about my code and the documentation that I've missed OR something is missing from the documentation.

Thanks for any pointers or suggestions.

using System;
using System.Diagnostics;
using Elasticsearch.Net;
using Nest;

namespace elastic_tester
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Trying to follow https://www.elastic.co/guide/en/elasticsearch/client/net-api/7.x/parent-child-relationships.html");

            var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
            var connectionSettings = new ConnectionSettings(connectionPool)
                .DefaultMappingFor<MyDocument>(m => m
                    .IndexName("index"))
                .DefaultMappingFor<MyChild>(m => m
                    .IndexName("index"))
                .DefaultMappingFor<MyParent>(m => m
                    .IndexName("index")
//                    .RoutingProperty(rp => rp.Id)
                    .RelationName("parent"))
                .DisableDirectStreaming(true)
                .PrettyJson();

            var client = new ElasticClient(connectionSettings);

            var clusterHealth = client.Cluster.Health();
            Console.WriteLine($"Running elasticsearch cluster: '{clusterHealth.ClusterName}', status: {clusterHealth.Status}");

            var createIndexResponse = client.Indices
                .Create("index", c => c
                .Index<MyDocument>()
                .Map<MyDocument>(m => m
                    .RoutingField(r => r.Required())
                    .AutoMap<MyParent>()
                    .AutoMap<MyChild>()
                    .Properties(props => props
                        .Join(j => j
                            .Name(p => p.MyJoinField)
                            .Relations(r => r
                                .Join<MyParent, MyChild>() ) ) ) ) );

            Debug.Assert(createIndexResponse.Acknowledged && createIndexResponse.IsValid);
            Console.WriteLine($"Created index named {createIndexResponse.Index}");


            var parentDocument = new MyParent
            {
                Id = 1,
                ParentProperty = "a parent property value",
                MyJoinField = JoinField.Root<MyParent>()
            };

            // with elasticsearch server 7.10.2 and NEST 7.10.0
            // I get a routing exception
            var indexResponse = client.IndexDocument(parentDocument);
            Console.WriteLine(indexResponse.DebugInformation);
            Debug.Assert(indexResponse.IsValid);
        }
    }

    public abstract class MyDocument
    {
        public int Id { get; set; }
        public JoinField MyJoinField { get; set; }
    }

    public class MyParent : MyDocument
    {
        [Text]
        public string ParentProperty { get; set; }
    }

    public class MyChild : MyDocument
    {
        [Text]
        public string ChildProperty { get; set; }
    }
}

Welcome to our community! :smiley:

I don't know NEST, but the first thing I would recommend is using the documentation for your version of Elasticsearch. 7.X is master, which will contain things that have not been released, or things that have changed, so should not be used for what you are doing.

Thanks for the idea! While the main elasticsearch documentation is split into many versions (major, minor), the NEST library doc just has these
image

After selecting 'other versions' it looks like this:
image

So I think 7.x is the closest to the version that I'm using. Right?

1 Like

Ah ok, I guess that documentation is structured a little differently to the product set.

Meant to include the output:

Trying to follow https://www.elastic.co/guide/en/elasticsearch/client/net-api/7.x/parent-child-relationships.html
Running elasticsearch cluster: 'docker-cluster', status: Yellow
Created index named index
Invalid NEST response built from a unsuccessful (400) low level call on PUT: /index/_doc/1?pretty=true
# Audit trail of this API call:
 - [1] BadResponse: Node: http://localhost:9200/ Took: 00:00:00.0435007
# OriginalException: Elasticsearch.Net.ElasticsearchClientException: Request failed to execute. Call: Status code 400 from: PUT /index/_doc/1?pretty=true. ServerError: Type: routing_missing_exception
 Reason: "routing is required for [index]/[_doc]/[1]"
# Request:
{"parentProperty":"a parent property value","id":1,"myJoinField":"parent"}
# Response:
{
  "error" : {
    "root_cause" : [
      {
        "type" : "routing_missing_exception",
        "reason" : "routing is required for [index]/[_doc]/[1]",
        "index_uuid" : "_na_",
        "index" : "index"
      }
    ],
    "type" : "routing_missing_exception",
    "reason" : "routing is required for [index]/[_doc]/[1]",
    "index_uuid" : "_na_",
    "index" : "index"
  },
  "status" : 400
}

Not 100% sure what is missing from the documented example, but I believe the child object needs another attribute, defined as the RoutingProperty. The new attribute needs to be set when you index the child documents - we use the parent's Id.

I'm no longer watching this, as I got my parent / child relationship working with Elasticsearch 7.10.

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