Search in sources :

Example 6 with RoutingMissingException

use of org.elasticsearch.action.RoutingMissingException in project elasticsearch by elastic.

the class SimpleRoutingIT method testRequiredRoutingMappingVariousAPIs.

public void testRequiredRoutingMappingVariousAPIs() throws Exception {
    client().admin().indices().prepareCreate("test").addAlias(new Alias("alias")).addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("_routing").field("required", true).endObject().endObject().endObject()).execute().actionGet();
    ensureGreen();
    logger.info("--> indexing with id [1], and routing [0]");
    client().prepareIndex(indexOrAlias(), "type1", "1").setRouting("0").setSource("field", "value1").get();
    logger.info("--> indexing with id [2], and routing [0]");
    client().prepareIndex(indexOrAlias(), "type1", "2").setRouting("0").setSource("field", "value2").setRefreshPolicy(RefreshPolicy.IMMEDIATE).get();
    logger.info("--> verifying get with id [1] with routing [0], should succeed");
    assertThat(client().prepareGet(indexOrAlias(), "type1", "1").setRouting("0").execute().actionGet().isExists(), equalTo(true));
    logger.info("--> verifying get with id [1], with no routing, should fail");
    try {
        client().prepareGet(indexOrAlias(), "type1", "1").get();
        fail();
    } catch (RoutingMissingException e) {
        assertThat(e.getMessage(), equalTo("routing is required for [test]/[type1]/[1]"));
    }
    logger.info("--> verifying explain with id [2], with routing [0], should succeed");
    ExplainResponse explainResponse = client().prepareExplain(indexOrAlias(), "type1", "2").setQuery(QueryBuilders.matchAllQuery()).setRouting("0").get();
    assertThat(explainResponse.isExists(), equalTo(true));
    assertThat(explainResponse.isMatch(), equalTo(true));
    logger.info("--> verifying explain with id [2], with no routing, should fail");
    try {
        client().prepareExplain(indexOrAlias(), "type1", "2").setQuery(QueryBuilders.matchAllQuery()).get();
        fail();
    } catch (RoutingMissingException e) {
        assertThat(e.getMessage(), equalTo("routing is required for [test]/[type1]/[2]"));
    }
    logger.info("--> verifying term vector with id [1], with routing [0], should succeed");
    TermVectorsResponse termVectorsResponse = client().prepareTermVectors(indexOrAlias(), "type1", "1").setRouting("0").get();
    assertThat(termVectorsResponse.isExists(), equalTo(true));
    assertThat(termVectorsResponse.getId(), equalTo("1"));
    try {
        client().prepareTermVectors(indexOrAlias(), "type1", "1").get();
        fail();
    } catch (RoutingMissingException e) {
        assertThat(e.getMessage(), equalTo("routing is required for [test]/[type1]/[1]"));
    }
    UpdateResponse updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1").setRouting("0").setDoc(Requests.INDEX_CONTENT_TYPE, "field1", "value1").get();
    assertThat(updateResponse.getId(), equalTo("1"));
    assertThat(updateResponse.getVersion(), equalTo(2L));
    try {
        client().prepareUpdate(indexOrAlias(), "type1", "1").setDoc(Requests.INDEX_CONTENT_TYPE, "field1", "value1").get();
        fail();
    } catch (RoutingMissingException e) {
        assertThat(e.getMessage(), equalTo("routing is required for [test]/[type1]/[1]"));
    }
    logger.info("--> verifying mget with ids [1,2], with routing [0], should succeed");
    MultiGetResponse multiGetResponse = client().prepareMultiGet().add(new MultiGetRequest.Item(indexOrAlias(), "type1", "1").routing("0")).add(new MultiGetRequest.Item(indexOrAlias(), "type1", "2").routing("0")).get();
    assertThat(multiGetResponse.getResponses().length, equalTo(2));
    assertThat(multiGetResponse.getResponses()[0].isFailed(), equalTo(false));
    assertThat(multiGetResponse.getResponses()[0].getResponse().getId(), equalTo("1"));
    assertThat(multiGetResponse.getResponses()[1].isFailed(), equalTo(false));
    assertThat(multiGetResponse.getResponses()[1].getResponse().getId(), equalTo("2"));
    logger.info("--> verifying mget with ids [1,2], with no routing, should fail");
    multiGetResponse = client().prepareMultiGet().add(new MultiGetRequest.Item(indexOrAlias(), "type1", "1")).add(new MultiGetRequest.Item(indexOrAlias(), "type1", "2")).get();
    assertThat(multiGetResponse.getResponses().length, equalTo(2));
    assertThat(multiGetResponse.getResponses()[0].isFailed(), equalTo(true));
    assertThat(multiGetResponse.getResponses()[0].getFailure().getId(), equalTo("1"));
    assertThat(multiGetResponse.getResponses()[0].getFailure().getMessage(), equalTo("routing is required for [test]/[type1]/[1]"));
    assertThat(multiGetResponse.getResponses()[1].isFailed(), equalTo(true));
    assertThat(multiGetResponse.getResponses()[1].getFailure().getId(), equalTo("2"));
    assertThat(multiGetResponse.getResponses()[1].getFailure().getMessage(), equalTo("routing is required for [test]/[type1]/[2]"));
    MultiTermVectorsResponse multiTermVectorsResponse = client().prepareMultiTermVectors().add(new TermVectorsRequest(indexOrAlias(), "type1", "1").routing("0")).add(new TermVectorsRequest(indexOrAlias(), "type1", "2").routing("0")).get();
    assertThat(multiTermVectorsResponse.getResponses().length, equalTo(2));
    assertThat(multiTermVectorsResponse.getResponses()[0].getId(), equalTo("1"));
    assertThat(multiTermVectorsResponse.getResponses()[0].isFailed(), equalTo(false));
    assertThat(multiTermVectorsResponse.getResponses()[0].getResponse().getId(), equalTo("1"));
    assertThat(multiTermVectorsResponse.getResponses()[0].getResponse().isExists(), equalTo(true));
    assertThat(multiTermVectorsResponse.getResponses()[1].getId(), equalTo("2"));
    assertThat(multiTermVectorsResponse.getResponses()[1].isFailed(), equalTo(false));
    assertThat(multiTermVectorsResponse.getResponses()[1].getResponse().getId(), equalTo("2"));
    assertThat(multiTermVectorsResponse.getResponses()[1].getResponse().isExists(), equalTo(true));
    multiTermVectorsResponse = client().prepareMultiTermVectors().add(new TermVectorsRequest(indexOrAlias(), "type1", "1")).add(new TermVectorsRequest(indexOrAlias(), "type1", "2")).get();
    assertThat(multiTermVectorsResponse.getResponses().length, equalTo(2));
    assertThat(multiTermVectorsResponse.getResponses()[0].getId(), equalTo("1"));
    assertThat(multiTermVectorsResponse.getResponses()[0].isFailed(), equalTo(true));
    assertThat(multiTermVectorsResponse.getResponses()[0].getFailure().getCause().getMessage(), equalTo("routing is required for [test]/[type1]/[1]"));
    assertThat(multiTermVectorsResponse.getResponses()[0].getResponse(), nullValue());
    assertThat(multiTermVectorsResponse.getResponses()[1].getId(), equalTo("2"));
    assertThat(multiTermVectorsResponse.getResponses()[1].isFailed(), equalTo(true));
    assertThat(multiTermVectorsResponse.getResponses()[1].getResponse(), nullValue());
    assertThat(multiTermVectorsResponse.getResponses()[1].getFailure().getCause().getMessage(), equalTo("routing is required for [test]/[type1]/[2]"));
}
Also used : TermVectorsRequest(org.elasticsearch.action.termvectors.TermVectorsRequest) MultiTermVectorsResponse(org.elasticsearch.action.termvectors.MultiTermVectorsResponse) TermVectorsResponse(org.elasticsearch.action.termvectors.TermVectorsResponse) UpdateResponse(org.elasticsearch.action.update.UpdateResponse) MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse) Alias(org.elasticsearch.action.admin.indices.alias.Alias) ExplainResponse(org.elasticsearch.action.explain.ExplainResponse) RoutingMissingException(org.elasticsearch.action.RoutingMissingException) MultiTermVectorsResponse(org.elasticsearch.action.termvectors.MultiTermVectorsResponse) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest)

Example 7 with RoutingMissingException

use of org.elasticsearch.action.RoutingMissingException in project elasticsearch by elastic.

the class ElasticsearchExceptionTests method testFromXContentWithCause.

public void testFromXContentWithCause() throws IOException {
    ElasticsearchException e = new ElasticsearchException("foo", new ElasticsearchException("bar", new ElasticsearchException("baz", new RoutingMissingException("_test", "_type", "_id"))));
    final XContent xContent = randomFrom(XContentType.values()).xContent();
    XContentBuilder builder = XContentBuilder.builder(xContent).startObject().value(e).endObject();
    ElasticsearchException parsed;
    try (XContentParser parser = createParser(builder)) {
        assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
        parsed = ElasticsearchException.fromXContent(parser);
        assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
        assertNull(parser.nextToken());
    }
    assertNotNull(parsed);
    assertEquals(parsed.getMessage(), "Elasticsearch exception [type=exception, reason=foo]");
    ElasticsearchException cause = (ElasticsearchException) parsed.getCause();
    assertEquals(cause.getMessage(), "Elasticsearch exception [type=exception, reason=bar]");
    cause = (ElasticsearchException) cause.getCause();
    assertEquals(cause.getMessage(), "Elasticsearch exception [type=exception, reason=baz]");
    cause = (ElasticsearchException) cause.getCause();
    assertEquals(cause.getMessage(), "Elasticsearch exception [type=routing_missing_exception, reason=routing is required for [_test]/[_type]/[_id]]");
    assertThat(cause.getHeaderKeys(), hasSize(0));
    assertThat(cause.getMetadataKeys(), hasSize(2));
    assertThat(cause.getMetadata("es.index"), hasItem("_test"));
    assertThat(cause.getMetadata("es.index_uuid"), hasItem("_na_"));
}
Also used : XContent(org.elasticsearch.common.xcontent.XContent) ToXContent(org.elasticsearch.common.xcontent.ToXContent) RoutingMissingException(org.elasticsearch.action.RoutingMissingException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 8 with RoutingMissingException

use of org.elasticsearch.action.RoutingMissingException in project elasticsearch by elastic.

the class ExceptionSerializationTests method testRoutingMissingException.

public void testRoutingMissingException() throws IOException {
    RoutingMissingException ex = serialize(new RoutingMissingException("idx", "type", "id"));
    assertEquals("idx", ex.getIndex().getName());
    assertEquals("type", ex.getType());
    assertEquals("id", ex.getId());
    assertEquals("routing is required for [idx]/[type]/[id]", ex.getMessage());
}
Also used : RoutingMissingException(org.elasticsearch.action.RoutingMissingException)

Aggregations

RoutingMissingException (org.elasticsearch.action.RoutingMissingException)8 ToXContent (org.elasticsearch.common.xcontent.ToXContent)3 XContent (org.elasticsearch.common.xcontent.XContent)3 XContentParser (org.elasticsearch.common.xcontent.XContentParser)3 Alias (org.elasticsearch.action.admin.indices.alias.Alias)2 MultiGetResponse (org.elasticsearch.action.get.MultiGetResponse)2 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)2 EOFException (java.io.EOFException)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)1 ElasticsearchException (org.elasticsearch.ElasticsearchException)1 NoShardAvailableActionException (org.elasticsearch.action.NoShardAvailableActionException)1 ExplainResponse (org.elasticsearch.action.explain.ExplainResponse)1 GetResponse (org.elasticsearch.action.get.GetResponse)1 MultiGetRequest (org.elasticsearch.action.get.MultiGetRequest)1 SearchPhaseExecutionException (org.elasticsearch.action.search.SearchPhaseExecutionException)1 ShardSearchFailure (org.elasticsearch.action.search.ShardSearchFailure)1 BroadcastShardOperationFailedException (org.elasticsearch.action.support.broadcast.BroadcastShardOperationFailedException)1 MultiTermVectorsResponse (org.elasticsearch.action.termvectors.MultiTermVectorsResponse)1