i'm here new in elastic and .net. Let's go to the point, i'm trying to get child when its parent executed with has child query. I have read some article, and it said i can use inner hits to return child and parent together. So i wrote the json query and it ran successfully. However, when the query converted to NEST, it can't return the inner hits result. You could see json query (GET API) in "Search Query 1" and NEST query in "Search Query 2".
All information is described below:
Index
GET music
{
"music" : {
"aliases" : { },
"mappings" : {
"properties" : {
"artist_relationship" : {
"type" : "join",
"eager_global_ordinals" : true,
"relations" : {
"song" : "user",
"artist" : "song"
}
},
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"song" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"user" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
},
"settings" : {
"index" : {
"routing" : {
"allocation" : {
"include" : {
"_tier_preference" : "data_content"
}
}
},
"number_of_shards" : "1",
"provided_name" : "music",
"creation_date" : "1648194806610",
"number_of_replicas" : "0",
"uuid" : "Ps-cumUnRcKmTt6QDudZUA",
"version" : {
"created" : "7140099"
}
}
}
}
}
Add parents
POST music/_bulk
{"index":{"_id":1}}
{"name":"John Legend","artist_relations":{"name":"artist"}}
{"index":{"_id":2}}
{"name":"Ariana Grande","artist_relations":{"name":"artist"}}
Add children
PUT music/_doc/3?routing=1
{"song":"All of Me","artist_relations":{"name":"song","parent":1}}
PUT music/_doc/4?routing=1
{"song":"Beauty and the Beast","artist_relations":{"name":"song","parent":1}}
PUT music/_doc/5?routing=2
{"song":"Beauty and the Beast","artist_relations":{"name":"song","parent":2}}
Add grandchildren
POST music/_bulk?routing=3
{"index":{"_id":"l-1"}}
{"user":"Gabriel","artist_relations":{"name":"user","parent":3}}
{"index":{"_id":"l-2"}}
{"user":"Berte","artist_relations":{"name":"user","parent":3}}
{"index":{"_id":"l-3"}}
{"user":"Emma","artist_relations":{"name":"user","parent":3}}
POST music/_doc/l-4?routing=4
{"user":"Berte","artist_relations":{"name":"user","parent":4}}
POST music/_doc/l-5?routing=5
{"user":"Emma","artist_relations":{"name":"user","parent":5}}
Model Mapping
public abstract class ArtistRelationship
{
public JoinField artist_relationship { get; set; }
}
public class Artist : ArtistRelationship
{
public string name { get; set; }
}
public class Song : ArtistRelationship
{
public string song { get; set; }
}
Search Query 1
GET music/_search
{
"query": {
"has_child": {
"type": "song",
"min_children": 1,
"max_children": 10,
"query": {
"match_all": {}
},
"inner_hits": {}
}
}
}
Search Query 2
var response = client.Search<Artist>(sd => sd
.Index("music")
.Query(qcd => qcd
.HasChild<Song>(hc => hc
.MinChildren(1)
.MaxChildren(10)
.Query(qcd2 => qcd2
.MatchAll()
)
.InnerHits()
)
)
);
Output: please check this link
As you can see on the output, from "Search Query 1" i got song called "All of me" but in the right side (Search Query 2), i couldn't find any key containing "All of me" value.
I have checked all object and only got "_id". When compared to output from Search Query 1, the results are similar. Both query returned "3" value.
I'm not sure i'm wrong on model mapping, because i have followed the docs instruction (https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/parent-child-relationships.html#_parent_and_child_example)
I'm sorry for bad english, i hope you all can understand . Thank you for taking your time and thanks in advance.