# Search/count children only

**URL:** <https://discuss.elastic.co/t/search-count-children-only/184729>\
**Category:** Elasticsearch\
**Created:** [June 7, 2019, 9:35am UTC](https://discuss.elastic.co/t/search-count-children-only/184729 "2019-06-07T09:35:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![michael04](https://avatars.discourse-cdn.com/v4/letter/m/ebca7d/32.png) [@michael04](https://discuss.elastic.co/u/michael04)\
**Post date:** [June 7, 2019, 9:35am UTC](https://discuss.elastic.co/t/search-count-children-only/184729/1 "2019-06-07T09:35:33Z")

</div>

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

```auto
    {
        "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:

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

```

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

```auto
    {
        "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...

---

<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:** [July 5, 2019, 9:35am UTC](https://discuss.elastic.co/t/search-count-children-only/184729/2 "2019-07-05T09:35:40Z")

</div>

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