You’re almost there, you simply need to add a terms aggregation to your query
{
"query" : {
"bool" : {
"should" : [
{ "term" : { "userid" : "20" } },
{ "term" : { "activity" : "9" } }
]
}
},
"aggs":{
"unique_ids": {
"terms": {
"field": "id"
}
}
}
}