@apaws06 You can use a script to achieve this.
I would recommend to run this in devtools first and ensure that you are getting count_logins.value >0 in some of the buckets (assuming you have these anomalies already in the data set, otherwise test this on a sample set first).
POST logins-demo/_search
{
"size": 0,
"query": {
"bool": {
"filter": [
{ "wildcard": { "username": "*adm" } },
{
"script": {
"script": {
"lang": "painless",
"source": "def h = doc['@timestamp'].value.getHour(); h >= 22 || h < 11"
}
}
},
{ "range": { "@timestamp": { "gte": "now-2d", "lt": "now" } } }
]
}
},
"aggs": {
"buckets": {
"date_histogram": { "field": "@timestamp", "fixed_interval": "5m" },
"aggs": {
"count_logins": { "value_count": { "field": "username" } } // <-- use "username" here
}
}
}
}
You should see something like this:
"hits": {
"total": {
"value": 6,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"buckets": {
"buckets": [
{
"key_as_string": "2025-10-27T22:15:00.000Z",
"key": 1761603300000,
"doc_count": 2,
"count_logins": {
"value": 2
}
},
{
"key_as_string": "2025-10-27T22:20:00.000Z",
"key": 1761603600000,
"doc_count": 0,
"count_logins": {
"value": 0
}
},
If you are not getting any hits, this could be a result of a mapping issue.
In my testing the final detector in .opendistro-anomaly-detectors index (accessible using admin cert and key) looks like this:
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : ".opendistro-anomaly-detectors",
"_id" : "VdSNKpoB7iQw7zyGj4oT",
"_score" : 1.0,
"_source" : {
"name" : "detector1",
"description" : "",
"time_field" : "@timestamp",
"indices" : [
"logins-demo"
],
"filter_query" : {
"bool" : {
"filter" : [
{
"bool" : {
"filter" : [
{
"wildcard" : {
"username" : {
"wildcard" : "*adm",
"boost" : 1.0
}
}
},
{
"script" : {
"script" : {
"source" : "def h = doc['@timestamp'].value.getHour(); h >= 22 || h < 11",
"lang" : "painless"
},
"boost" : 1.0
}
}
],
"adjust_pure_negative" : true,
"boost" : 1.0
}
}
],
"adjust_pure_negative" : true,
"boost" : 1.0
}
},
"window_delay" : {
"period" : {
"interval" : 1,
"unit" : "Minutes"
}
},
"shingle_size" : 1,
"schema_version" : 0,
"feature_attributes" : [
{
"feature_id" : "UtSNKpoB7iQw7zyGjorR",
"feature_name" : "feature1",
"feature_enabled" : true,
"aggregation_query" : {
"count_logins" : {
"value_count" : {
"field" : "username"
}
}
}
}
],
"recency_emphasis" : 2560,
"history" : 40,
"ui_metadata" : {
"features" : {
"feature1" : {
"featureType" : "custom_aggs"
}
},
"filters" : [
{
"query" : "{\n \"bool\": {\n \"filter\": [\n { \"wildcard\": { \"username\": \"*adm\" } },\n {\n \"script\": {\n \"script\": {\n \"source\": \"def h = doc['@timestamp'].value.getHour(); h >= 22 || h < 11\",\n \"lang\": \"painless\"\n }\n }\n }\n ]\n }\n}",
"label" : "",
"filterType" : "custom_filter",
"fieldInfo" : [ ],
"fieldValue" : "",
"operator" : "is"
}
]
},
"last_update_time" : 1761651057589,
"user" : {
"name" : "admin",
"backend_roles" : [
"admin"
],
"roles" : [
"security_rest_api_access",
"all_access"
],
"custom_attribute_names" : [ ],
"user_requested_tenant" : "__user__",
"user_requested_tenant_access" : "NONE"
},
"detection_interval" : {
"period" : {
"interval" : 10,
"unit" : "Minutes"
}
},
"detector_type" : "SINGLE_ENTITY",
"rules" : [
{
"action" : "IGNORE_ANOMALY",
"conditions" : [
{
"feature_name" : "feature1",
"threshold_type" : "ACTUAL_OVER_EXPECTED_RATIO",
"operator" : "LTE",
"value" : 0.2
}
]
},
{
"action" : "IGNORE_ANOMALY",
"conditions" : [
{
"feature_name" : "feature1",
"threshold_type" : "EXPECTED_OVER_ACTUAL_RATIO",
"operator" : "LTE",
"value" : 0.2
}
]
}
]
}
}
]
}
}