# Getting number of childrens in the query when searching for parent?

**URL:** <https://discuss.elastic.co/t/getting-number-of-childrens-in-the-query-when-searching-for-parent/109217>\
**Category:** Elasticsearch\
**Created:** [November 27, 2017, 12:47pm UTC](https://discuss.elastic.co/t/getting-number-of-childrens-in-the-query-when-searching-for-parent/109217 "2017-11-27T12:47:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jalaj](https://avatars.discourse-cdn.com/v4/letter/j/e79b87/32.png) [@jalaj](https://discuss.elastic.co/u/jalaj)\
**Post date:** [November 27, 2017, 12:47pm UTC](https://discuss.elastic.co/t/getting-number-of-childrens-in-the-query-when-searching-for-parent/109217/1 "2017-11-27T12:47:27Z")

</div>

Hi,

I have indexed some data using in user index

```
PUT userstest 
{
  "settings": {
    "analysis": {
      "analyzer": {
        "custom_lowercase": {
          "tokenizer": "whitespace",
          "filter": [
            "lowercase"
          ]
        }
      }
   },
    "number_of_shards": 2
    , "number_of_replicas": 1
  }
  , "mappings": {
        "user": { 
      "properties": { 
        "join_field": {
          "type": "join",
          "relations": {
            "user": ["following"]
          }
        },
        "dtype":{"type": "keyword"}, 
        "uid": {"type": "text", "analyzer": "custom_lowercase" }, 
        "name": { "type": "text","analyzer": "custom_lowercase" }
      }
    }
  }
}

```

I have indexes 2 users in it.

```
POST /userstest/user/vibhor?pretty
{
  "uid":"vibhor",
  "name":"vibhor",
  "join_field": "user",
  "dtype":"user"
}

POST /userstest/user/vibhorg?pretty
{
  "uid":"vibhorg",
  "name":"vibhorg",
  "join_field": "user",
  "dtype":"user"
}

```

Also , indexed the following childrens in it.

```
POST /userstest/user?routing=vibhorg
    {
      "uid":"B",
      "name":"B",
      "join_field": {
        "name": "following", 
        "parent": "vibhorg" 
      },
      "dtype":"following"
    }

POST /userstest/user?routing=vibhorg
    {
      "uid":"B",
      "name":"B",
      "join_field": {
        "name": "following", 
        "parent": "vibhorg" 
      },
      "dtype":"following"
    }

POST /userstest/user?routing=vibhorg
    {
      "uid":"A",
      "name":"A",
      "join_field": {
        "name": "following", 
        "parent": "vibhorg" 
      },
      "dtype":"following"
    }

POST /userstest/user?routing=vibhor
    {
      "uid":"A",
      "name":"A",
      "join_field": {
        "name": "following", 
        "parent": "vibhor" 
      },
      "dtype":"following"
    }

```

I ran the following search query where I need all users which have childrens having uid A or B where parent's name starts with vibhor

```
GET userstest/user/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "prefix": {
            "name": {
              "value": "vibhor"
            }
          }
        },
        {
          "term": {
            "dtype": {
              "value": "user"
            }
          }
        },
        {
				"has_child": {
					"type": "following",
					"score_mode": "sum", 
					"query": {
						"bool": {
							"should": [{
							  "match": {
							    "uid": "A"
							  }
							},{
							  "match": {
							    "uid": "B"
							  }
							}
							]
						}
					}
				}
			}
      ]
    }
  }
}

```

What I want is the number of childrens also in the output documents. Can I get it efficiently ?

---

<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:** [December 25, 2017, 12:47pm UTC](https://discuss.elastic.co/t/getting-number-of-childrens-in-the-query-when-searching-for-parent/109217/2 "2017-12-25T12:47:49Z")

</div>

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