Reputation: 2966
I'm using Elasticsearch RC 2.0.0.
I got some parent-child relations In my Elasticsearch db. I want to retrieve all Children related to a parent object. I always receive an empty list of results. I followed the instructions of the elasticsearch documentation and I compared my code to several Books. I don't understand, why my query should return an empty result.
In this case I built an simplified example. I put two objects to elasticsearch and set object a as the parent of object b. Then I try to retrieve all objects, wich have a parent of type a.
This is my Input:
PUT test
PUT test/myobject/_mapping
{
"myobject":{
"_parent" : {"type":"pobject"},
"properties" : {
"name" : {"type":"string"}
}
}
}
PUT test/pobject/_mapping
{
"pobject" : {
"properties": {
"name": {"type":"string"}
}
}
}
PUT test/pobject/1
{
"name":"theParent"
}
PUT test/myobject/1?_parent=1&routing=_id
{
"name":"theChild"
}
POST test/myobject/_search?routing=_id
{
"query":{
"has_parent":{
"type":"pobject",
"query":{
"match_all":{}
}
}
}
}
This will return
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}
Upvotes: 3
Views: 1204
Reputation: 52368
The mistake is here: PUT test/myobject/1?_parent=1&routing=_id
The parameter is parent
, not _parent
.
POST test/myobject/1?parent=1
{
"name": "theChild"
}
Also, you don't need to use routing=_id
. See the documentation.
The complete list of commands to test:
DELETE test
PUT test
PUT test/myobject/_mapping
{
"myobject": {
"_parent": {
"type": "pobject"
},
"properties": {
"name": {
"type": "string"
}
}
}
}
PUT test/pobject/_mapping
{
"pobject": {
"properties": {
"name": {
"type": "string"
}
}
}
}
POST test/pobject/1
{
"name": "theParent"
}
POST test/myobject/1?parent=1
{
"name": "theChild"
}
POST test/myobject/_search
{
"query": {
"has_parent": {
"parent_type": "pobject",
"query": {
"match_all": {}
}
}
}
}
Upvotes: 3