Hello,
I have the following watcher alert that calculates the ratio of orders/searches for each of my customers and returns it with an email.
{
"trigger": {
"schedule": {
"cron": "0 0/2 * 1/1 * ? *"
}
},
"input": {
"search": {
"request": {
"search_type": "query_then_fetch",
"indices": [
"webshop-events-*"
],
"rest_total_hits_as_int": true,
"body": {
"size": 0,
"query": {
"bool": {
"must": [],
"filter": [
{
"match_all": {}
},
{
"range": {
"@timestamp": {
"from": "now-2400h",
"to": "now"
}
}
}
],
"should": [],
"must_not": [
{
"bool": {
"should": [
{
"match_phrase": {
"customer_nr": "1234"
}
},
{
"match_phrase": {
"customer_nr": "471118"
}
},
{
"match_phrase": {
"customer_nr": "4409412"
}
},
{
"match_phrase": {
"customer_nr": "5136291"
}
},
{
"match_phrase": {
"customer_nr": "9208797"
}
},
{
"match_phrase": {
"customer_nr": "471696"
}
},
{
"match_phrase": {
"customer_nr": "4409849"
}
}
],
"minimum_should_match": 1
}
}
]
}
},
"aggs": {
"convratepercustomeraccount": {
"terms": {
"field": "customer_nr",
"size": 10000
},
"aggs": {
"total_orders": {
"filter": {
"term": {
"order_type.keyword": "ORDER"
}
}
},
"total_searches": {
"filter": {
"terms": {
"event_type.keyword": [
"fulltextsearch",
"fahrzeugsuche",
"teilesuche"
]
}
}
},
"output": {
"bucket_script": {
"buckets_path": {
"totalorders": "total_orders._count",
"totalsearches": "total_searches._count"
},
"script": "(params.totalorders / params.totalsearches) * 100"
}
},
"ratio_bucket_sort": {
"bucket_sort": {
"size": 20,
"sort": [
{
"output": {
"order": "desc"
}
}
]
}
}
}
}
}
}
}
}
},
"condition": {
"script": {
"source": "return ctx.payload.aggs.convratepercustomeraccount.buckets.total_orders.doc_count > params.threshold",
"lang": "painless",
"params": {
"threshold": 5
}
}
},
"actions": {
"send_email": {
"email": {
"profile": "standard",
"to": [
"alexandros.ananikidis@sag-ag.ch"
],
"subject": "Customer Conversion rate",
"body": {
"html": "<b>Conversion Rate per Customer:</b> <ul>{{#ctx.payload.aggregations.convratepercustomeraccount.buckets}}<li><b>Customer:</b> {{key}} <b>Rate:</b> {{output.value}}%</li>{{/ctx.payload.aggregations.convratepercustomeraccount.buckets}}</ul>"
}
}
}
}
}
It works fine except the part where i put the condition section which is the following:
"condition": {
"script": {
"source": "return ctx.payload.aggs.convratepercustomeraccount.buckets.total_orders.doc_count > params.threshold",
"lang": "painless",
"params": {
"threshold": 5
}
}
}
The error that i get is the following because of my condition:
"exception": {
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"return ctx.payload.aggs.convratepercustomeraccount.buckets.total_orders.doc_count > params.threshold",
" ^---- HERE"
],
"script": "return ctx.payload.aggs.convratepercustomeraccount.buckets.total_orders.doc_count > params.threshold",
"lang": "painless",
"position": {
"offset": 23,
"start": 0,
"end": 100
},
"caused_by": {
"type": "null_pointer_exception",
"reason": "Cannot invoke \"Object.getClass()\" because \"callArgs[0]\" is null",
How can i change my condition so i can make it access that nested field without throwing an exception?