diff --git a/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/get/TransportGetSnapshotsAction.java b/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/get/TransportGetSnapshotsAction.java index d8b47fbba9f7e..502be6d039b51 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/get/TransportGetSnapshotsAction.java +++ b/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/get/TransportGetSnapshotsAction.java @@ -180,10 +180,9 @@ private List buildSimpleSnapshotInfos(final Set toReso } } } - for (Map.Entry> entry : snapshotsToIndices.entrySet()) { - final List indices = entry.getValue(); + for (SnapshotId snapshotId : toResolve) { + final List indices = snapshotsToIndices.getOrDefault(snapshotId, Collections.emptyList()); CollectionUtil.timSort(indices); - final SnapshotId snapshotId = entry.getKey(); snapshotInfos.add(new SnapshotInfo(snapshotId, indices, repositoryData.getSnapshotState(snapshotId))); } CollectionUtil.timSort(snapshotInfos); diff --git a/server/src/test/java/org/elasticsearch/snapshots/SnapshotStatusApisIT.java b/server/src/test/java/org/elasticsearch/snapshots/SnapshotStatusApisIT.java index 13d94d7eefa0c..e864f3bb2b33f 100644 --- a/server/src/test/java/org/elasticsearch/snapshots/SnapshotStatusApisIT.java +++ b/server/src/test/java/org/elasticsearch/snapshots/SnapshotStatusApisIT.java @@ -41,6 +41,8 @@ import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.greaterThan; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.is; public class SnapshotStatusApisIT extends AbstractSnapshotIntegTestCase { @@ -167,4 +169,26 @@ public void testExceptionOnMissingShardLevelSnapBlob() throws IOException { expectThrows(SnapshotMissingException.class, () -> client().admin().cluster() .prepareSnapshotStatus("test-repo").setSnapshots("test-snap").execute().actionGet()); } + + public void testGetSnapshotsWithoutIndices() { + logger.info("--> creating repository"); + assertAcked(client().admin().cluster().preparePutRepository("test-repo").setType("fs").setSettings( + Settings.builder().put("location", randomRepoPath()).build())); + + logger.info("--> snapshot"); + final SnapshotInfo snapshotInfo = + client().admin().cluster().prepareCreateSnapshot("test-repo", "test-snap") + .setIndices().setWaitForCompletion(true).get().getSnapshotInfo(); + + assertThat(snapshotInfo.state(), is(SnapshotState.SUCCESS)); + assertThat(snapshotInfo.totalShards(), is(0)); + + logger.info("--> verify that snapshot without index shows up in non-verbose listing"); + final List snapshotInfos = + client().admin().cluster().prepareGetSnapshots("test-repo").setVerbose(false).get().getSnapshots(); + assertThat(snapshotInfos, hasSize(1)); + final SnapshotInfo found = snapshotInfos.get(0); + assertThat(found.snapshotId(), is(snapshotInfo.snapshotId())); + assertThat(found.state(), is(SnapshotState.SUCCESS)); + } }