次の関数は、エラスティック サーチで範囲クエリを実行します。この関数をマルチスレッド環境で繰り返し使用すると、アプリケーションがクラッシュするまでメモリが大幅に増加します。
誰でもこの問題の解決策を持っていますか?
/**
RestHighLevelClient restHighLevelClient = new RestHighLevelClient( //
RestClient.builder(new HttpHost("localhost",9200,"http")));
**/
public List<Map<String, Object>> getAllDocumentsInRange(long startTime, long endTime,
RestHighLevelClient restHighLevelClient) {
try {
QueryBuilder queryBuilder = QueryBuilders//
.rangeQuery("date")//
.gte(startTime)//
.lte(endTime);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(queryBuilder);
searchSourceBuilder.sort(new FieldSortBuilder("date").order(SortOrder.ASC));
searchSourceBuilder.size(10000);
searchSourceBuilder.timeout(new TimeValue(20, TimeUnit.SECONDS));
List<Map<String, Object>> docsMap = new ArrayList<>();
SearchRequest searchRequest = new SearchRequest(new String[] { "mainIndex" }, searchSourceBuilder);
Scroll scroll = new Scroll(TimeValue.timeValueSeconds(30));
searchRequest.scroll(scroll);
RestHighLevelClient restHighLevelClient = new RestHighLevelClient( //
RestClient.builder(new HttpHost("localhost",9200,"http")));
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
String scrollId = searchResponse.getScrollId();
SearchHit[] searchHits = searchResponse.getHits().getHits();
while (searchHits != null && searchHits.length > 0) {
for (SearchHit hit : searchHits) {
Map<String, Object> elasticDocVersion = hit.getSourceAsMap();
docsMap.add(elasticDocVersion);
}
SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId);
scrollRequest.scroll(scroll);
searchResponse = restHighLevelClient.scroll(scrollRequest, RequestOptions.DEFAULT);
scrollId = searchResponse.getScrollId();
searchHits = searchResponse.getHits().getHits();
}
ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
clearScrollRequest.addScrollId(scrollId);
ClearScrollResponse clearScrollResponse = //
restHighLevelClient.clearScroll(clearScrollRequest, RequestOptions.DEFAULT);
boolean succeeded = clearScrollResponse.isSucceeded();
logger.trace("search scroll clearation:{}", succeeded);
} catch (Exception e) {
logger.error("error in creaing QueryBuilder class: {}", e.getMessage());
}
return new ArrayList<>();
}
これは、アプリケーションを実行した後のメモリ使用量のイメージです
上記の機能を同期するなど、さまざまな解決策を試しましたが、どれも問題を解決しませんでした!