How do I use Elasticsearch aggregation using python elasticsearch_dsl library?

I have a python model that creates the index as

class Telemetry(DocType):
    id = Keyword()
    imei = Keyword()
    location = field.GeoPoint()
    timestamp = Date()

I would like to query distinct count of imei. What I have done so far is, but this does not give any output.

def get_telemetry_for_license():
    docs = Telemetry.search().aggs.metric('imei_count', 'cardinality', field='imei')
    return imei_count

Any help on the above i appreciate.

I have also tried other way but it loads default 10,000 docs only and my docs size is few millions. If I increase default size my elasticsearch service does not response. Here is the code for other effort.

def get_telemetry_for_license():
    docs = Telemetry.search().source(exclude['location_shape']).extra(size=10000)
    docs = [doc.__dict__['_d_'] for doc in docs]
    imei =[]
    imei_count=0
    for doc in docs:
        if doc.get("imei") is not None:
            if doc['imei'] not in imei:
                imei_count += 1
                imei.append(doc['imei'])

    return imei_count

I would be grateful should you help me in any of my effort.

Thanks
Raf.

From reading the python docs it seems that you do not execute the request in the get_telemetry_for_license() method, so you are still dealing with the request object?

See Search DSL — Elasticsearch DSL 7.2.0 documentation

Let me check after newly Elasticsearch issue resolved. Now cant change mapping to use count, earlier i was able to.

curl -X PUT "localhost:9200/enterprise_telemetry_v1/_mapping/_doc?pretty" -H 'Content-Type: application/json' -d'
{
  "properties": {
    "imei": {
      "type": "keyword"
    }
  }
}
'

I have fixed mapping issue,
back to python dsl, now it raises error imei_count is not defined.

def get_telemetry_for_license():
    docs = Telemetry.search().aggs.metric('imei_count', 'cardinality', field='imei')
    docs = docs.execute()
    return imei_count