Elasticsearch _count查询请求缓存 [英] Elasticsearch _count query request cache

查看:124
本文介绍了Elasticsearch _count查询请求缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

摘自文档:解决方案

只要文档没有说明,请转到源代码;-)

在这种情况下,如果我们查看

此外,当

因此,从这一点上,我们可以推断出实际上也对计数查询进行了缓存.

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.

解决方案

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.

这篇关于Elasticsearch _count查询请求缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆