# Elastic Search to return different counts along with Search results

**URL:** <https://discuss.elastic.co/t/elastic-search-to-return-different-counts-along-with-search-results/305358>\
**Category:** Elasticsearch\
**Created:** [May 22, 2022, 6:07pm UTC](https://discuss.elastic.co/t/elastic-search-to-return-different-counts-along-with-search-results/305358 "2022-05-22T18:07:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![srikanthkadem](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/srikanthkadem/32/40018_2.png) [@srikanthkadem](https://discuss.elastic.co/u/srikanthkadem)\
**Post date:** [May 22, 2022, 6:07pm UTC](https://discuss.elastic.co/t/elastic-search-to-return-different-counts-along-with-search-results/305358/1 "2022-05-22T18:07:40Z")

</div>

Hi ,  
I have a requirement to get the differnt counts based on status field in my document for given search critiria. For e.g If I am searching with Comic keyword , I generally get 10 documents(default size) with TotalHits as 900 . Similar to total hits as need fetch different counts with in search query like number of active(status field value) documents, number of cancelled documents etc,  
can you please help me to write query for this. I tried aggegrations but it is giving total active count instead of matching records .

Regards,  
Srikanth

---

<div class="post-metadata">

**Author:** ![srikanthkadem](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/srikanthkadem/32/40018_2.png) [@srikanthkadem](https://discuss.elastic.co/u/srikanthkadem)\
**Post date:** [May 23, 2022, 9:33am UTC](https://discuss.elastic.co/t/elastic-search-to-return-different-counts-along-with-search-results/305358/2 "2022-05-23T09:33:40Z")

</div>

Hi All-  
Please help me on this.

---

<div class="post-metadata">

**Author:** ![anime\_lover](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anime_lover/32/100265_2.png) [@anime\_lover](https://discuss.elastic.co/u/anime_lover)\
**Post date:** [May 24, 2022, 11:57am UTC](https://discuss.elastic.co/t/elastic-search-to-return-different-counts-along-with-search-results/305358/3 "2022-05-24T11:57:25Z")

</div>

hi there ,  
can you provide sample query along with sample data.It will help us in understanding in better way.

---

<div class="post-metadata">

**Author:** ![srikanthkadem](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/srikanthkadem/32/40018_2.png) [@srikanthkadem](https://discuss.elastic.co/u/srikanthkadem)\
**Post date:** [May 24, 2022, 1:39pm UTC](https://discuss.elastic.co/t/elastic-search-to-return-different-counts-along-with-search-results/305358/4 "2022-05-24T13:39:22Z")

</div>

Hi Anime\_lover ,  
thanks for the response .  
Here is the sample data for explanation.  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/a/9/a963e5c407bcdaea3b760ca51cd80e9de45da95e.png)

if I search with keyword india , I get 3 records . Along with records I want count of Active status jobs, cancelled status jobs, overdue which are true , overdue which are false for my given search condition (i.e india).

---

<div class="post-metadata">

**Author:** ![anime\_lover](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anime_lover/32/100265_2.png) [@anime\_lover](https://discuss.elastic.co/u/anime_lover)\
**Post date:** [June 6, 2022, 5:18am UTC](https://discuss.elastic.co/t/elastic-search-to-return-different-counts-along-with-search-results/305358/5 "2022-06-06T05:18:45Z")

</div>

Hi there ,  
Sorry for late response.

Here for your information :  
my records are as below:

```auto
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 5,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "sample",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "jobId" : 1,
          "title" : "first title",
          "country" : "nepal",
          "status" : "active",
          "overDue" : "true"
        }
      },
      {
        "_index" : "sample",
        "_id" : "YupBN4EBlAA2g3ulx4eO",
        "_score" : 1.0,
        "_source" : {
          "jobId" : 2,
          "title" : "second title",
          "country" : "india",
          "status" : "active",
          "overDue" : "true"
        }
      },
      {
        "_index" : "sample",
        "_id" : "Y-pCN4EBlAA2g3ulJofl",
        "_score" : 1.0,
        "_source" : {
          "jobId" : 3,
          "title" : "third title",
          "country" : "india",
          "status" : "active",
          "overDue" : "false"
        }
      },
      {
        "_index" : "sample",
        "_id" : "ZOpCN4EBlAA2g3ulx4cQ",
        "_score" : 1.0,
        "_source" : {
          "jobId" : 5,
          "title" : "fifth title",
          "country" : "india",
          "status" : "cancelled",
          "overDue" : "true"
        }
      },
      {
        "_index" : "sample",
        "_id" : "DzRCN4EB43mQOuzybfUU",
        "_score" : 1.0,
        "_source" : {
          "jobId" : 4,
          "title" : "fourth title",
          "country" : "india",
          "status" : "cancelled",
          "overDue" : "false"
        }
      }
    ]
  }
}

```

- so as per my view here you need count for active ,cancelled status count and overDue count

so my solution is as below:

```auto
GET /sample/_search
{
  "size": 1, 
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "country": "india"
          }
        }
      ]
    }
  },
  "aggs": {
    "status": {
      "terms": {
        "field": "status.keyword",
        "size": 10
      }
    },
    "overDue": {
      "terms": {
        "field": "overDue.keyword",
        "size": 10
      }
    }
  }
}

```

Output will be like

```auto
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 0.20763937,
    "hits" : [
      {
        "_index" : "sample",
        "_id" : "YupBN4EBlAA2g3ulx4eO",
        "_score" : 0.20763937,
        "_source" : {
          "jobId" : 2,
          "title" : "second title",
          "country" : "india",
          "status" : "active",
          "overDue" : "true"
        }
      }
    ]
  },
  "aggregations" : {
    "overDue" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "false",
          "doc_count" : 2
        },
        {
          "key" : "true",
          "doc_count" : 2
        }
      ]
    },
    "status" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "active",
          "doc_count" : 2
        },
        {
          "key" : "cancelled",
          "doc_count" : 2
        }
      ]
    }
  }
}

```

i hope this help you out if not feel free to replay.  
Thank you

---

<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 4, 2022, 5:19am UTC](https://discuss.elastic.co/t/elastic-search-to-return-different-counts-along-with-search-results/305358/6 "2022-07-04T05:19:19Z")

</div>

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