1

Taken from docs: https://www.elastic.co/guide/en/elasticsearch/reference/7.9/shard-request-cache.html#shard-request-cache

By default, the requests cache will only cache the results of search requests where size=0, so it will not cache hits, but it will cache hits.total, aggregations, and suggestions.

Most queries that use now (see Date Math) cannot be cached.

Scripted queries that use the API calls which are non-deterministic, such as Math.random() or new Date() are not cached.

However how does this play with _count queries? _count queries behave almost exactly the same as _search queries with size=0?

I'd expect request cache to cache count queries as well, but couldn't find any information about it.

Evaldas Buinauskas
  • 13,739
  • 11
  • 55
  • 107

1 Answers1

2

Whenever the documentation doesn't tell, go to the source ;-)

In this case, if we look at the source of RestCountAction (i.e. the class handling the _count endpoint), we can see that what it actually does is creating a SearchRequest with size: 0

    a search request
          |
          v
    SearchRequest countRequest = new SearchRequest(Strings.splitStringByCommaToArray(request.param("index")));
    countRequest.indicesOptions(IndicesOptions.fromRequest(request, countRequest.indicesOptions()));
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().size(0).trackTotalHits(true);
                                                                          ^
                                                                          |
                                                                     with size 0

Furthermore, when building the response we can see that the count value is actually the value of hits.total from the SearchResponse:

    builder.field("count", response.getHits().getTotalHits().value);

So, from that, we can deduce that count queries are de facto cached as well.

Amit
  • 30,756
  • 6
  • 57
  • 88
Val
  • 207,596
  • 13
  • 358
  • 360
  • and opster, I explicitly used the `request_cache=true` for my heavy/ costly search queries and according to doc, than it would cache the `hits` as well but even after multiple times and try on different queries, I can't see much difference in query performance –  Sep 10 '20 at 10:50
  • Please let me know what could be the cause, one thing I notice is that `hits.total` was also changing which as my index is also getting the write request and I feel this could be the cause as shard is getting refreshed and cache is getting invalidated –  Sep 10 '20 at 10:52
  • 1
    @PrernaGupta maybe you want to create a question of your own for your specific use case? This one was not exactly about what you're asking – Val Sep 10 '20 at 10:53
  • @PrernaGupta from the link the OP shared: `Cached results are invalidated automatically whenever the shard refreshes, but only if the data in the shard has actually changed.` so it's pretty clear that if you have write requests, the cache get's cleared – Val Sep 10 '20 at 10:54
  • so even if data is not changed(no new indexed request) and refresh_interval is set to 1 sec(default and same in my case) search results will be removed from cache? –  Sep 10 '20 at 10:56
  • read again `...but only if the data in the shard has actually changed`, but you said that your index was getting write requests – Val Sep 10 '20 at 10:56
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/221259/discussion-between-prerna-gupta-and-val). –  Sep 10 '20 at 10:57