# Scan through result in ES-DSL aggregation

**URL:** <https://discuss.elastic.co/t/scan-through-result-in-es-dsl-aggregation/197677>\
**Category:** Elasticsearch\
**Created:** [September 2, 2019, 11:20am UTC](https://discuss.elastic.co/t/scan-through-result-in-es-dsl-aggregation/197677 "2019-09-02T11:20:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![minhtriet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/minhtriet/32/53486_2.png) [@minhtriet](https://discuss.elastic.co/u/minhtriet)\
**Post date:** [September 2, 2019, 11:20am UTC](https://discuss.elastic.co/t/scan-through-result-in-es-dsl-aggregation/197677/1 "2019-09-02T11:20:46Z")

</div>

I wanted to see how many unique link that a user has posted for every user and save something like`user_id, number_of_post` to a csv. Here is what I have come up so far

`s.aggs.bucket('user_term', A('terms', field='user__id')).metric('url_count', A('value_count', field='link'))`

However, I have yet found a way to iterate through that result. Is there a way for this?

---

<div class="post-metadata">

**Author:** ![rugenl](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rugenl/32/12887_2.png) [@rugenl](https://discuss.elastic.co/u/rugenl)\
**Post date:** [September 2, 2019, 12:34pm UTC](https://discuss.elastic.co/t/scan-through-result-in-es-dsl-aggregation/197677/2 "2019-09-02T12:34:14Z")

</div>

I think that code is just building the query, do you have following code something like this:

```
response = s.execute()
print('Total %d hits found.' % response.hits.total)
for h in response:
    print(h.title, h.body)
```

---

<div class="post-metadata">

**Author:** ![minhtriet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/minhtriet/32/53486_2.png) [@minhtriet](https://discuss.elastic.co/u/minhtriet)\
**Post date:** [September 3, 2019, 9:52am UTC](https://discuss.elastic.co/t/scan-through-result-in-es-dsl-aggregation/197677/3 "2019-09-03T09:52:28Z")

</div>

Thank you for your answer! I now have something like

```auto
s.aggs.bucket('users', 'terms', field='user.id').metric('url_count', 'cardinality', field='link')

r = s.execute()

for user in r.aggregations.users.buckets:
    print(f'User {user.key} posted {user.url_count.value} links')

```

but theyonly return 10 results. If I change it to `s=s[:]`, I would have a `TransportError(502)`. Stackoverflow seems not haveing anything promising

---

<div class="post-metadata">

**Author:** ![rugenl](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rugenl/32/12887_2.png) [@rugenl](https://discuss.elastic.co/u/rugenl)\
**Post date:** [September 3, 2019, 2:18pm UTC](https://discuss.elastic.co/t/scan-through-result-in-es-dsl-aggregation/197677/4 "2019-09-03T14:18:28Z")

</div>

Add the size parameter, I think 10 is the default

> [@minhtriet](#):
>
> s.aggs.bucket('users', 'terms', field='user.id', size=1000)....

There is a limit to 10,000 buckets in later versions, so 10K for a simple aggregation, or a total of 10K for nested. They add up quick too...

---

<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 1, 2019, 2:18pm UTC](https://discuss.elastic.co/t/scan-through-result-in-es-dsl-aggregation/197677/5 "2019-10-01T14:18:28Z")

</div>

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