# Post\_filtering or filtering the inner hits of parent-child search query result?

**URL:** <https://discuss.elastic.co/t/post-filtering-or-filtering-the-inner-hits-of-parent-child-search-query-result/179291>\
**Category:** Elasticsearch\
**Created:** [May 2, 2019, 7:04am UTC](https://discuss.elastic.co/t/post-filtering-or-filtering-the-inner-hits-of-parent-child-search-query-result/179291 "2019-05-02T07:04:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![fajara\_kurniawan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fajara_kurniawan/32/45370_2.png) [@fajara\_kurniawan](https://discuss.elastic.co/u/fajara_kurniawan)\
**Post date:** [May 2, 2019, 7:04am UTC](https://discuss.elastic.co/t/post-filtering-or-filtering-the-inner-hits-of-parent-child-search-query-result/179291/1 "2019-05-02T07:04:30Z")

</div>

lets say i have this kind search query for parent\_child / join datatypes.

```
{
    "query": {
        "bool": {
            "must": [
                {
                    "has_child": {
                        "type": "order_details",
                        "min_children": 1,
                        "query": {
                            "bool": {
                                "must": [
                                    {
                                        "range": {
                                            "order_date": {
                                                "gte": "2019-02-13",
                                                "lte": "2019-04-14"
                                            }
                                        }
                                    },
                                    {
                                        "terms": {
                                            "payment_state": [
                                                "paid",
                                                "paid_75"
                                            ]
                                        }
                                    }
                                ],
                                "must_not": [
                                    {
                                        "match": {
                                            "business_unit_id": 42
                                        }
                                    }
                                ]
                            }
                        },
                        "inner_hits": {
                            "size": 1,
                            "sort": {
                                "order_date": "desc"
                            }
                        }
                    }
                },
                {
                    "has_child": {
                        "type": "order_details",
                        "min_children": 2,
                        "query": {
                            "match_all": {}
                        }
                    }
                }
            ]
        }
    }
}  

```

the idea is to find the last order for each customer that matching that filter criteria and has at least 2 total order. that query accomplished that idea nicely. However , i need to do more filtering after that filter . for example check if their last order that matched the filter has sent\_type of BE , i'm thinking something like this :

```
{
    "query": {
        "bool": {
            "must": [
                {
                    "has_child": {
                        "type": "order_details",
                        "min_children": 1,
                        "query": {
                            "bool": {
                                "must": [
                                    {
                                        "range": {
                                            "order_date": {
                                                "gte": "2019-02-13",
                                                "lte": "2019-04-14"
                                            }
                                        }
                                    },
                                    {
                                        "terms": {
                                            "payment_state": [
                                                "paid",
                                                "paid_75"
                                            ]
                                        }
                                    }
                                ],
                                "must_not": [
                                    {
                                        "match": {
                                            "business_unit_id": 42
                                        }
                                    }
                                ]
                            }
                        },
                        "inner_hits": {
                            "size": 1,
                            "sort": {
                                "order_date": "desc"
                            }
                        }
                    }
                },
                {
                    "has_child": {
                        "type": "order_details",
                        "min_children": 2,
                        "query": {
                            "match_all": {}
                        }
                    }
                }
            ]
        }
    },
    "post_filter": {
        "match": {
            "type": "BE"
        }
    }
}

```

which obviously doesn't work, since i need to do the post filtering in inner\_hits.  
edit : I'm on elastic search 6.7

any kinds of help is appreciated.

thanks.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 30, 2019, 7:04am UTC](https://discuss.elastic.co/t/post-filtering-or-filtering-the-inner-hits-of-parent-child-search-query-result/179291/2 "2019-05-30T07:04:33Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
