diff --git a/server/src/main/java/org/elasticsearch/action/fieldcaps/TransportFieldCapabilitiesAction.java b/server/src/main/java/org/elasticsearch/action/fieldcaps/TransportFieldCapabilitiesAction.java index 4e5e26407210e..f2ffee2507e4f 100644 --- a/server/src/main/java/org/elasticsearch/action/fieldcaps/TransportFieldCapabilitiesAction.java +++ b/server/src/main/java/org/elasticsearch/action/fieldcaps/TransportFieldCapabilitiesAction.java @@ -112,12 +112,12 @@ protected void doExecute(Task task, FieldCapabilitiesRequest request, final Acti final Map indexResponses = Collections.synchronizedMap(new HashMap<>()); // This map is used to share the index response for indices which have the same index mapping hash to reduce the memory usage. - final Map> indexMappingHashToResponses = Collections.synchronizedMap(new HashMap<>()); + final Map indexMappingHashToResponses = Collections.synchronizedMap(new HashMap<>()); final Consumer handleIndexResponse = resp -> { if (resp.canMatch() && resp.getIndexMappingHash() != null) { - Map curr = indexMappingHashToResponses.putIfAbsent(resp.getIndexMappingHash(), resp.get()); + FieldCapabilitiesIndexResponse curr = indexMappingHashToResponses.putIfAbsent(resp.getIndexMappingHash(), resp); if (curr != null) { - resp = new FieldCapabilitiesIndexResponse(resp.getIndexName(), resp.getIndexMappingHash(), curr, true); + resp = new FieldCapabilitiesIndexResponse(resp.getIndexName(), curr.getIndexMappingHash(), curr.get(), true); } } indexResponses.putIfAbsent(resp.getIndexName(), resp); @@ -226,6 +226,12 @@ private static FieldCapabilitiesRequest prepareRemoteRequest( return remoteRequest; } + private static boolean hasSameMappingHash(FieldCapabilitiesIndexResponse r1, FieldCapabilitiesIndexResponse r2) { + return r1.getIndexMappingHash() != null + && r2.getIndexMappingHash() != null + && r1.getIndexMappingHash().equals(r2.getIndexMappingHash()); + } + private FieldCapabilitiesResponse merge( Map indexResponsesMap, FieldCapabilitiesRequest request, @@ -239,8 +245,7 @@ private FieldCapabilitiesResponse merge( final Map> responseMapBuilder = new HashMap<>(); int lastPendingIndex = 0; for (int i = 1; i <= indexResponses.length; i++) { - // use object equality to avoid expensive string comparison of mapping hashes. - if (i == indexResponses.length || indexResponses[lastPendingIndex].get() != indexResponses[i].get()) { + if (i == indexResponses.length || hasSameMappingHash(indexResponses[lastPendingIndex], indexResponses[i]) == false) { final String[] subIndices; if (lastPendingIndex == 0 && i == indexResponses.length) { subIndices = indices;