Parent/Child relationship with NEST on 6.x


(Carlo Arnaboldi) #1

Greetings. I've been tasked to gain some basic knowledge with NEST / Elastic search to verify if this product suits our needs. I've a C# / Relational databases background.

I've installed Kibana (6.1.1) and Elastic search (6.1.1)

I've also created a VS2017 project with latests version of elasticsearch.NET (6.0.0-rc1) and NEST (6.0.0-rc1) with the idea to create 2 types which share a parent-child relation. After the types are created correctly, I'm planning to import some documents from 2 existing SQL tables which are in one-to-many relationship.

I've been following the guide at this page to do so (doing exactly what is stated):

https://www.elastic.co/guide/en/elasticsearch/client/net-api/master/parent-child-relationships.html

but several pieces of code are not compiling, for instance:

var infer = client.Infer;
var parent = new MyParent {Id = 1337, MyJoinField = JoinField.Root<MyParent>()};
infer.Routing(parent).Should().Be("1337");

I'm unable to find the "Routing" member of the class "Inferrer" (object infer). It looks like I'm working with the wrong guide but I've been downloading the most recent NEST / ElasticSearch.NET packets available on nuget (including prereleases) and I'm looking at the most recent NEST guide (I've selected "master" in the combobox to choose guide version).


Unable to create an index with parent child relationship using .net NEST
(Russ Cam) #2

Hey @Carlo_Arnaboldi, the usage of Infer within the documentation is simply to assert that the routing value is what is expected; the documentation is generated from tests to ensure that it stays in line with the client.

In your code, you would simply index the Parent and Child documents, and there are a number of different ways that you can specify the join field.

For Parent

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

// 2. Use typeof(MyParent)
parentDocument = new MyParent
{
    Id = 1,
    ParentProperty = "a parent prop",
    MyJoinField = typeof(MyParent) 
};

// 2. Use string
parentDocument = new MyParent
{
    Id = 1,
    ParentProperty = "a parent prop",
    MyJoinField = "myparent" 
};

var indexParentResponse = client.IndexDocument<MyDocument>(parentDocument);

and for Child

// 1. Use JoinField.Link<MyChild, MyParent>(parentDocument)
var childDocument = new MyChild
{
    MyJoinField = JoinField.Link<MyChild, MyParent>(parentDocument)
});

// 2. Use JoinField.Link<MyChild>(1)
childDocument = new MyChild
{
    Id = 2,
    MyJoinField = JoinField.Link<MyChild>(1)
});

indexChildResponse = client.IndexDocument<MyDocument>(childDocument );

(Carlo Arnaboldi) #3

Sorry for the late reply. Thanks for pointing that out. Anyway at the end we decided avoid ES joins since they are not supported in Kibana. We'll perform application-side joins in the event that we need to query ES outside of Kibana (with NEST, for example)


(system) #4

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