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

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 ?

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