Count distinct by date

I am trying to count distinct date in a field so I followed the cardinality aggregation to do it but it failed to return the expected result, any suggestion on what I am doing wrong please?

{
"query": {
	"bool": {
		"filter": [ 
			{"term": { "Id": "793"} },
			 {"range": {
				"starttime": {
					"gte": "2019-08-12T11:20:00",
					"lte": "2019-08-12T11:30:00"
				}
			 } }
		]
	}
},
"aggs": {
    "sessions": {
        "terms" : { "field" : "stationid" },
        "aggs": {
            "total_minute": {
                "sum": {"script":"doc['stoptime'].date.getMillis() - doc['starttime'].date.getMillis()"}
            }
        }
    } ,
   "count_starttime": {
    	"cardinality" : 
            {"script":"doc['starttime'].date.dayOfMonth"}
  }
}

}

Presumably the correct answer is, at most,"1 day" given your query is a 10 minute window.
What answer do you get?

I think I figure out the proper way to do it. Thanks Mark for pointing out the 1 day condition in my query:

{
"query": { "term": { "username": { "value": "guest@bdtt" } } },
"aggs": {
"sessions": {
    "terms" : { "field" : "callingstationid" },
    "aggs": {
        "total_minute": {
            "sum": {"script":"doc['acctstoptime'].date.getMillis() - doc['acctstarttime'].date.getMillis()"}
        },
        "count_starttime": {
	    	"cardinality" : 
	            {"script":"doc['acctstarttime'].date.dayOfMonth"}
		}
    }
}

}
}

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