Morten Hauberg
Morten Hauberg

Reputation: 571

Elasticsearch inner hits on grandchildren

I some troubles with the new inner_hits feature. When used on parent/child it works, but if i try to use it on a grandchild, it doesn't seem to work.

This is my mapping

{
    "test": {
        "template": "test",
        "settings": {
            "index": {
                "number_of_replicas": 0
            }
        },
        "mappings": {
            "parents": {
                "dynamic": "strict",
                "_routing": {
                    "required": true
                },
                "properties": {
                    "parent_value": {
                        "type": "string"
                    }
                }
            },
            "children": {
                "dynamic": "strict",
                "_routing": {
                    "required": true
                },
                "_parent": {
                    "type": "parents"
                },
                "properties": {
                    "parent_id": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "child_value": {
                        "type": "string"
                    }
                }
            },
            "grandchildren": {
                "dynamic": "strict",
                "_routing": {
                    "required": true
                },
                "_parent": {
                    "type": "children"
                },
                "properties": {
                    "children_id": {
                        "type": "string",
                        "index": "not_analyzed"
                    }
                }
            }
        }
    }
}

I insert data via Sense

PUT test/parents/parent_id?routing=1
{
    "parent_value": "PARENT VALUE"
}

PUT test/children/child_id?routing=1&parent=parent_id
{
    "parent_id": "parent_id",
    "child_value": "CHILD VALUE"
}

PUT test/grandchildren/grandchild_id?routing=1&parent=child_id
{
    "children_id": "child_id"
}

This works perfect

GET test/children/_search?routing=1
{
   "post_filter": {
      "bool": {
         "must": [
            {
               "has_parent": {
                  "parent_type": "parents",
                  "filter": {
                     "bool": {
                        "must": [
                           {
                              "ids": {
                                  "values": ["parent_id"]
                              }
                           }
                        ]
                     }
                  },
                  "inner_hits": {
                  }
               }
            }
         ]
      }
   }
}

Yay!

But if i try this, it finds a document, but the inner_hits is empty.

GET test/grandchildren/_search?routing=1
{
   "post_filter": {
      "bool": {
         "must": [
            {
               "has_parent": {
                  "parent_type": "children",
                  "filter": {
                     "bool": {
                        "must": [
                           {
                              "ids": {
                                  "values": ["child_id"]
                              }
                           }
                        ]
                     }
                  },
                  "inner_hits": {
                  }
               }
            }
         ]
      }
   }
}

What am i doing wrong..?

Upvotes: 0

Views: 911

Answers (1)

Mon Calamari
Mon Calamari

Reputation: 4463

It's a known issue. The workaround is to duplicate your query for all levels of inner hits branch:

curl -XGET "http://localhost:9200/_search" -d'
{
  "query": {
    "nested": {
      "path": "cars",
      "query": {
        "nested": {
          "path": "cars.manufacturers",
          "query": {
            "match": {
              "cars.manufacturers.country": "Japan"
            }
          }
        }
      }
    }
  },
  "inner_hits": {
    "cars": {
      "path": {
        "cars": {
          "query": {
            "nested": {
              "path": "cars.manufacturers",
              "query": {
                "match": {
                  "cars.manufacturers.country": "Japan"
                }
              }
            }
          },
          "inner_hits": {
            "manufacturers": {
              "path": {
                "cars.manufacturers": {
                  "query": {
                    "match": {
                      "cars.manufacturers.country": "Japan"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}'

Upvotes: 1

Related Questions