# Store query in cache

**URL:** <https://discuss.elastic.co/t/store-query-in-cache/318994>\
**Category:** Elasticsearch\
**Created:** [November 15, 2022, 7:03pm UTC](https://discuss.elastic.co/t/store-query-in-cache/318994 "2022-11-15T19:03:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ismet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ismet/32/103499_2.png) [@Ismet](https://discuss.elastic.co/u/Ismet)\
**Post date:** [November 15, 2022, 7:03pm UTC](https://discuss.elastic.co/t/store-query-in-cache/318994/1 "2022-11-15T19:03:07Z")

</div>

Hi, how to put query with aggregation in ram cache? I can't find an example.  
Version: 7.9  
There is my query:

```auto
GET product/_search
{
  "size": 0,
  "query": {
    "bool": {
      "must_not": {
        "match": {
          "blocked": true
        }
      },
      "should": [
        {
          "nested": {
            "path": "categories",
            "query": {
              "bool": {
                "filter": {
                  "bool": {
                    "should": [
                      {
                        "match": {
                          "categories.id": "CAT1"
                        }
                      },
                      {
                        "match": {
                          "categories.id": "CAT2"
                        }
                      }
                    ]
                  }
                }
              }
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "categories": {
      "nested": {
        "path": "categories"
      },
      "aggs": {
        "category_name": {
          "terms": {
            "field": "categories.id.keyword",
            "min_doc_count": 1,
            "size": 500,
            "order": {
              "_count": "desc"
            }
          },
          "aggs": {
            "category_products": {
              "reverse_nested": {},
              "aggs": {
                "product": {
                  "top_hits": {
                    "size": 10,
                    "sort": {
                      "noOfSales": "desc"
                    },
                    "_source": [
                      "id"
                    ]
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [November 16, 2022, 10:02pm UTC](https://discuss.elastic.co/t/store-query-in-cache/318994/2 "2022-11-16T22:02:27Z")

</div>

Elasticsearch will cache commonly accessed queries automatically, what issue are you trying to solve with this?

> [@Ismet](#):
>
> Version: 7.9

This is [EOL](https://www.elastic.co/support/eol), please upgrade.

---

<div class="post-metadata">

**Author:** ![Ismet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ismet/32/103499_2.png) [@Ismet](https://discuss.elastic.co/u/Ismet)\
**Post date:** [November 17, 2022, 2:14pm UTC](https://discuss.elastic.co/t/store-query-in-cache/318994/3 "2022-11-17T14:14:33Z")

</div>

This query should be in cache, but response time is 3s. How can I know if a query is in the cache?

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [November 21, 2022, 2:08am UTC](https://discuss.elastic.co/t/store-query-in-cache/318994/4 "2022-11-21T02:08:13Z")

</div>

[Node query cache settings | Elasticsearch Guide [8.5] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/8.5/query-cache.html) is a starting point on this. I am not sure if there is a specific API endpoint to see if something is cached though sorry.

If your query is slow, it might be worth posting some more context on it and we might be able to share some suggestions.

---

<div class="post-metadata">

**Author:** ![Ismet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ismet/32/103499_2.png) [@Ismet](https://discuss.elastic.co/u/Ismet)\
**Post date:** [December 13, 2022, 2:22pm UTC](https://discuss.elastic.co/t/store-query-in-cache/318994/5 "2022-12-13T14:22:53Z")

</div>

This is aggs:

```auto
  "aggs": {
    "categories": {
      "nested": {
        "path": "categories"
      },
      "aggs": {
        "category_name": {
          "terms": {
            "field": "categories.categoryId.keyword",
            "min_doc_count": 1,
            "size": 600,
            "order": {
              "_count": "desc"
            }
          },
      "aggs": {
        "category_products": {
          "reverse_nested": {},
          "aggs": {
            "product": {
              "top_hits": {
                "size": 1,
                "sort": {
                  "noOfSales": "desc"
                },
                    "_source": [
                      "id",
                      "noOfSales",
                      "assets",
                      "inventory",
                      "categories"
                    ]
              }
            }
          }
            }
          }
        }
      }
    }
  }

```

last aggs (category\_products) is very slow, 3 min is request time,

```auto
     "aggs": {
          "category_products": {
            "reverse_nested": {},
            "aggs": {
              "product": {
                "top_hits": {
                  "size": 1,
                  "sort": {
                    "noOfSales": "desc"
                  },
                    "_source": [
                      "id",
                      "noOfSales",
                      "assets",
                      "inventory",
                      "categories"
                    ]
                }
              }
            }

```

I try to get most sales product in category and display categories whit most sales product. I sort all products in buckets and take them first,. Is there a better, faster way?

Doc count error upper bound is 13, by buckets 4.

---

<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:** [January 10, 2023, 2:23pm UTC](https://discuss.elastic.co/t/store-query-in-cache/318994/6 "2023-01-10T14:23:28Z")

</div>

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