Avión
Avión

Reputation: 8386

Elasticsearch children-father issue with 'has_parent'

Having the following mapping...

curl -XPUT 'localhost:9200/myindex' -d '{
  "mappings": {
    "my_parent": {},
    "my_child": {
      "_parent": {
        "type": "my_parent" 
}}}}'

... the following parent:

curl -X PUT localhost:9200/myindex/my_parent/1?pretty=true' -d '{
  "title" : "Microsiervos - Discos duros de 10TB",
  "body" : "Empiezan a sacar DD de 30GB en el mercado"
}'

and the following children:

curl -XPUT 'localhost:9200/myindex/my_child/2?parent=1' -d '{
  "user": "Pepe"
}'

If I do the following has_child query:

curl -XGET 'localhost:9200/myindex/my_parent/_search?pretty=true' -d '{
  "query": {
    "has_child": { 
      "type": "my_child",
      "query" : {
           "query_string" : {
               "default_field" : "user",
               "query" : "Pepe"
}}}}}'

I get the desired output. Pepe is found, and his father is shown:

  "hits" : {
    "total" : 1,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "myindex",
      "_type" : "my_parent",
      "_id" : "2",
      "_score" : 1.0,
      "_source":{
  "title" : "Microsiervos - En el 69 llegamos a la luna",
  "body" : "Se cumplen 3123 anos de la llegada a la luna"
}

But if I try to do it in reverse trying to get the children using has_parent:

curl -XGET 'localhost:9200/myindex/my_parent/_search?pretty=true' -d '{
  "query": {
    "has_parent": { 
      "parent_type": "my_parent",
      "query" : {
           "query_string" : {
               "default_field" : "body",
               "query" : "mercado"
}}}}}'

I dont get any hits. I was supposing to get the Pepe children as output. What I'm missing or doing wrong?

PS: I'm using Elasticsearch 2.1.1

Upvotes: 0

Views: 25

Answers (1)

Richa
Richa

Reputation: 7649

You have made a mistake above.You are searching in my_parent type. If you want to fetch children using parent, you should fetch it from child_type. Change query to:

curl -XGET 'localhost:9200/myindex/my_child/_search?pretty=true' -d      
'{
"query": {
"has_parent": { 
  "parent_type": "my_parent",
  "query" : {
       "query_string" : {
           "default_field" : "body",
           "query" : "mercado"
      }
    }
  }
 }
}'

Please note I have used

curl -XGET 'localhost:9200/myindex/my_child/_search?pretty=true' 

instead of

curl -XGET 'localhost:9200/myindex/my_parent/_search?pretty=true'

Upvotes: 1

Related Questions