您的位置:首页 > 编程语言 > Java开发

elasticsearch清空索引缓存 java api

2016-06-21 15:18 831 查看
参考链接:https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-clearcache.html

应用场景:

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秒内清空完成,而不是立即的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: