Skip to content

Commit a3d7e43

Browse files
committed
Indices API: fixes GET Alias API backwards compatibility
For just the case when only the aliases are requested, the default indices options are to ignore missing indexes. When requesting any other feature or any combination of features, the default will be to error on missing indices. Closes #7793
1 parent 189a806 commit a3d7e43

File tree

5 files changed

+97
-7
lines changed

5 files changed

+97
-7
lines changed

docs/reference/indices/aliases.asciidoc

+3
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,9 @@ The rest endpoint is: `/{index}/_alias/{alias}`.
314314

315315
coming[1.4.0.Beta1,The API will always include an `aliases` section, even if there aren't any aliases. Previous versions would not return the `aliases` section]
316316

317+
WARNING: For future versions of Elasticsearch, the default <<multi-index>> options will error if a requested index is unavailable. This is to bring
318+
this API in line with the other indices GET APIs
319+
317320
[float]
318321
==== Examples:
319322

docs/reference/migration/migrate_1_4.asciidoc

+3-6
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Add or update a mapping via the <<indices-create-index,create index>> or
3232
[float]
3333
=== Indices APIs
3434

35-
The <<warmer-retrieving, get warmer api>> will return a section for `warmers` even if there are
35+
The <<warmer-retrieving, get warmer api>> will return a section for `warmers` even if there are
3636
no warmers. This ensures that the following two examples are equivalent:
3737

3838
[source,js]
@@ -42,7 +42,7 @@ curl -XGET 'http://localhost:9200/_all/_warmers'
4242
curl -XGET 'http://localhost:9200/_warmers'
4343
--------------------------------------------------
4444

45-
The <<alias-retrieving, get alias api>> will return a section for `aliases` even if there are
45+
The <<alias-retrieving, get alias api>> will return a section for `aliases` even if there are
4646
no aliases. This ensures that the following two examples are equivalent:
4747

4848
[source,js]
@@ -52,10 +52,7 @@ curl -XGET 'http://localhost:9200/_all/_aliases'
5252
curl -XGET 'http://localhost:9200/_aliases'
5353
--------------------------------------------------
5454

55-
In addition, the <<alias-retrieving, get alias api>> now supports <<multi-index>> options and, by default, will
56-
produce an error response if a requested index does not exist.
57-
58-
The <<indices-get-mapping, get mapping api>> will return a section for `mappings` even if there are
55+
The <<indices-get-mapping, get mapping api>> will return a section for `mappings` even if there are
5956
no mappings. This ensures that the following two examples are equivalent:
6057

6158
[source,js]

src/main/java/org/elasticsearch/action/admin/indices/get/GetIndexRequest.java

+35
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121

2222
import org.elasticsearch.ElasticsearchIllegalArgumentException;
2323
import org.elasticsearch.action.ActionRequestValidationException;
24+
import org.elasticsearch.action.support.IndicesOptions;
2425
import org.elasticsearch.action.support.master.info.ClusterInfoRequest;
26+
import org.elasticsearch.cluster.metadata.MetaData;
2527
import org.elasticsearch.common.io.stream.StreamInput;
2628
import org.elasticsearch.common.io.stream.StreamOutput;
2729

@@ -33,6 +35,7 @@
3335
public class GetIndexRequest extends ClusterInfoRequest<GetIndexRequest> {
3436

3537
private String[] features = new String[] { "_settings", "_warmers", "_mappings", "_aliases" };
38+
private boolean indicesOptionsSet = false;
3639

3740
public GetIndexRequest features(String[] features) {
3841
if (features == null) {
@@ -56,12 +59,44 @@ public ActionRequestValidationException validate() {
5659
public void readFrom(StreamInput in) throws IOException {
5760
super.readFrom(in);
5861
features = in.readStringArray();
62+
indicesOptionsSet = in.readBoolean();
5963
}
6064

6165
@Override
6266
public void writeTo(StreamOutput out) throws IOException {
6367
super.writeTo(out);
6468
out.writeStringArray(features);
69+
out.writeBoolean(indicesOptionsSet);
70+
}
71+
72+
@Override
73+
public GetIndexRequest indicesOptions(IndicesOptions indicesOptions) {
74+
this.indicesOptionsSet = true;
75+
return super.indicesOptions(indicesOptions);
76+
}
77+
78+
@Override
79+
public IndicesOptions indicesOptions() {
80+
if (!indicesOptionsSet) {
81+
indicesOptions(resolveIndicesOptions());
82+
}
83+
IndicesOptions indicesOptions = super.indicesOptions();
84+
return indicesOptions;
85+
}
86+
87+
private IndicesOptions resolveIndicesOptions() {
88+
IndicesOptions defaultIndicesOptions = IndicesOptions.strictExpandOpen();
89+
String[] indices = indices();
90+
// This makes sure that the get aliases API behaves exactly like in previous versions wrt indices options iff only aliases are requested
91+
if (features != null && features.length == 1 && features[0] != null && ("_alias".equals(features[0]) || "_aliases".equals(features[0]))) {
92+
// If we are asking for all indices we need to return open and closed, if not we only expand to open
93+
if (MetaData.isAllIndices(indices)) {
94+
defaultIndicesOptions = IndicesOptions.fromOptions(true, true, true, true);
95+
} else {
96+
defaultIndicesOptions = IndicesOptions.lenientExpandOpen();
97+
}
98+
}
99+
return defaultIndicesOptions;
65100
}
66101

67102
}

src/main/java/org/elasticsearch/rest/action/admin/indices/get/RestGetIndicesAction.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ public void handleRequest(final RestRequest request, final RestChannel channel,
6969
if (features != null) {
7070
getIndexRequest.features(features);
7171
}
72-
getIndexRequest.indicesOptions(IndicesOptions.fromRequest(request, getIndexRequest.indicesOptions()));
72+
// The order of calls to the request is important here. We must set the indices and features before
73+
// we call getIndexRequest.indicesOptions(); or we might get the wrong default indices options
74+
IndicesOptions defaultIndicesOptions = getIndexRequest.indicesOptions();
75+
getIndexRequest.indicesOptions(IndicesOptions.fromRequest(request, defaultIndicesOptions));
7376
getIndexRequest.local(request.paramAsBoolean("local", getIndexRequest.local()));
7477
client.admin().indices().getIndex(getIndexRequest, new RestBuilderListener<GetIndexResponse>(channel) {
7578

src/test/java/org/elasticsearch/action/admin/indices/get/GetIndexTests.java

+52
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,58 @@ public void testEmptyMixedFeatures() {
242242
assertEmptyWarmers(response);
243243
}
244244

245+
@Test(expected=IndexMissingException.class)
246+
public void testNotFoundMapping() {
247+
client().admin().indices().prepareGetIndex().addIndices("non_existent_idx").setFeatures("_mapping").get();
248+
}
249+
250+
@Test(expected=IndexMissingException.class)
251+
public void testNotFoundMappings() {
252+
client().admin().indices().prepareGetIndex().addIndices("non_existent_idx").setFeatures("_mappings").get();
253+
}
254+
255+
@Test(expected=IndexMissingException.class)
256+
public void testNotFoundSettings() {
257+
client().admin().indices().prepareGetIndex().addIndices("non_existent_idx").setFeatures("_settings").get();
258+
}
259+
260+
@Test(expected=IndexMissingException.class)
261+
public void testNotFoundWarmer() {
262+
client().admin().indices().prepareGetIndex().addIndices("non_existent_idx").setFeatures("_warmer").get();
263+
}
264+
265+
@Test(expected=IndexMissingException.class)
266+
public void testNotFoundWarmers() {
267+
client().admin().indices().prepareGetIndex().addIndices("non_existent_idx").setFeatures("_warmers").get();
268+
}
269+
270+
@Test
271+
public void testNotFoundAlias() {
272+
GetIndexResponse response = client().admin().indices().prepareGetIndex().addIndices("non_existent_idx").setFeatures("_alias").get();
273+
String[] indices = response.indices();
274+
assertThat(indices, notNullValue());
275+
assertThat(indices.length, equalTo(0));
276+
}
277+
278+
@Test(expected=IndexMissingException.class)
279+
public void testNotFoundMixedFeatures() {
280+
int numFeatures = randomIntBetween(2, allFeatures.length);
281+
List<String> features = new ArrayList<String>(numFeatures);
282+
for (int i = 0; i < numFeatures; i++) {
283+
features.add(randomFrom(allFeatures));
284+
}
285+
client().admin().indices().prepareGetIndex().addIndices("non_existent_idx")
286+
.setFeatures(features.toArray(new String[features.size()])).get();
287+
}
288+
289+
@Test
290+
public void testNotFoundAliases() {
291+
GetIndexResponse response = client().admin().indices().prepareGetIndex().addIndices("non_existent_idx").setFeatures("_aliases").get();
292+
String[] indices = response.indices();
293+
assertThat(indices, notNullValue());
294+
assertThat(indices.length, equalTo(0));
295+
}
296+
245297
private void assertWarmers(GetIndexResponse response, String indexName) {
246298
ImmutableOpenMap<String, ImmutableList<Entry>> warmers = response.warmers();
247299
assertThat(warmers, notNullValue());

0 commit comments

Comments
 (0)