# Are buckets persistent?

**URL:** <https://discuss.elastic.co/t/are-buckets-persistent/143780>\
**Category:** Elasticsearch\
**Created:** [August 10, 2018, 12:13am UTC](https://discuss.elastic.co/t/are-buckets-persistent/143780 "2018-08-10T00:13:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jose\_Ramierez](https://avatars.discourse-cdn.com/v4/letter/j/4bbf92/32.png) [@Jose\_Ramierez](https://discuss.elastic.co/u/Jose_Ramierez)\
**Post date:** [August 10, 2018, 12:13am UTC](https://discuss.elastic.co/t/are-buckets-persistent/143780/1 "2018-08-10T00:13:03Z")

</div>

ES 6.3

Related to this thread: [Are contents of a bucket held in the same shard?](https://discuss.elastic.co/t/are-contents-of-a-bucket-held-in-the-same-shard/141059)

Discussing Bucket Aggregations: [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket.html)

I thought buckets were persistent, that we store documents into specific buckets (of our definition).

My colleague believes buckets are dynamic, built at query time.

Who is right?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [August 10, 2018, 1:09am UTC](https://discuss.elastic.co/t/are-buckets-persistent/143780/2 "2018-08-10T01:09:49Z")

</div>

What do you mean by "buckets"?

---

<div class="post-metadata">

**Author:** ![Jose\_Ramierez](https://avatars.discourse-cdn.com/v4/letter/j/4bbf92/32.png) [@Jose\_Ramierez](https://discuss.elastic.co/u/Jose_Ramierez)\
**Post date:** [August 10, 2018, 1:26am UTC](https://discuss.elastic.co/t/are-buckets-persistent/143780/3 "2018-08-10T01:26:23Z")

</div>

original post above enhanced to make that clear.

Thanks!

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [August 10, 2018, 1:38am UTC](https://discuss.elastic.co/t/are-buckets-persistent/143780/4 "2018-08-10T01:38:04Z")

</div>

Buckets are computed live. They are not persistent on disk if this is what you meant.

Your colleague is right.

But data structure (doc values) to make this computation fast are stored on disk at index time.

---

<div class="post-metadata">

**Author:** ![Jose\_Ramierez](https://avatars.discourse-cdn.com/v4/letter/j/4bbf92/32.png) [@Jose\_Ramierez](https://discuss.elastic.co/u/Jose_Ramierez)\
**Post date:** [August 12, 2018, 9:13pm UTC](https://discuss.elastic.co/t/are-buckets-persistent/143780/5 "2018-08-12T21:13:00Z")

</div>

David,

Can you help me reconcile this thread, with what we discussed here:

> [@Are contents of a bucket held in the same shard?](https://discuss.elastic.co/t/are-contents-of-a-bucket-held-in-the-same-shard/141059):
>
> We are evaluating using buckets to keep data by user account in a single bucket (e.g. the bucket becomes the "accountid" container, holding all documents for that account) The question has arisen: If we have a 20 or 100 shard index, will a given bucket always be kept within a single shard? If not, how many shards will it span?

On that thread, it sounded like I could define a bucket up front (for a given account), and then documents added to that bucket would be held together on a single shard.

Please help me understand all the way.

Thanks!

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [August 12, 2018, 11:48pm UTC](https://discuss.elastic.co/t/are-buckets-persistent/143780/6 "2018-08-12T23:48:13Z")

</div>

If you use a routing key, all documents with the same key will go to a single and same shard.

Buckets (that you compute when running an aggregation) are computed live when you run the search query.  
If you pass a routing key to the request, then only one shard will be used to run the aggregation.

So if you have a userId in your documents, and use userId as the routing key, then if you want to compute an aggregation for user 7 (only), you can pass routing=7, do a bool filter query with userId=7 and run a terms aggregation on whichever field you want.

Now, what problem are you trying to solve actually?

---

<div class="post-metadata">

**Author:** ![Jose\_Ramierez](https://avatars.discourse-cdn.com/v4/letter/j/4bbf92/32.png) [@Jose\_Ramierez](https://discuss.elastic.co/u/Jose_Ramierez)\
**Post date:** [August 13, 2018, 12:26am UTC](https://discuss.elastic.co/t/are-buckets-persistent/143780/7 "2018-08-13T00:26:07Z")

</div>

> [@dadoonet](#):
>
> Now, what problem are you trying to solve actually?

David,

This is a continuation of the thought started [here](https://discuss.elastic.co/t/elasticsearch-compound-index-or-discrete-indexes/140970/6).

On our earlier conversations here, I had the impression that buckets had a persistence aspect. From reviewing the docs, and your comments on this thread, it appears I am mistaken. It appears that routing + shards is the the only path for me.

I suppose the next question becomes: When I do bucket aggregations, how do I make sure the ES limits itself to a single shard? (Does routing apply to bucket aggregations?)

Thank you!

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [August 13, 2018, 2:47am UTC](https://discuss.elastic.co/t/are-buckets-persistent/143780/8 "2018-08-13T02:47:47Z")

</div>

That does not answer to my question. Why do you want to know that? Is there any reason you don't want to use the default behavior?

---

<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:** [September 10, 2018, 2:47am UTC](https://discuss.elastic.co/t/are-buckets-persistent/143780/9 "2018-09-10T02:47:47Z")

</div>

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