Pipeline Aggregation output not as expected

This is what i want to achieve. I want to get the number of times employees have visited given state/country. Following is the data present as format EmployeeId, state/country

E1, France
E2, France
E2, France
E3, France
E3, France
E4, France

Desired output ( when search for state:France )
2 employee 2 times
2 employee 1 times

Following is my query:

POST _search
{
  "query": {
    "bool": {
      "must":
        {
          "match": {
            "country": "France"
          }
        }
    }
  },
  "aggs": {
    "employees": {
      "terms": {
        "field": "employeeId",
        "size": "10"
      },
      "aggs": {
        "countVisited": {
          "value_count": {
            "field": "employeeId"
          }
        }
      }
    },
    "types_count": {
      "cardinality": {
        "field": "countVisited"
      }
    }
  }
}

But this is just returning per employee count i.e
E1, 1
E2, 2
E3, 2
E4, 1

Any suggestions how can i achieve the needed output ?

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