Aggregate on 2 unique fields

Hi all,
I have a doc with multiple fields (assume 10)
I want to preform an aggregation based on 2 fields of these documents. only if those fields are identical add them to the aggs.

Im trying to use a scripted agg like so:

{
  "size": 0, 
    "aggs" : {
        "blabla" : {
            "terms" : {
                "script" : "doc['solan_type'].value+doc['solan_device'].value"
            }
        }
    }
}

but getting an error for painless syntax

{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "runtime error",
        "script_stack": [
          "doc['solan_type'].value+doc['solan_device'].value",
          "                                           ^---- HERE"
        ],
        "script": "doc['solan_type'].value+doc['solan_device'].value",
        "lang": "painless"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query_fetch",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "main_solan",
        "node": "DpIfGrgeQm-RE34WMASY_A",
        "reason": {
          "type": "script_exception",
          "reason": "runtime error",
          "caused_by": {
            "type": "null_pointer_exception",
            "reason": null
          },
          "script_stack": [
            "doc['solan_type'].value+doc['solan_device'].value",
            "                                           ^---- HERE"
          ],
          "script": "doc['solan_type'].value+doc['solan_device'].value",
          "lang": "painless"
        }
      }
    ],
    "caused_by": {
      "type": "script_exception",
      "reason": "runtime error",
      "caused_by": {
        "type": "null_pointer_exception",
        "reason": null
      },
      "script_stack": [
        "doc['solan_type'].value+doc['solan_device'].value",
        "                                           ^---- HERE"
      ],
      "script": "doc['solan_type'].value+doc['solan_device'].value",
      "lang": "painless"
    }
  },
  "status": 500
}

anyone knows how to preform this?
Thanks!

You may have to include null checks in your script before doing the concatenation. If one or more of the fields you are attempting to access doesn't have a value, it will return null which then throws the NPE when trying to concatenate.

(I think that's what's going on here).

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