user10091301
user10091301

Reputation:

Elastic search Group by count for particular field

I have a elastic search index with following documents

    {
       "id":1
      "mainid  ": "497940311988134801282012-04-10  ",

    }

  {
       "id":2
      "mainid  ": "497940311988134801282012-04-10  ",

    }

I am looking to have a query similar like -example mysql table

id    mainid                                       
1     497940311988134801282012-04-10                

2     497940311988134801282012-04-10                
3     497940311988134801282012-04-10              
4     something different





select id ,mainid ,count(mainid) as county  from wfcharges group by mainid,id having county>1;

in elastic search ,as there is no count aggregate function is available in elastic .I am stuck here.This is what ,I have tried. Any suggestions or online resources.Thanks

GET /wfcharges/_search
{
    "aggs" : {
        "countfield" : {
            "count" : { "field" : "mainid" }
        }
    }
}

Upvotes: 0

Views: 2000

Answers (2)

Evaldas Buinauskas
Evaldas Buinauskas

Reputation: 14077

This query is going to be exactly what you need:

GET /wfcharges/_search
{
  "aggs": {
    "countfield": {
      "terms": {
        "field": "mainid",
        "min_doc_count": 2
      }
    }
  }
}

It's going to aggregate by mainid field and tell that minimum document count for this bucket has to be 2 ( more than 1):

Upvotes: 0

Polynomial Proton
Polynomial Proton

Reputation: 5135

I think you'd want to use the terms aggregation. This will group by similar terms and return a count of each term. Look at the linked url for example.

In you case, it would look like this:

GET /wfcharges/_search
{
    "aggs" : {
        "countfield" : {
            "terms" : { "field" : "mainid" }
        }
    }
}

Upvotes: 1

Related Questions