For your custom purpose I think you can specify a custom pattern analyzer on the filed and take the terms aggregation of field. An example is as follows :
Define your custom analyzer :
PUT /test_index
{
"settings": {
"analysis": {
"analyzer": {
"nonword": {
"type": "pattern",
"pattern": "/"
}
}
}
}
}
Create mapping :
POST /test_index/_mapping/test_1
{
"properties": {
"dir": {
"type": "string",
"index": "analyzed",
"analyzer": "nonword",
"fields": {
"un_touched": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
Note : 'un_touched' field is kept for holding the original version of the data.
Populate data and perform the aggregation :
GET /test_index/test_1/_search
{
"aggs": {
"my_agg": {
"terms": {
"field": "dir",
"size": 0
}
}
}
}
Note : This is only a minimal example and you should really care about the pattern;