# Group by Count equals value

**URL:** <https://discuss.elastic.co/t/group-by-count-equals-value/301161>\
**Category:** Elasticsearch\
**Created:** [March 31, 2022, 6:18am UTC](https://discuss.elastic.co/t/group-by-count-equals-value/301161 "2022-03-31T06:18:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chivas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chivas/32/58800_2.png) [@chivas](https://discuss.elastic.co/u/chivas)\
**Post date:** [March 31, 2022, 6:18am UTC](https://discuss.elastic.co/t/group-by-count-equals-value/301161/1 "2022-03-31T06:18:00Z")

</div>

Hello,  
I am using below query to group by a field and return the count, is there a way to return documents whose doc\_count equals 1 (or less than 2)

```auto
GET logs-test*/_search
{
  "aggs": {
    "2": {
      "terms": {
        "field": "id",
        "order": {
          "_count": "asc"
        },
        "min_doc_count": 2
      }
    }
  },
  "size": 0,
  "query": {
    "bool": {
      "must": [],
      "filter": [
        {
          "match_phrase": {
            "body.function": "myfunctionName"
          }
        },
        {
          "range": {
            "timestamp": {
              "gte": "2022-03-30T17:00:00.000Z",
              "lte": "2022-03-31T16:59:59.999Z"
            }
          }
        }
      ]
    }
  }
}

```

Thanks!

---

<div class="post-metadata">

**Author:** ![chivas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chivas/32/58800_2.png) [@chivas](https://discuss.elastic.co/u/chivas)\
**Post date:** [March 31, 2022, 7:04am UTC](https://discuss.elastic.co/t/group-by-count-equals-value/301161/2 "2022-03-31T07:04:09Z")

</div>

Ok, managed to solve it using below

```auto
GET logs-test*/_search
{
  "aggs": {
    "id_count": {
      "terms": {
        "field": "id"
      },
         "aggs": {
            "my_filter": {
               "bucket_selector": {
                  "buckets_path": {
                     "the_doc_count": "_count"
                  },
                  "script": "params.the_doc_count == 1"
               }
            }
         }
    }
  },
  "size": 0,
  "query": {
    "bool": {
      "filter": [
        {
          "match_phrase": {
            "body.function": "myfunctionName"
          }
        },
        {
          "range": {
            "timestamp": {
              "format": "strict_date_optional_time",
              "gte": "2022-03-30T17:00:00.000Z",
              "lte": "2022-03-31T16:59:59.999Z"
            }
          }
        }
      ]
    }
  }
}

```

---

<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 28, 2022, 7:04am UTC](https://discuss.elastic.co/t/group-by-count-equals-value/301161/3 "2022-04-28T07:04:50Z")

</div>

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