Parent child relation to 2nd level and more - has_child query

I had partial success implementing parent child relation. I was able to
create the mappings correctly (I presume), and post documents without any
issue.
In the example below, "family" index has types "dad", "child",
"grandchild". "child" has parent type as "dad", and "grandchild" has parent
type "child".

While using "has_child" query, I could only get documents 1 level deep (dad
to child), but not to the 2nd level (child to grandchild). See the last
POST at the bottom that didn't return grandchild as expected.

  • Can you please review whats wrong here (if any) ?
  • Any idea on whats going on would be greatly appreciated!

POST http://localhost:9200/family
{
"mappings": {
"dad": {
"properties": {
"name": {
"type": "string", "index": "not_analyzed"
}
}
},
"child": {
"_parent": {
"type": "dad"
},
"properties": {
"name": {
"type": "string", "index": "not_analyzed"
}
}
},
"grandchild": {
"_parent": {
"type": "child"
},
"properties": {
"name": {
"type": "string", "index": "not_analyzed"
}
}
}
}
}

POST http://localhost:9200/family/dad/1
{
"name" : "Dad1"
}

POST http://localhost:9200/family/child/10?parent=1
{
"name" : "Child1",
"_parent" : "1"
}

POST http://localhost:9200/family/grandchild/100?parent=10
{
"name" : "GrandChild1",
"_parent" : "10"
}

POST http://localhost:9200/family/dad/_search
{
"query" : {
"has_child" : {
"type" : "child",
"query" : {
"match" : {
"name" : "Child1"
}
}
}
}
}

Response to the above POST:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "family",
"_type": "dad",
"_id": "1",
"_score": 1,
"_source": {
"name": "Dad1"
}
}
]
}
}

POST http://localhost:9200/family/child/_search
{
"query" : {
"has_child" : {
"type" : "grandchild",
"query" : {
"match" : {
"name" : "GrandChild1"
}
}
}
}
}

Response to the above POST (it doesn't return "child" type with name
"Child1"):
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/d2cdb28d-1c8e-4054-b5a2-9d58cf811aa1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

I solved the above problem by adding "routing" parameter to the POST to
create grandchild. By default ES uses parent id of grandchild unless
routing is explicitly specified (for example in URL). Here I specified
routing to be the id of the dad (or grandparent from the perspective of the
grandchild). After the routing is specified in creation, last POST to
search child having the grandchild with name 'GrandChild1' succeeds.

FYI Relevant StackOverflow question & answer here.

POST http://localhost:9200/family/grandchild/100?parent=10&routing=1
{
"name" : "GrandChild1",
"_parent" : "10"
}

On Sunday, November 30, 2014 10:24:32 PM UTC-6, Madhav A wrote:

I had partial success implementing parent child relation. I was able to
create the mappings correctly (I presume), and post documents without any
issue.
In the example below, "family" index has types "dad", "child",
"grandchild". "child" has parent type as "dad", and "grandchild" has parent
type "child".

While using "has_child" query, I could only get documents 1 level deep
(dad to child), but not to the 2nd level (child to grandchild). See the
last POST at the bottom that didn't return grandchild as expected.

  • Can you please review whats wrong here (if any) ?
  • Any idea on whats going on would be greatly appreciated!

POST http://localhost:9200/family http://localhost:9200/family
{
"mappings": {
"dad": {
"properties": {
"name": {
"type": "string", "index": "not_analyzed"
}
}
},
"child": {
"_parent": {
"type": "dad"
},
"properties": {
"name": {
"type": "string", "index": "not_analyzed"
}
}
},
"grandchild": {
"_parent": {
"type": "child"
},
"properties": {
"name": {
"type": "string", "index": "not_analyzed"
}
}
}
}
}

POST http://localhost:9200/family/dad/1
http://localhost:9200/family/dad/1

{
"name" : "Dad1"
}

POST http://localhost:9200/family/child/10?parent=1
http://localhost:9200/family/child/10?parent=1

{
"name" : "Child1",
"_parent" : "1"
}

POST http://localhost:9200/family/grandchild/100?parent=10
http://localhost:9200/family/grandchild/100?parent=10

{
"name" : "GrandChild1",
"_parent" : "10"
}

POST http://localhost:9200/family/dad/_search
http://localhost:9200/family/dad/_search

{
"query" : {
"has_child" : {
"type" : "child",
"query" : {
"match" : {
"name" : "Child1"
}
}
}
}
}

Response to the above POST:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "family",
"_type": "dad",
"_id": "1",
"_score": 1,
"_source": {
"name": "Dad1"
}
}
]
}
}

POST http://localhost:9200/family/child/_search
http://localhost:9200/family/child/_search

{
"query" : {
"has_child" : {
"type" : "grandchild",
"query" : {
"match" : {
"name" : "GrandChild1"
}
}
}
}
}

Response to the above POST (it doesn't return "child" type with name
"Child1"):
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/122083d0-0feb-43a3-9835-3519ca8ff2f6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.