diff --git a/server/src/main/java/org/elasticsearch/common/util/concurrent/AtomicArray.java b/server/src/main/java/org/elasticsearch/common/util/concurrent/AtomicArray.java index fa82aa0ac634a..18aa1578a6619 100644 --- a/server/src/main/java/org/elasticsearch/common/util/concurrent/AtomicArray.java +++ b/server/src/main/java/org/elasticsearch/common/util/concurrent/AtomicArray.java @@ -45,6 +45,23 @@ public int length() { return array.length(); } + /** + * Returns the size of the expected results, excluding potential null values. + * @return the number of non-null elements + */ + public int nonNullLength() { + if (nonNullList != null) { + return nonNullList.size(); + } + int count = 0; + for (int i = 0; i < array.length(); i++) { + if (array.get(i) != null) { + count++; + } + } + return count; + } + /** * Sets the element at position {@code i} to the given value. * diff --git a/x-pack/plugin/async-search/src/main/java/org/elasticsearch/xpack/search/MutableSearchResponse.java b/x-pack/plugin/async-search/src/main/java/org/elasticsearch/xpack/search/MutableSearchResponse.java index 06c2fe4c875f6..2130c2f0a84ce 100644 --- a/x-pack/plugin/async-search/src/main/java/org/elasticsearch/xpack/search/MutableSearchResponse.java +++ b/x-pack/plugin/async-search/src/main/java/org/elasticsearch/xpack/search/MutableSearchResponse.java @@ -221,7 +221,7 @@ synchronized AsyncStatusResponse toStatusResponse(String asyncExecutionId, long totalShards, successfulShards, skippedShards, - getQueryFailuresCount(), + queryFailures == null ? 0 : queryFailures.nonNullLength(), ExceptionsHelper.status(ExceptionsHelper.unwrapCause(failure)) ); } @@ -234,7 +234,7 @@ synchronized AsyncStatusResponse toStatusResponse(String asyncExecutionId, long totalShards, successfulShards, skippedShards, - getQueryFailuresCount(), + queryFailures == null ? 0 : queryFailures.nonNullLength(), null // for a still running search, completion status is null ); } @@ -268,17 +268,4 @@ private ShardSearchFailure[] buildQueryFailures() { } return failures.toArray(ShardSearchFailure[]::new); } - - private int getQueryFailuresCount() { - if (queryFailures == null) { - return 0; - } - int count = 0; - for (int i = 0; i < queryFailures.length(); i++) { - if (queryFailures.get(i) != null) { - count++; - } - } - return count; - } }