Post_filtering or filtering the inner hits of parent-child search query result?

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.

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