# Access Nested Objects using Painless for Alerting to Create Trigger Condition

**URL:** <https://discuss.elastic.co/t/access-nested-objects-using-painless-for-alerting-to-create-trigger-condition/308500>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-alerting, painless\
**Created:** [June 29, 2022, 4:26pm UTC](https://discuss.elastic.co/t/access-nested-objects-using-painless-for-alerting-to-create-trigger-condition/308500 "2022-06-29T16:26:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Divyank\_Mahalle](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/divyank_mahalle/32/91240_2.png) [@Divyank\_Mahalle](https://discuss.elastic.co/u/Divyank_Mahalle)\
**Post date:** [June 29, 2022, 4:26pm UTC](https://discuss.elastic.co/t/access-nested-objects-using-painless-for-alerting-to-create-trigger-condition/308500/1 "2022-06-29T16:26:40Z")

</div>

I tried creating Trigger condition in Monitor.I was trying to extract fields present in `_source` which is giving error `"reason" : "Illegal list shortcut value [_source]."`.

DSL Extraction Query-

```auto
{
    "size": 1,
    "sort": {"Date" : "desc" },
    "query": {
        "bool": {
            "filter": [
                {
                    "range": {
                        "Date": {
                            "from": "{{period_end}}||-3d",
                            "to": "{{period_end}}",
                            "include_lower": true,
                            "include_upper": true,
                            "format": "epoch_millis",
                            "boost": 1
                        }
                    }
                }
            ],
            "adjust_pure_negative": true,
            "boost": 1
        }
    },
    "aggregations": {
        "metric": {
            "avg": {
                "field": "difference"
            }
        }
    }
}

```

Output-

```auto
{
    "_shards": {
        "total": 4,
        "failed": 0,
        "successful": 4,
        "skipped": 2
    },
    "hits": {
        "hits": [
            {
                "_index": "reliance_cal_spread",
                "_source": {
                    "symbol": "RELIANCE",
                    "std_dev": 2.4,
                    "upper_range": 10.3,
                    "current_month_close": 2536.3,
                    "mean": 7.9,
                    "weekday": "Tuesday",
                    "difference": 3,
                    "Date": "2022-06-28T00:00:00",
                    "next_month_close": 2539.3,
                    "lower_range": 5.5
                },
                "_id": "E2-cqoEB0vMP3J-sGCIB",
                "sort": [
                    1656374400000
                ],
                "_score": null
            }
        ],
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": null
    },
    "took": 8,
    "timed_out": false,
    "aggregations": {
        "metric": {
            "value": 2.799999952316284
        }
    }
}

```

Trigger Condition-

```auto
return ctx.results[0].aggregations.metric.value == null ? false :ctx.results[0].hits.hits._source.difference > 6

```

Error-

```auto
{
  "type" : "script_exception",
  "reason" : "runtime error",
  "script_stack" : [
    "return ctx.results[0].aggregations.metric.value == null ? false :ctx.results[0].hits.hits._source.difference > 6",
    " ^---- HERE"
  ],
  "script" : "return ctx.results[0].aggregations.metric.value == null ? false :ctx.results[0].hits.hits._source.difference > 6",
  "lang" : "painless",
  "position" : {
    "offset" : 89,
    "start" : 0,
    "end" : 112
  },
  "caused_by" : {
    "type" : "illegal_argument_exception",
    "reason" : "Illegal list shortcut value [_source]."
  }
}

```

While I am able to extract other conditions such as-`aggregations.metric.value` and `hits.total.value`.

Trigger Condition

```auto
return ctx.results[0].aggregations.metric.value == null ? false :ctx.results[0].aggregations.metric.value < 6

```

Trigger Condition Response-

```auto
True

```

---

<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:** [July 27, 2022, 4:27pm UTC](https://discuss.elastic.co/t/access-nested-objects-using-painless-for-alerting-to-create-trigger-condition/308500/3 "2022-07-27T16:27:37Z")

</div>

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