# How to group and return counts

**URL:** <https://discuss.elastic.co/t/how-to-group-and-return-counts/63867>\
**Category:** Elasticsearch\
**Created:** [October 25, 2016, 12:15pm UTC](https://discuss.elastic.co/t/how-to-group-and-return-counts/63867 "2016-10-25T12:15:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![paul2](https://avatars.discourse-cdn.com/v4/letter/p/df705f/32.png) [@paul2](https://discuss.elastic.co/u/paul2)\
**Post date:** [October 25, 2016, 12:15pm UTC](https://discuss.elastic.co/t/how-to-group-and-return-counts/63867/1 "2016-10-25T12:15:36Z")

</div>

Hi, I'm using a field, transactiontype, to return the count of each transaction type. The following code works but returns both the full number of hits and the counts.

Does anyone know what I do to return counts only? Maybe it's because I'm using \_search in the url. I tried \_count but that didn't work.

```auto
**{**
 **"query": {**
 **"match": {**
 **"MAJORPOSTCODE": "WC1R,WC1A,WC1V,WC1B,WC1N,WC1X,WC1E,WC1H"**
 **}**
 **	},**
 **"aggs" : {**
 **"Transactiontypes" : { "terms" : { "field" : "TRANSACTIONTYPE" } }**
 **}**
 **}**

```

**Ideal Reponse**

```auto
 "aggregations": {
    "count_by_type": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "auction",
          "doc_count": 43
        },
        {
          "key": "lease",
          "doc_count": 39
        },

```

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [October 25, 2016, 1:01pm UTC](https://discuss.elastic.co/t/how-to-group-and-return-counts/63867/2 "2016-10-25T13:01:03Z")

</div>

Tip: wrap things in ``` so they are formatted. I edited your post with my godlike admin powers so I could read it. The `**` that you added to the query don't really help with readability either.

> [@paul2](#):
>
> Maybe it's because I'm using \_search in the url.

That shouldn't matter.

> [@paul2](#):
>
> The following code works but returns both the full number of hits and the counts.

You can use `size=0` to remove all of the hits from the response. You can use [response filtering](https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#_response_filtering) to further limit the response.

When people ask questions like this I worry that they are exposing Elasticsearch directly over an untrusted connection. That isn't a great idea because it is fairly easy to craft a query that puts a super large load on Elasticsearch. And if you've really misconfigured Elasticsearch you can get it to run arbitrary commands for you as the Elasticsearch user. I only bring it up because it is a problem waiting to happen that you might have.

---

<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, 2017, 10:09pm UTC](https://discuss.elastic.co/t/how-to-group-and-return-counts/63867/3 "2017-07-05T22:09:59Z")

</div>


