Search in sources :

Example 1 with TypesExistsResponse

use of org.elasticsearch.action.admin.indices.exists.types.TypesExistsResponse in project elasticsearch by elastic.

the class TypesExistsIT method testSimple.

public void testSimple() throws Exception {
    Client client = client();
    CreateIndexResponse response1 = client.admin().indices().prepareCreate("test1").addMapping("type1", jsonBuilder().startObject().startObject("type1").endObject().endObject()).addMapping("type2", jsonBuilder().startObject().startObject("type2").endObject().endObject()).execute().actionGet();
    CreateIndexResponse response2 = client.admin().indices().prepareCreate("test2").addMapping("type1", jsonBuilder().startObject().startObject("type1").endObject().endObject()).execute().actionGet();
    client.admin().indices().prepareAliases().addAlias("test1", "alias1").execute().actionGet();
    assertAcked(response1);
    assertAcked(response2);
    TypesExistsResponse response = client.admin().indices().prepareTypesExists("test1").setTypes("type1").execute().actionGet();
    assertThat(response.isExists(), equalTo(true));
    response = client.admin().indices().prepareTypesExists("test1").setTypes("type2").execute().actionGet();
    assertThat(response.isExists(), equalTo(true));
    response = client.admin().indices().prepareTypesExists("test1").setTypes("type3").execute().actionGet();
    assertThat(response.isExists(), equalTo(false));
    try {
        client.admin().indices().prepareTypesExists("notExist").setTypes("type1").execute().actionGet();
        fail("Exception should have been thrown");
    } catch (IndexNotFoundException e) {
    }
    try {
        client.admin().indices().prepareTypesExists("notExist").setTypes("type0").execute().actionGet();
        fail("Exception should have been thrown");
    } catch (IndexNotFoundException e) {
    }
    response = client.admin().indices().prepareTypesExists("alias1").setTypes("type1").execute().actionGet();
    assertThat(response.isExists(), equalTo(true));
    response = client.admin().indices().prepareTypesExists("*").setTypes("type1").execute().actionGet();
    assertThat(response.isExists(), equalTo(true));
    response = client.admin().indices().prepareTypesExists("test1", "test2").setTypes("type1").execute().actionGet();
    assertThat(response.isExists(), equalTo(true));
    response = client.admin().indices().prepareTypesExists("test1", "test2").setTypes("type2").execute().actionGet();
    assertThat(response.isExists(), equalTo(false));
}
Also used : IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) TypesExistsResponse(org.elasticsearch.action.admin.indices.exists.types.TypesExistsResponse) Client(org.elasticsearch.client.Client) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse)

Example 2 with TypesExistsResponse

use of org.elasticsearch.action.admin.indices.exists.types.TypesExistsResponse in project elasticsearch by elastic.

the class RestTypesExistsAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    TypesExistsRequest typesExistsRequest = new TypesExistsRequest(Strings.splitStringByCommaToArray(request.param("index")), Strings.splitStringByCommaToArray(request.param("type")));
    typesExistsRequest.local(request.paramAsBoolean("local", typesExistsRequest.local()));
    typesExistsRequest.indicesOptions(IndicesOptions.fromRequest(request, typesExistsRequest.indicesOptions()));
    return channel -> client.admin().indices().typesExists(typesExistsRequest, new RestResponseListener<TypesExistsResponse>(channel) {

        @Override
        public RestResponse buildResponse(TypesExistsResponse response) throws Exception {
            if (response.isExists()) {
                return new BytesRestResponse(OK, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY);
            } else {
                return new BytesRestResponse(NOT_FOUND, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY);
            }
        }
    });
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) RestResponse(org.elasticsearch.rest.RestResponse) IOException(java.io.IOException) NOT_FOUND(org.elasticsearch.rest.RestStatus.NOT_FOUND) RestController(org.elasticsearch.rest.RestController) Strings(org.elasticsearch.common.Strings) BytesArray(org.elasticsearch.common.bytes.BytesArray) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) Settings(org.elasticsearch.common.settings.Settings) TypesExistsRequest(org.elasticsearch.action.admin.indices.exists.types.TypesExistsRequest) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) RestRequest(org.elasticsearch.rest.RestRequest) OK(org.elasticsearch.rest.RestStatus.OK) TypesExistsResponse(org.elasticsearch.action.admin.indices.exists.types.TypesExistsResponse) NodeClient(org.elasticsearch.client.node.NodeClient) HEAD(org.elasticsearch.rest.RestRequest.Method.HEAD) RestResponseListener(org.elasticsearch.rest.action.RestResponseListener) TypesExistsRequest(org.elasticsearch.action.admin.indices.exists.types.TypesExistsRequest) RestResponse(org.elasticsearch.rest.RestResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) TypesExistsResponse(org.elasticsearch.action.admin.indices.exists.types.TypesExistsResponse) IOException(java.io.IOException)

Aggregations

TypesExistsResponse (org.elasticsearch.action.admin.indices.exists.types.TypesExistsResponse)2 IOException (java.io.IOException)1 CreateIndexResponse (org.elasticsearch.action.admin.indices.create.CreateIndexResponse)1 TypesExistsRequest (org.elasticsearch.action.admin.indices.exists.types.TypesExistsRequest)1 IndicesOptions (org.elasticsearch.action.support.IndicesOptions)1 Client (org.elasticsearch.client.Client)1 NodeClient (org.elasticsearch.client.node.NodeClient)1 Strings (org.elasticsearch.common.Strings)1 BytesArray (org.elasticsearch.common.bytes.BytesArray)1 Settings (org.elasticsearch.common.settings.Settings)1 IndexNotFoundException (org.elasticsearch.index.IndexNotFoundException)1 BaseRestHandler (org.elasticsearch.rest.BaseRestHandler)1 BytesRestResponse (org.elasticsearch.rest.BytesRestResponse)1 RestController (org.elasticsearch.rest.RestController)1 RestRequest (org.elasticsearch.rest.RestRequest)1 HEAD (org.elasticsearch.rest.RestRequest.Method.HEAD)1 RestResponse (org.elasticsearch.rest.RestResponse)1 NOT_FOUND (org.elasticsearch.rest.RestStatus.NOT_FOUND)1 OK (org.elasticsearch.rest.RestStatus.OK)1 RestResponseListener (org.elasticsearch.rest.action.RestResponseListener)1