diff --git a/server/src/main/java/org/elasticsearch/index/shard/ShardGetService.java b/server/src/main/java/org/elasticsearch/index/shard/ShardGetService.java index 5caedb5de0bf3..b717eb7b40a73 100644 --- a/server/src/main/java/org/elasticsearch/index/shard/ShardGetService.java +++ b/server/src/main/java/org/elasticsearch/index/shard/ShardGetService.java @@ -8,6 +8,8 @@ package org.elasticsearch.index.shard; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.document.DocumentField; @@ -359,17 +361,24 @@ private static StoredFieldLoader buildStoredFieldLoader(String[] fields, FetchSo * This session must be created and used by a single thread. */ public static final class MGetSession implements Releasable { + private static final Logger logger = LogManager.getLogger(MGetSession.class); private final ShardGetService shardGetService; private final MultiEngineGet multiEngineGet; + private final boolean enabled; + public MGetSession(ShardGetService shardGetService) { this.shardGetService = shardGetService; this.multiEngineGet = shardGetService.indexShard.newMultiEngineGet(); + this.enabled = shardGetService.indexSettings().getSettings().getAsInt("index.max_script_fields", 32) == 32; + logger.trace("MGetSession enabled=[{}].", enabled); } @Override public void close() { - multiEngineGet.close(); + if (enabled) { + multiEngineGet.close(); + } } public ShardId shardId() { @@ -385,19 +394,25 @@ public GetResult get( FetchSourceContext fetchSourceContext, boolean forceSyntheticSource ) throws IOException { - return shardGetService.get( - id, - multiEngineGet::engineGet, - gFields, - realtime, - version, - versionType, - UNASSIGNED_SEQ_NO, - UNASSIGNED_PRIMARY_TERM, - fetchSourceContext, - forceSyntheticSource, - false - ); + try { + return shardGetService.get( + id, + multiEngineGet::engineGet, + gFields, + realtime, + version, + versionType, + UNASSIGNED_SEQ_NO, + UNASSIGNED_PRIMARY_TERM, + fetchSourceContext, + forceSyntheticSource, + false + ); + } finally { + if (enabled == false) { + close(); + } + } } } }