Search/count children only

I'm looking for the best way to search children only. I have create the following mapping with a prefix -> file parent-child relation.

    {
        "mappings": {
            "_doc": {
                "properties": {
                    "created": {
                        "type": "date"
                    },
                    "key": {
                        "type": "keyword"
                    },
                    "prefix": {
                        "type": "keyword"
                    },
                    "file_name": {
                        "type": "keyword"
                    },
                    "file_extension": {
                        "type": "keyword"
                    },
                    "file_size": {
                        "type": "long"
                    },
                    "metadata": {
                        "type": "object"
                    },
                    "doc_relation": {
                        "type": "join",
                        "relations": {
                            "prefix": "file"
                        }
                    }
                }
            }
        }
    }

Now I want to count how many child documents there are. My first naive approach failed:

    {
        "query": {
            "term": {
                "doc_relation.prefix": "file"
            }
        }
    }

So I came up with the following query, which gives the correct result.

    {
        "query": {
            "has_parent": {
                "parent_type": "prefix",
                "query": {
                    "match_all": {}
                }
            }
        }
    }

However, I believe that the has_parent query with match_all might add an additional (useless) overhead.

Is this the correct and best way to solve this? Are there better approaches?

PS: I am new to elasticsearch, though I'm working with different databases for quite some time...

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