# How do I use Elasticsearch aggregation using python elasticsearch\_dsl library?

**URL:** <https://discuss.elastic.co/t/how-do-i-use-elasticsearch-aggregation-using-python-elasticsearch-dsl-library/283345>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [September 4, 2021, 2:17pm UTC](https://discuss.elastic.co/t/how-do-i-use-elasticsearch-aggregation-using-python-elasticsearch-dsl-library/283345 "2021-09-04T14:17:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rafiqul](https://avatars.discourse-cdn.com/v4/letter/r/db5fbb/32.png) [@rafiqul](https://discuss.elastic.co/u/rafiqul)\
**Post date:** [September 4, 2021, 2:17pm UTC](https://discuss.elastic.co/t/how-do-i-use-elasticsearch-aggregation-using-python-elasticsearch-dsl-library/283345/1 "2021-09-04T14:17:53Z")

</div>

I have a python model that creates the index as

```auto
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.

```auto
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.

```auto
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.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [September 6, 2021, 8:18am UTC](https://discuss.elastic.co/t/how-do-i-use-elasticsearch-aggregation-using-python-elasticsearch-dsl-library/283345/2 "2021-09-06T08:18:31Z")

</div>

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](https://elasticsearch-dsl.readthedocs.io/en/latest/search_dsl.html#id2)

---

<div class="post-metadata">

**Author:** ![rafiqul](https://avatars.discourse-cdn.com/v4/letter/r/db5fbb/32.png) [@rafiqul](https://discuss.elastic.co/u/rafiqul)\
**Post date:** [September 7, 2021, 6:46am UTC](https://discuss.elastic.co/t/how-do-i-use-elasticsearch-aggregation-using-python-elasticsearch-dsl-library/283345/3 "2021-09-07T06:46:57Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![rafiqul](https://avatars.discourse-cdn.com/v4/letter/r/db5fbb/32.png) [@rafiqul](https://discuss.elastic.co/u/rafiqul)\
**Post date:** [September 7, 2021, 8:42am UTC](https://discuss.elastic.co/t/how-do-i-use-elasticsearch-aggregation-using-python-elasticsearch-dsl-library/283345/4 "2021-09-07T08:42:13Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 5, 2021, 8:43am UTC](https://discuss.elastic.co/t/how-do-i-use-elasticsearch-aggregation-using-python-elasticsearch-dsl-library/283345/5 "2021-10-05T08:43:11Z")

</div>

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