Elastic Search to return different counts along with Search results

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

Hi All-
Please help me on this.

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

Hi Anime_lover ,
thanks for the response .
Here is the sample data for explanation.
image

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).

Hi there ,
Sorry for late response.

Here for your information :
my records are as below:

{
  "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:

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

{
  "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

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