# Custom Rule using Scripted Field

**URL:** <https://discuss.elastic.co/t/custom-rule-using-scripted-field/171830>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-machine-learning\
**Created:** [March 11, 2019, 7:31pm UTC](https://discuss.elastic.co/t/custom-rule-using-scripted-field/171830 "2019-03-11T19:31:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cosruss](https://avatars.discourse-cdn.com/v4/letter/c/eb9ed0/32.png) [@cosruss](https://discuss.elastic.co/u/cosruss)\
**Post date:** [March 11, 2019, 7:31pm UTC](https://discuss.elastic.co/t/custom-rule-using-scripted-field/171830/1 "2019-03-11T19:31:17Z")

</div>

Is it possible to create an ML job with custom rule referencing a scripted field? I'm trying to skip results during a certain period of the day. Running 6.6 Here are the relevant snippets:

In the datafeed\_config section I've got this, which seems to work:  
"script\_fields": {  
"timestamp\_hour": {  
"script": {  
"lang": "painless",  
"inline": "doc['@timestamp'].value.getHourOfDay()"  
}  
}  
},

In the detectors section I've got this, which throws an error:  
"custom\_rules": [{  
"actions": ["skip\_result"],  
"conditions": [  
{  
"applies\_to": "timestamp\_hour",  
"operator": "lt",  
"value": 12  
}  
]  
}],

Error: [rule\_condition] failed to parse field [applies\_to]

---

<div class="post-metadata">

**Author:** ![richcollier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/richcollier/32/115035_2.png) [@richcollier](https://discuss.elastic.co/u/richcollier)\
**Post date:** [March 12, 2019, 12:54pm UTC](https://discuss.elastic.co/t/custom-rule-using-scripted-field/171830/2 "2019-03-12T12:54:41Z")

</div>

Why not use [calendar events](https://www.elastic.co/blog/scheduled-events-and-the-amorous-anomaly-elasticsearch-machine-learning) to ignore the timeframes that you want?

---

<div class="post-metadata">

**Author:** ![cosruss](https://avatars.discourse-cdn.com/v4/letter/c/eb9ed0/32.png) [@cosruss](https://discuss.elastic.co/u/cosruss)\
**Post date:** [March 20, 2019, 2:54pm UTC](https://discuss.elastic.co/t/custom-rule-using-scripted-field/171830/3 "2019-03-20T14:54:55Z")

</div>

It's my understanding that calendar events are for specific date/times. I'm looking for recurring daily time-of-day rules. Am I misunderstanding the calendar event?

---

<div class="post-metadata">

**Author:** ![richcollier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/richcollier/32/115035_2.png) [@richcollier](https://discuss.elastic.co/u/richcollier)\
**Post date:** [March 20, 2019, 3:12pm UTC](https://discuss.elastic.co/t/custom-rule-using-scripted-field/171830/4 "2019-03-20T15:12:55Z")

</div>

Yes, the calendar events allow you to ignore specific dates/times and there currently is no "recurring" option at the moment (would be a nice [feature request you could add](https://github.com/elastic/kibana/issues)).

However, with that said, the [calendar event does have an API](https://www.elastic.co/guide/en/elasticsearch/reference/6.6/ml-post-calendar-event.html), you can programmatically add many at once or one per day with a scheduled script/cron job

Just a thought...

---

<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:** [April 17, 2019, 3:12pm UTC](https://discuss.elastic.co/t/custom-rule-using-scripted-field/171830/5 "2019-04-17T15:12:58Z")

</div>

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