Aggregation: order by on count of filtered field


(Sumit Sontakke) #1

How can I get result ordered by based on count of filtered field. Here I have filtered transactionID for Error Codes and I want result set in desc order of count of errors.

Query:

GET index-name/_search
{
"aggs" : {
"TransID" : {
"terms" : {
"field" : "transID.keyword"
, "order": {
"_key": "desc"
}
},
"aggs" : {
"ErrorCode" : {
"filter" : { "term" : { "ErrorCode.keyword": "Error-2345" }}
}
}
}
}
}

In response I get aggregations randomly, (not in order by values)
I want result set to be sorted in desc order for doc_count of errorCode

"aggregations": {

"TransID": { 

  "doc_count_error_upper_bound": 0, 

  "sum_other_doc_count": 11401660, 

  "buckets": [ 

    { 

      "key": "transactionID-***", 

      "doc_count": 168, 

      "ErrorCode": { 

        "doc_count": 2 

      } 

    }, 

    { 

      "key": "transactionID-***", 

      "doc_count": 246, 

      "ErrorCode": { 

        "doc_count": 20 

      } 

    }, 

    { 

      "key": "transactionID-***", 

      "doc_count": 145, 

      "ErrorCode": { 

        "doc_count": 0 

      } 

    }, 

    { 

      "key": "transactionID-***", 

      "doc_count": 83, 

      "ErrorCode": { 

        "doc_count": 0 

      } 

    }, 

    { 

      "key": "transactionID-***", 

      "doc_count": 103, 

      "ErrorCode": { 

        "doc_count": 0 

      } 

    }, 

    { 

      "key": "transactionID-***", 

      "doc_count": 104, 

      "ErrorCode": { 

        "doc_count": 0 

      } 

    }, 

    { 

      "key": "transactionID-***", 

      "doc_count": 134, 

      "ErrorCode": { 

        "doc_count": 0 

      } 

    }, 

    { 

      "key": "transactionID-***", 

      "doc_count": 214, 

      "ErrorCode": { 

        "doc_count": 0 

      } 

    }, 

    { 

      "key": "transactionID-***", 

      "doc_count": 115, 

      "ErrorCode": { 

        "doc_count": 0 

      } 

    }, 

    { 

      "key": "transactionID-***", 

      "doc_count": 115, 

      "ErrorCode": { 

        "doc_count": 0 

      } 

    } 

  ] 

} 

}

}

Thanks


(Sumit Sontakke) #2

Awaiting for any assistance....


(Marius Dragomir) #3

Hello,
This is an Elasticsearch questions, so you should ask in that forum sub-section about query building.


(system) #4

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