# Aggregate on 2 unique fields

**URL:** <https://discuss.elastic.co/t/aggregate-on-2-unique-fields/93414>\
**Category:** Elasticsearch\
**Created:** [July 17, 2017, 3:12pm UTC](https://discuss.elastic.co/t/aggregate-on-2-unique-fields/93414 "2017-07-17T15:12:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sharongur](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@sharongur](https://discuss.elastic.co/u/sharongur)\
**Post date:** [July 17, 2017, 3:12pm UTC](https://discuss.elastic.co/t/aggregate-on-2-unique-fields/93414/1 "2017-07-17T15:12:22Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [July 21, 2017, 2:19pm UTC](https://discuss.elastic.co/t/aggregate-on-2-unique-fields/93414/2 "2017-07-21T14:19:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 18, 2017, 2:20pm UTC](https://discuss.elastic.co/t/aggregate-on-2-unique-fields/93414/3 "2017-08-18T14:20:05Z")

</div>

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