Nested has_child query

Hello,
I have an index, in which there are 3 types of document, call them parent
-> child -> grandchild. So every grandchild's _parent is some child, and
every child's _parent is some parent.

Performing these two queries yields correct results

curl -XPOST http://localhost:9200/my_index/parent/_search -d '{
"filter": {
"has_child": {
"query": {
"match_all": {}
},
"type": "child"
}
},
"from": 0,
"query": {
"match_all": {}
},
"size": 10
}'

curl -XPOST http://localhost:9200/my_index/child/_search -d '{
"filter": {
"has_parent": {
"parent_type": "parent",
"query": {
"match_all": {}
}
}
},
"from": 0,
"query": {
"match_all": {}
},
"size": 10
}'

But when doing a deeper query I get an empty list, even though there are
correct values in ES:

curl -XPOST http://localhost:9200/my_index/child/_search -d '{
"filter": {
"has_child": {
"query": {
"match_all": {}
},
"type": "grandchild"
}
},
"from": 0,
"query": {
"match_all": {}
},
"size": 10
}'

Also, the same seems to hold (i.e. empty list is returned) when trying to
search for a parent that has children that have grandchildren:

curl -XPOST http://localhost:9200/my_index/parent/_search/ -d '{
"query": {
"has_child": {
"type": "child",
"query": {
"has_child": {
"type": "grandchild",
"query": {
"match_all": {}
}
}
}
}
}
}'

I'm using the most recent ElasticSearch server, i.e. 0.20.2

Could someone tell me what is the problem?

Cheers,
Przemek

--

When you indexed grandchildren, did you specify the routing of the top
parent? In other words did you index grandchildren like this:

curl -XPUT
"http://localhost:9200/my_index/grandchild/grandchild_id?parent=child_id&routing=parent_id"
-d '{.....}'

or just

curl -XPUT
"http://localhost:9200/my_index/grandchild/grandchild_id?parent=child_id" -d
'{.....}'

On Friday, January 4, 2013 7:36:10 AM UTC-5, Przemek wrote:

Hello,
I have an index, in which there are 3 types of document, call them parent
-> child -> grandchild. So every grandchild's _parent is some child, and
every child's _parent is some parent.

Performing these two queries yields correct results

curl -XPOST http://localhost:9200/my_index/parent/_search -d '{
"filter": {
"has_child": {
"query": {
"match_all": {}
},
"type": "child"
}
},
"from": 0,
"query": {
"match_all": {}
},
"size": 10
}'

curl -XPOST http://localhost:9200/my_index/child/_search -d '{
"filter": {
"has_parent": {
"parent_type": "parent",
"query": {
"match_all": {}
}
}
},
"from": 0,
"query": {
"match_all": {}
},
"size": 10
}'

But when doing a deeper query I get an empty list, even though there are
correct values in ES:

curl -XPOST http://localhost:9200/my_index/child/_search -d '{
"filter": {
"has_child": {
"query": {
"match_all": {}
},
"type": "grandchild"
}
},
"from": 0,
"query": {
"match_all": {}
},
"size": 10
}'

Also, the same seems to hold (i.e. empty list is returned) when trying to
search for a parent that has children that have grandchildren:

curl -XPOST http://localhost:9200/my_index/parent/_search/ -d '{
"query": {
"has_child": {
"type": "child",
"query": {
"has_child": {
"type": "grandchild",
"query": {
"match_all": {}
}
}
}
}
}
}'

I'm using the most recent Elasticsearch server, i.e. 0.20.2

Could someone tell me what is the problem?

Cheers,
Przemek

--

Yaaay! Works! Thank you, didn't have a clue that it has to be indexed this
way. :slight_smile:

Cheers,
Przemek

On Friday, January 4, 2013 2:52:58 PM UTC+1, Igor Motov wrote:

When you indexed grandchildren, did you specify the routing of the top
parent? In other words did you index grandchildren like this:

curl -XPUT "
http://localhost:9200/my_index/grandchild/grandchild_id?parent=child_id&routing=parent_id"
-d '{.....}'

or just

curl -XPUT "
http://localhost:9200/my_index/grandchild/grandchild_id?parent=child_id" -d
'{.....}'

On Friday, January 4, 2013 7:36:10 AM UTC-5, Przemek wrote:

Hello,
I have an index, in which there are 3 types of document, call them parent
-> child -> grandchild. So every grandchild's _parent is some child, and
every child's _parent is some parent.

Performing these two queries yields correct results

curl -XPOST http://localhost:9200/my_index/parent/_search -d '{
"filter": {
"has_child": {
"query": {
"match_all": {}
},
"type": "child"
}
},
"from": 0,
"query": {
"match_all": {}
},
"size": 10
}'

curl -XPOST http://localhost:9200/my_index/child/_search -d '{
"filter": {
"has_parent": {
"parent_type": "parent",
"query": {
"match_all": {}
}
}
},
"from": 0,
"query": {
"match_all": {}
},
"size": 10
}'

But when doing a deeper query I get an empty list, even though there are
correct values in ES:

curl -XPOST http://localhost:9200/my_index/child/_search -d '{
"filter": {
"has_child": {
"query": {
"match_all": {}
},
"type": "grandchild"
}
},
"from": 0,
"query": {
"match_all": {}
},
"size": 10
}'

Also, the same seems to hold (i.e. empty list is returned) when trying to
search for a parent that has children that have grandchildren:

curl -XPOST http://localhost:9200/my_index/parent/_search/ -d '{
"query": {
"has_child": {
"type": "child",
"query": {
"has_child": {
"type": "grandchild",
"query": {
"match_all": {}
}
}
}
}
}
}'

I'm using the most recent Elasticsearch server, i.e. 0.20.2

Could someone tell me what is the problem?

Cheers,
Przemek

--

Out of curiosity: if I added one more level, i.e. grandgrandchild, how
should I index it with

curl -XPUT
'http://localhost:9200/my_index/grandgrandchild/grandgrandchild_id?parent=grandchild_id&routing=child_id&routing=parent_id"
-d '{...}'

or in some other manner?

Przemek

--

The has_child query can only work if both parent and child reside in the
same shard. With one level, it's done automatically: parents are routed
based on their ids (parent_id) and when you index children you specify
parent=parent_id on URI, and elasticsearch is using parent_id for routing
instead of child_id. When you index grandchildren, it's getting tricky,
because they should be indexed into the same shard as their ancestors, but
both parent=... value and their own ids might contain wrong routing value.
That's way you have to specify correct routing value (id of the top parent)
in the routing parameter. Similarly, when you are
indexing grate-grandchild, you still need to specify the id of the top
parent as a routing value to make sure that it will land in the same shards
as all ancestors:

curl -XPUT '
http://localhost:9200/my_index/grandgrandchild/grandgrandchild_id?parent=grandchild_id&routing=parent_idhttp://localhost:9200/my_index/grandgrandchild/grandgrandchild_id?parent=grandchild_id&routing=child_id&routing=parent_id"
-d '{...}'

On Friday, January 4, 2013 9:58:15 AM UTC-5, Przemek wrote:

Out of curiosity: if I added one more level, i.e. grandgrandchild, how
should I index it with

curl -XPUT '
http://localhost:9200/my_index/grandgrandchild/grandgrandchild_id?parent=grandchild_id&routing=child_id&routing=parent_id"
-d '{...}'

or in some other manner?

Przemek

--

OK, thank you for clarifying this.

Przemek

--