How to Achieving count(distinct(doc['userId'])) with facets script

{
"query" : {
"match_all" : { }
},
"facets" : {
"stats" : {
"terms_stats" : {
"key_field" : "tag",
"value_script" : "count(distinct(doc['num1'].value))"
}
}
}
}