elasticsearch清空索引缓存

2018-04-03 14:36:02
1087次阅读
0个评论
应用场景:

1.在进行测试时,如果想知道真实的查询情况,或者缓存占用的太多,需要清理缓存时,就需要先清除内存中的缓存信息; 
2.当内存使用超过自己设置的阀值,没法自动清理时,可以强制清理; 

以下为清空缓存方法:


/**
     * 清空指定索引的缓存
     * @param indexName 索引名
     * @return  清空结果
     */
    public boolean clearIndicesCache(String indexName){
        ClearIndicesCacheResponse  response = null;
        try {
            response = getClient().admin().indices()
                    .clearCache(new ClearIndicesCacheRequest(indexName)
                    .fieldDataCache(true)
                    .filterCache(true)
                    .idCache(true)
                    ).actionGet();
            System.out.println(FastJSONHelper.serialize(response));
            if (response.getFailedShards()>0) {
                return false;
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

CURL方式: 

curl -XPOST ‘http://localhost:9200/twitter/_cache/clear’

twitter为indexName,默认情况下会清空所有缓存.可以设置filter,fielddata,query_cache,或者id_cache为true,如我们只清空filter缓存命令: 
curl -XPOST ‘http://localhost:9200/twitter/_cache/clear?filter=true’

fields参数可以指定删除特定的字段,如: 
curl -XPOST ‘http://localhost:9200/twitter/_cache/clear?filter=true&fields=cphm,cplx’

filter缓存大约会在60秒内清空完成,而不是立即的。

参考链接:https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-clearcache.html 

收藏00

登录 后评论。没有帐号? 注册 一个。