Retrieve the children documents in a parent-child

I have 2 types in my index: profile and post.
each profile has many posts, so we decided to make a parent-child relationship in the mapping
Each profile has a profile_id. I want to search on the profile type, and get all the posts of a specific id...
I tried many ways but without any luck...


It sounds like what you want is either the has_child or has_parent query. For instance, if you had posts as a child of profiles, then if you wanted all the posts from a particular profile_id:

POST /index/posts/_search
  "query": {
    "has_parent": {
        "parent_type": "profile",
        "query": {
          "match": {
            "id": 4

However, you could also denormalize your data and add the profile id into each post, this is the most efficient way to do this (for the future or reindexing) since then you don't have to do a child or parent query.

First, thanks for the reply
Second, In the end I did found myself denormalize the data...which made me think - in which scenario i WILL use the parent-child insted of denormalizing the data?

So the use-case where parent-child is a better alternative is when you need to do a lot of updating to the individual documents independently, and those sub-documents also need to be treated as distinct on their own. Generally, though, it's always easier to deal with the data if it's denormalized however.