puneet_0303
puneet_0303

Reputation: 197

Aggregating multiple values of single key into a single bucket elasticsearch

I have a elastic search index with following mapping

 {
  "probe_alert" : {
    "mappings" : {
      "alert" : {
        "properties" : {
          "id" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "probeChannelId" : {
            "type" : "long"
          },
          "severity" : {
            "type" : "integer"
          },
        }
      }
    }
  }
}

Sample indexed data : For each channel index has a severity value

[
      {
        "_index" : "probe_alert",
        "_type" : "alert",
        "_id" : "b_cu0nYB8EMvknGcmMxk",
        "_score" : 0.0,
        "_source" : {
          "id" : "b_cu0nYB8EMvknGcmMxk",
          "probeChannelId" : 15,
          "severity" : 2,
        }
      },
      {
        "_index" : "probe_alert",
        "_type" : "alert",
        "_id" : "b_cu0nYB8EMvknGcmMxk",
        "_score" : 0.0,
        "_source" : {
          "id" : "b_cu0nYB8EMvknGcmMxk",
          "probeChannelId" : 17,
          "severity" : 5,
        }
      },
      {
        "_index" : "probe_alert",
        "_type" : "alert",
        "_id" : "b_cu0nYB8EMvknGcmMxk",
        "_score" : 0.0,
        "_source" : {
          "id" : "b_cu0nYB8EMvknGcmMxk",
          "probeChannelId" : 18,
          "severity" : 10,
        }
      },
      {
        "_index" : "probe_alert",
        "_type" : "alert",
        "_id" : "b_cu0nYB8EMvknGcmMxk",
        "_score" : 0.0,
        "_source" : {
          "id" : "b_cu0nYB8EMvknGcmMxk",
          "probeChannelId" : 19,
          "severity" : 5,
        }
      },
      {
        "_index" : "probe_alert",
        "_type" : "alert",
        "_id" : "b_cu0nYB8EMvknGcmMxk",
        "_score" : 0.0,
        "_source" : {
          "id" : "b_cu0nYB8EMvknGcmMxk",
          "probeChannelId" :20,
          "severity" : 10,
        }
      }
    ]

I have done terms aggregation for fetching max severity value for a single probeChannelId but now I want to aggregate on multiple values of probeChannelId and get max value of severity. Expected Result :

"aggregations" : {
    "aggs_by_channels" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : [15,17],
          "doc_count" : 1,
          "aggs_by_severity" : {
            "value" : 5.0
          }
        },
        {
          "key" : [18,19,20],
          "doc_count" : 1,
          "aggs_by_severity" : {
            "value" : 10.0
          }
        }
      ]
    }
  }

In response i want group of values probeChannelId to have highest severity value

Upvotes: 0

Views: 559

Answers (1)

Bhavya
Bhavya

Reputation: 16172

If you want to get the highest severity value, among a set of documents, then you can try out the below query using the Adjacency matrix aggregation

Search Query:

{
  "size": 0,
  "aggs": {
    "interactions": {
      "adjacency_matrix": {
        "filters": {
          "[15,17]": {
            "terms": {
              "probeChannelId": [
                15,
                17
              ]
            }
          },
          "[18,19,20]": {
            "terms": {
              "probeChannelId": [
                18,
                19,
                20
              ]
            }
          }
        }
      },
      "aggs": {
        "max_severity": {
          "max": {
            "field": "severity"
          }
        }
      }
    }
  }
}

Search Result:

"aggregations": {
    "interactions": {
      "buckets": [
        {
          "key": "[15,17]",
          "doc_count": 2,
          "max_severity": {
            "value": 5.0           // note this
          }
        },
        {
          "key": "[18,19,20]",
          "doc_count": 3,
          "max_severity": {
            "value": 10.0        // note this
          }
        }
      ]
    }

Upvotes: 1

Related Questions