Fixed date math expression support in multi get requests. (#20659)

Date math index/alias expressions in mget will now be resolved to a concrete single index instead of failing the mget item with an `IndexNotFoundException`.

Added also an integration test to verify multi index aliases do not fail the entire mget request.

Closes #17957
This commit is contained in:
qwerty4030 2016-10-20 00:26:55 -07:00 committed by Luca Cavanna
parent c92b550df2
commit 95b6f85c87
3 changed files with 46 additions and 11 deletions

View File

@ -64,16 +64,11 @@ public class TransportMultiGetAction extends HandledTransportAction<MultiGetRequ
for (int i = 0; i < request.items.size(); i++) {
MultiGetRequest.Item item = request.items.get(i);
if (!clusterState.metaData().hasConcreteIndex(item.index())) {
responses.set(i, newItemFailure(item.index(), item.type(), item.id(), new IndexNotFoundException(item.index())));
continue;
}
String concreteSingleIndex;
try {
item.routing(clusterState.metaData().resolveIndexRouting(item.parent(), item.routing(), item.index()));
concreteSingleIndex = indexNameExpressionResolver.concreteSingleIndex(clusterState, item).getName();
item.routing(clusterState.metaData().resolveIndexRouting(item.parent(), item.routing(), concreteSingleIndex));
if ((item.routing() == null) && (clusterState.getMetaData().routingRequired(concreteSingleIndex, item.type()))) {
String message = "routing is required for [" + concreteSingleIndex + "]/[" + item.type() + "]/[" + item.id() + "]";
responses.set(i, newItemFailure(concreteSingleIndex, item.type(), item.id(), new IllegalArgumentException(message)));

View File

@ -24,6 +24,7 @@ import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse;
import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.get.MultiGetResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexMetaData;
@ -77,6 +78,17 @@ public class DateMathIndexExpressionsIntegrationIT extends ESIntegTestCase {
assertThat(getResponse.isExists(), is(true));
assertThat(getResponse.getId(), equalTo("3"));
MultiGetResponse mgetResponse = client().prepareMultiGet()
.add(dateMathExp1, "type", "1")
.add(dateMathExp2, "type", "2")
.add(dateMathExp3, "type", "3").get();
assertThat(mgetResponse.getResponses()[0].getResponse().isExists(), is(true));
assertThat(mgetResponse.getResponses()[0].getResponse().getId(), equalTo("1"));
assertThat(mgetResponse.getResponses()[1].getResponse().isExists(), is(true));
assertThat(mgetResponse.getResponses()[1].getResponse().getId(), equalTo("2"));
assertThat(mgetResponse.getResponses()[2].getResponse().isExists(), is(true));
assertThat(mgetResponse.getResponses()[2].getResponse().getId(), equalTo("3"));
IndicesStatsResponse indicesStatsResponse = client().admin().indices().prepareStats(dateMathExp1, dateMathExp2, dateMathExp3).get();
assertThat(indicesStatsResponse.getIndex(index1), notNullValue());
assertThat(indicesStatsResponse.getIndex(index2), notNullValue());

View File

@ -36,12 +36,14 @@ import java.util.Map;
import static org.elasticsearch.action.support.WriteRequest.RefreshPolicy.IMMEDIATE;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
public class SimpleMgetIT extends ESIntegTestCase {
public void testThatMgetShouldWorkWithOneIndexMissing() throws IOException {
createIndex("test");
@ -51,7 +53,7 @@ public class SimpleMgetIT extends ESIntegTestCase {
MultiGetResponse mgetResponse = client().prepareMultiGet()
.add(new MultiGetRequest.Item("test", "test", "1"))
.add(new MultiGetRequest.Item("nonExistingIndex", "test", "1"))
.execute().actionGet();
.get();
assertThat(mgetResponse.getResponses().length, is(2));
assertThat(mgetResponse.getResponses()[0].getIndex(), is("test"));
@ -63,18 +65,44 @@ public class SimpleMgetIT extends ESIntegTestCase {
assertThat(((ElasticsearchException) mgetResponse.getResponses()[1].getFailure().getFailure()).getIndex().getName(),
is("nonExistingIndex"));
mgetResponse = client().prepareMultiGet()
.add(new MultiGetRequest.Item("nonExistingIndex", "test", "1"))
.execute().actionGet();
.get();
assertThat(mgetResponse.getResponses().length, is(1));
assertThat(mgetResponse.getResponses()[0].getIndex(), is("nonExistingIndex"));
assertThat(mgetResponse.getResponses()[0].isFailed(), is(true));
assertThat(mgetResponse.getResponses()[0].getFailure().getMessage(), is("no such index"));
assertThat(((ElasticsearchException) mgetResponse.getResponses()[0].getFailure().getFailure()).getIndex().getName(),
is("nonExistingIndex"));
}
public void testThatMgetShouldWorkWithMultiIndexAlias() throws IOException {
assertAcked(prepareCreate("test").addAlias(new Alias("multiIndexAlias")));
assertAcked(prepareCreate("test2").addAlias(new Alias("multiIndexAlias")));
client().prepareIndex("test", "test", "1").setSource(jsonBuilder().startObject().field("foo", "bar").endObject())
.setRefreshPolicy(IMMEDIATE).get();
MultiGetResponse mgetResponse = client().prepareMultiGet()
.add(new MultiGetRequest.Item("test", "test", "1"))
.add(new MultiGetRequest.Item("multiIndexAlias", "test", "1"))
.get();
assertThat(mgetResponse.getResponses().length, is(2));
assertThat(mgetResponse.getResponses()[0].getIndex(), is("test"));
assertThat(mgetResponse.getResponses()[0].isFailed(), is(false));
assertThat(mgetResponse.getResponses()[1].getIndex(), is("multiIndexAlias"));
assertThat(mgetResponse.getResponses()[1].isFailed(), is(true));
assertThat(mgetResponse.getResponses()[1].getFailure().getMessage(), containsString("more than one indices"));
mgetResponse = client().prepareMultiGet()
.add(new MultiGetRequest.Item("multiIndexAlias", "test", "1"))
.get();
assertThat(mgetResponse.getResponses().length, is(1));
assertThat(mgetResponse.getResponses()[0].getIndex(), is("multiIndexAlias"));
assertThat(mgetResponse.getResponses()[0].isFailed(), is(true));
assertThat(mgetResponse.getResponses()[0].getFailure().getMessage(), containsString("more than one indices"));
}
public void testThatParentPerDocumentIsSupported() throws Exception {
@ -95,7 +123,7 @@ public class SimpleMgetIT extends ESIntegTestCase {
MultiGetResponse mgetResponse = client().prepareMultiGet()
.add(new MultiGetRequest.Item(indexOrAlias(), "test", "1").parent("4"))
.add(new MultiGetRequest.Item(indexOrAlias(), "test", "1"))
.execute().actionGet();
.get();
assertThat(mgetResponse.getResponses().length, is(2));
assertThat(mgetResponse.getResponses()[0].isFailed(), is(false));
@ -163,7 +191,7 @@ public class SimpleMgetIT extends ESIntegTestCase {
MultiGetResponse mgetResponse = client().prepareMultiGet()
.add(new MultiGetRequest.Item(indexOrAlias(), "test", id).routing(routingOtherShard))
.add(new MultiGetRequest.Item(indexOrAlias(), "test", id))
.execute().actionGet();
.get();
assertThat(mgetResponse.getResponses().length, is(2));
assertThat(mgetResponse.getResponses()[0].isFailed(), is(false));