diff --git a/src/main/java/org/opensearch/ad/transport/SearchAnomalyResultTransportAction.java b/src/main/java/org/opensearch/ad/transport/SearchAnomalyResultTransportAction.java index bfe4272e4..2aa294a7a 100644 --- a/src/main/java/org/opensearch/ad/transport/SearchAnomalyResultTransportAction.java +++ b/src/main/java/org/opensearch/ad/transport/SearchAnomalyResultTransportAction.java @@ -90,19 +90,28 @@ protected void doExecute(Task task, SearchRequest request, ActionListener customResultIndices = new HashSet<>(); - for (String index : concreteIndices) { - if (index.startsWith(CUSTOM_RESULT_INDEX_PREFIX)) { - customResultIndices.add(index); + if (concreteIndices != null) { + for (String index : concreteIndices) { + if (index.startsWith(CUSTOM_RESULT_INDEX_PREFIX)) { + customResultIndices.add(index); + } } } + // If user need to query custom result index only, and that custom result index deleted. Then + // we should not search anymore. Just throw exception here. if (onlyQueryCustomResultIndex && customResultIndices.size() == 0) { listener.onFailure(new IllegalArgumentException("No custom result indices found")); return; diff --git a/src/test/java/org/opensearch/ad/transport/SearchAnomalyResultActionTests.java b/src/test/java/org/opensearch/ad/transport/SearchAnomalyResultActionTests.java index c1011bfbe..38b369e40 100644 --- a/src/test/java/org/opensearch/ad/transport/SearchAnomalyResultActionTests.java +++ b/src/test/java/org/opensearch/ad/transport/SearchAnomalyResultActionTests.java @@ -40,12 +40,10 @@ public void testSearchResultAction() throws IOException { @Test public void testNoIndex() { deleteIndexIfExists(CommonName.ANOMALY_RESULT_INDEX_ALIAS); - expectThrows( - IllegalArgumentException.class, - () -> client() - .execute(SearchAnomalyResultAction.INSTANCE, matchAllRequest().indices(ALL_AD_RESULTS_INDEX_PATTERN)) - .actionGet(10000) - ); + SearchResponse searchResponse = client() + .execute(SearchAnomalyResultAction.INSTANCE, matchAllRequest().indices(ALL_AD_RESULTS_INDEX_PATTERN)) + .actionGet(10000); + assertEquals(0, searchResponse.getHits().getTotalHits().value); } }