diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/ScrollDataExtractor.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/ScrollDataExtractor.java index ac286d976ae..aaee3f2528c 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/ScrollDataExtractor.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/ScrollDataExtractor.java @@ -214,7 +214,9 @@ class ScrollDataExtractor implements DataExtractor { } private void resetScroll() { - clearScroll(scrollId); + if (scrollId != null) { + clearScroll(scrollId); + } scrollId = null; } diff --git a/plugin/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/ScrollDataExtractorTests.java b/plugin/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/ScrollDataExtractorTests.java index c7a47f4eb16..fe900755044 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/ScrollDataExtractorTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/ScrollDataExtractorTests.java @@ -384,6 +384,17 @@ public class ScrollDataExtractorTests extends ESTestCase { expectThrows(SearchPhaseExecutionException.class, () -> extractor.next()); } + public void testSearchPhaseExecutionExceptionOnInitScroll() throws IOException { + TestDataExtractor extractor = new TestDataExtractor(1000L, 2000L); + + extractor.setNextResponse(createResponseWithShardFailures()); + extractor.setNextResponse(createResponseWithShardFailures()); + + expectThrows(IOException.class, () -> extractor.next()); + + assertThat(capturedClearScrollIds.isEmpty(), is(true)); + } + public void testDomainSplitScriptField() throws IOException { SearchSourceBuilder.ScriptField withoutSplit = new SearchSourceBuilder.ScriptField(