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; }
}
}