# Pipeline Aggregation output not as expected

**URL:** <https://discuss.elastic.co/t/pipeline-aggregation-output-not-as-expected/174460>\
**Category:** Elasticsearch\
**Created:** [March 29, 2019, 4:00am UTC](https://discuss.elastic.co/t/pipeline-aggregation-output-not-as-expected/174460 "2019-03-29T04:00:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xephonia](https://avatars.discourse-cdn.com/v4/letter/x/b782af/32.png) [@Xephonia](https://discuss.elastic.co/u/Xephonia)\
**Post date:** [March 29, 2019, 4:00am UTC](https://discuss.elastic.co/t/pipeline-aggregation-output-not-as-expected/174460/1 "2019-03-29T04:00:14Z")

</div>

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:

```auto
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 ?

---

<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:** [April 26, 2019, 4:00am UTC](https://discuss.elastic.co/t/pipeline-aggregation-output-not-as-expected/174460/2 "2019-04-26T04:00:15Z")

</div>

Closed on April 26, 2019, 4:00am UTC

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