Search in sources :

Example 31 with IndicesExistsRequest

use of org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest in project components by Talend.

the class ElasticsearchTestUtils method deleteIndex.

/**
 * Deletes an index and block until deletion is complete.
 *
 * @param index The index to delete
 * @param client The client which points to the Elasticsearch instance
 * @throws InterruptedException if blocking thread is interrupted or index existence check failed
 * @throws java.util.concurrent.ExecutionException if index existence check failed
 * @throws IOException if deletion failed
 */
static void deleteIndex(String index, Client client) throws InterruptedException, java.util.concurrent.ExecutionException, IOException {
    IndicesAdminClient indices = client.admin().indices();
    IndicesExistsResponse indicesExistsResponse = indices.exists(new IndicesExistsRequest(index)).get();
    if (indicesExistsResponse.isExists()) {
        indices.prepareClose(index).get();
        // delete index is an asynchronous request, neither refresh or upgrade
        // delete all docs before starting tests. WaitForYellow() and delete directory are too slow,
        // so block thread until it is done (make it synchronous!!!)
        AtomicBoolean indexDeleted = new AtomicBoolean(false);
        AtomicBoolean waitForIndexDeletion = new AtomicBoolean(true);
        indices.delete(Requests.deleteIndexRequest(index), new DeleteActionListener(indexDeleted, waitForIndexDeletion));
        while (waitForIndexDeletion.get()) {
            Thread.sleep(100);
        }
        if (!indexDeleted.get()) {
            throw new IOException("Failed to delete index " + index);
        }
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IndicesExistsResponse(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse) IndicesAdminClient(org.elasticsearch.client.IndicesAdminClient) IOException(java.io.IOException) IndicesExistsRequest(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest)

Example 32 with IndicesExistsRequest

use of org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest in project ranger by apache.

the class RequestUtils method getIndexFromRequest.

// To support all kinds of request in elasticsearch
public static <Request extends ActionRequest> List<String> getIndexFromRequest(Request request) {
    List<String> indexs = new ArrayList<>();
    if (request instanceof SingleShardRequest) {
        indexs.add(((SingleShardRequest<?>) request).index());
        return indexs;
    }
    if (request instanceof ReplicationRequest) {
        indexs.add(((ReplicationRequest<?>) request).index());
        return indexs;
    }
    if (request instanceof InstanceShardOperationRequest) {
        indexs.add(((InstanceShardOperationRequest<?>) request).index());
        return indexs;
    }
    if (request instanceof CreateIndexRequest) {
        indexs.add(((CreateIndexRequest) request).index());
        return indexs;
    }
    if (request instanceof PutMappingRequest) {
        if (((PutMappingRequest) request).getConcreteIndex() != null) {
            indexs.add(((PutMappingRequest) request).getConcreteIndex().getName());
            return indexs;
        } else {
            return Arrays.asList(((PutMappingRequest) request).indices());
        }
    }
    if (request instanceof SearchRequest) {
        return Arrays.asList(((SearchRequest) request).indices());
    }
    if (request instanceof IndicesStatsRequest) {
        return Arrays.asList(((IndicesStatsRequest) request).indices());
    }
    if (request instanceof OpenIndexRequest) {
        return Arrays.asList(((OpenIndexRequest) request).indices());
    }
    if (request instanceof DeleteIndexRequest) {
        return Arrays.asList(((DeleteIndexRequest) request).indices());
    }
    if (request instanceof BulkRequest) {
        @SuppressWarnings("rawtypes") List<DocWriteRequest<?>> requests = ((BulkRequest) request).requests();
        if (CollectionUtils.isNotEmpty(requests)) {
            for (DocWriteRequest<?> docWriteRequest : requests) {
                indexs.add(docWriteRequest.index());
            }
            return indexs;
        }
    }
    if (request instanceof MultiGetRequest) {
        List<Item> items = ((MultiGetRequest) request).getItems();
        if (CollectionUtils.isNotEmpty(items)) {
            for (Item item : items) {
                indexs.add(item.index());
            }
            return indexs;
        }
    }
    if (request instanceof GetMappingsRequest) {
        return Arrays.asList(((GetMappingsRequest) request).indices());
    }
    if (request instanceof GetSettingsRequest) {
        return Arrays.asList(((GetSettingsRequest) request).indices());
    }
    if (request instanceof IndicesExistsRequest) {
        return Arrays.asList(((IndicesExistsRequest) request).indices());
    }
    if (request instanceof GetAliasesRequest) {
        return Arrays.asList(((GetAliasesRequest) request).indices());
    }
    if (request instanceof GetIndexRequest) {
        return Arrays.asList(((GetIndexRequest) request).indices());
    }
    if (request instanceof GetFieldMappingsRequest) {
        return Arrays.asList(((GetFieldMappingsRequest) request).indices());
    }
    if (request instanceof TypesExistsRequest) {
        return Arrays.asList(((TypesExistsRequest) request).indices());
    }
    if (request instanceof ValidateQueryRequest) {
        return Arrays.asList(((ValidateQueryRequest) request).indices());
    }
    if (request instanceof RecoveryRequest) {
        return Arrays.asList(((RecoveryRequest) request).indices());
    }
    if (request instanceof IndicesSegmentsRequest) {
        return Arrays.asList(((IndicesSegmentsRequest) request).indices());
    }
    if (request instanceof IndicesShardStoresRequest) {
        return Arrays.asList(((IndicesShardStoresRequest) request).indices());
    }
    if (request instanceof UpgradeStatusRequest) {
        return Arrays.asList(((UpgradeStatusRequest) request).indices());
    }
    if (request instanceof ClusterSearchShardsRequest) {
        return Arrays.asList(((ClusterSearchShardsRequest) request).indices());
    }
    if (request instanceof IndicesAliasesRequest) {
        List<IndicesAliasesRequest.AliasActions> aliasActions = ((IndicesAliasesRequest) request).getAliasActions();
        if (CollectionUtils.isNotEmpty(aliasActions)) {
            for (IndicesAliasesRequest.AliasActions action : aliasActions) {
                indexs.addAll(Arrays.asList(action.indices()));
            }
            return indexs;
        }
    }
    if (request instanceof ClearIndicesCacheRequest) {
        return Arrays.asList(((ClearIndicesCacheRequest) request).indices());
    }
    if (request instanceof CloseIndexRequest) {
        return Arrays.asList(((CloseIndexRequest) request).indices());
    }
    if (request instanceof FlushRequest) {
        return Arrays.asList(((FlushRequest) request).indices());
    }
    if (request instanceof SyncedFlushRequest) {
        return Arrays.asList(((SyncedFlushRequest) request).indices());
    }
    if (request instanceof ForceMergeRequest) {
        return Arrays.asList(((ForceMergeRequest) request).indices());
    }
    if (request instanceof RefreshRequest) {
        return Arrays.asList(((RefreshRequest) request).indices());
    }
    if (request instanceof RolloverRequest) {
        return Arrays.asList(((RolloverRequest) request).indices());
    }
    if (request instanceof UpdateSettingsRequest) {
        return Arrays.asList(((UpdateSettingsRequest) request).indices());
    }
    if (request instanceof ResizeRequest) {
        return Arrays.asList(((ResizeRequest) request).indices());
    }
    if (request instanceof DeleteIndexTemplateRequest) {
        indexs.add(((DeleteIndexTemplateRequest) request).name());
        return indexs;
    }
    if (request instanceof GetIndexTemplatesRequest) {
        return Arrays.asList(((GetIndexTemplatesRequest) request).names());
    }
    if (request instanceof PutIndexTemplateRequest) {
        indexs.add(((PutIndexTemplateRequest) request).name());
        return indexs;
    }
    if (request instanceof UpgradeRequest) {
        return Arrays.asList(((UpgradeRequest) request).indices());
    }
    if (request instanceof FieldCapabilitiesRequest) {
        return Arrays.asList(((FieldCapabilitiesRequest) request).indices());
    }
    if (request instanceof MultiSearchRequest) {
        List<SearchRequest> searchRequests = ((MultiSearchRequest) request).requests();
        if (CollectionUtils.isNotEmpty(searchRequests)) {
            for (SearchRequest singleRequest : searchRequests) {
                indexs.addAll(Arrays.asList(singleRequest.indices()));
            }
            return indexs;
        }
    }
    if (request instanceof MultiTermVectorsRequest) {
        List<TermVectorsRequest> termVectorsRequests = ((MultiTermVectorsRequest) request).getRequests();
        if (CollectionUtils.isNotEmpty(termVectorsRequests)) {
            for (TermVectorsRequest singleRequest : termVectorsRequests) {
                indexs.addAll(Arrays.asList(singleRequest.indices()));
            }
            return indexs;
        }
    }
    if (request instanceof UpdateByQueryRequest) {
        return Arrays.asList(((UpdateByQueryRequest) request).indices());
    }
    if (request instanceof DeleteByQueryRequest) {
        return Arrays.asList(((DeleteByQueryRequest) request).indices());
    }
    if (request instanceof ReindexRequest) {
        indexs.addAll(Arrays.asList(((ReindexRequest) request).getSearchRequest().indices()));
        indexs.addAll(Arrays.asList(((ReindexRequest) request).getDestination().indices()));
        return indexs;
    }
    // ClearScrollRequest does not carry any index, so return empty List
    if (request instanceof ClearScrollRequest) {
        return indexs;
    }
    // No matched request type to find specific index , set default value *
    indexs.add("*");
    return indexs;
}
Also used : ForceMergeRequest(org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest) IndicesShardStoresRequest(org.elasticsearch.action.admin.indices.shards.IndicesShardStoresRequest) TypesExistsRequest(org.elasticsearch.action.admin.indices.exists.types.TypesExistsRequest) ClearIndicesCacheRequest(org.elasticsearch.action.admin.indices.cache.clear.ClearIndicesCacheRequest) ArrayList(java.util.ArrayList) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) ReplicationRequest(org.elasticsearch.action.support.replication.ReplicationRequest) DeleteIndexTemplateRequest(org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest) SyncedFlushRequest(org.elasticsearch.action.admin.indices.flush.SyncedFlushRequest) InstanceShardOperationRequest(org.elasticsearch.action.support.single.instance.InstanceShardOperationRequest) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest) Item(org.elasticsearch.action.get.MultiGetRequest.Item) ReindexRequest(org.elasticsearch.index.reindex.ReindexRequest) SingleShardRequest(org.elasticsearch.action.support.single.shard.SingleShardRequest) RolloverRequest(org.elasticsearch.action.admin.indices.rollover.RolloverRequest) GetFieldMappingsRequest(org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsRequest) IndicesSegmentsRequest(org.elasticsearch.action.admin.indices.segments.IndicesSegmentsRequest) ClusterSearchShardsRequest(org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsRequest) IndicesAliasesRequest(org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) RefreshRequest(org.elasticsearch.action.admin.indices.refresh.RefreshRequest) TermVectorsRequest(org.elasticsearch.action.termvectors.TermVectorsRequest) MultiTermVectorsRequest(org.elasticsearch.action.termvectors.MultiTermVectorsRequest) SearchRequest(org.elasticsearch.action.search.SearchRequest) MultiSearchRequest(org.elasticsearch.action.search.MultiSearchRequest) PutMappingRequest(org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest) UpdateSettingsRequest(org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest) UpgradeRequest(org.elasticsearch.action.admin.indices.upgrade.post.UpgradeRequest) GetIndexTemplatesRequest(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequest) PutIndexTemplateRequest(org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest) RecoveryRequest(org.elasticsearch.action.admin.indices.recovery.RecoveryRequest) SyncedFlushRequest(org.elasticsearch.action.admin.indices.flush.SyncedFlushRequest) FlushRequest(org.elasticsearch.action.admin.indices.flush.FlushRequest) UpgradeStatusRequest(org.elasticsearch.action.admin.indices.upgrade.get.UpgradeStatusRequest) GetIndexRequest(org.elasticsearch.action.admin.indices.get.GetIndexRequest) GetAliasesRequest(org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest) ClearScrollRequest(org.elasticsearch.action.search.ClearScrollRequest) OpenIndexRequest(org.elasticsearch.action.admin.indices.open.OpenIndexRequest) DeleteByQueryRequest(org.elasticsearch.index.reindex.DeleteByQueryRequest) ValidateQueryRequest(org.elasticsearch.action.admin.indices.validate.query.ValidateQueryRequest) GetMappingsRequest(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest) UpdateByQueryRequest(org.elasticsearch.index.reindex.UpdateByQueryRequest) GetSettingsRequest(org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest) CloseIndexRequest(org.elasticsearch.action.admin.indices.close.CloseIndexRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) MultiTermVectorsRequest(org.elasticsearch.action.termvectors.MultiTermVectorsRequest) MultiSearchRequest(org.elasticsearch.action.search.MultiSearchRequest) FieldCapabilitiesRequest(org.elasticsearch.action.fieldcaps.FieldCapabilitiesRequest) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) IndicesExistsRequest(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest) ResizeRequest(org.elasticsearch.action.admin.indices.shrink.ResizeRequest) IndicesStatsRequest(org.elasticsearch.action.admin.indices.stats.IndicesStatsRequest)

Example 33 with IndicesExistsRequest

use of org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest in project uavstack by uavorg.

the class ESClient method existIndex.

/**
 * existIndex
 *
 * @param index
 * @return
 */
public boolean existIndex(String index) {
    IndicesExistsRequest request = new IndicesExistsRequest(index);
    IndicesExistsResponse response = client.admin().indices().exists(request).actionGet();
    if (response.isExists()) {
        return true;
    }
    return false;
}
Also used : IndicesExistsResponse(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse) IndicesExistsRequest(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest)

Example 34 with IndicesExistsRequest

use of org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest in project uavstack by uavorg.

the class DoTestTransportHookProxy method main.

@SuppressWarnings("unchecked")
public static void main(String[] args) throws IOException {
    ConsoleLogger cl = new ConsoleLogger("test");
    cl.setDebugable(true);
    UAVServer.instance().setLog(cl);
    UAVServer.instance().putServerInfo(CaptureConstants.INFO_APPSERVER_VENDOR, ServerVendor.TOMCAT);
    MOFAgent.mofContext.put("org.uavstack.mof.ext.clsloader", Thread.currentThread().getContextClassLoader());
    TransportHookProxy p = new TransportHookProxy("test", Collections.emptyMap());
    p.doProxyInstall(null, "testApp");
    String[] esAddrs = { "127.0.0.1:9300" };
    String clusterName = "";
    String index = "esindex";
    String type = "String";
    String alias = "alias";
    Boolean result;
    Settings settings = Settings.EMPTY;
    if (!StringHelper.isEmpty(clusterName)) {
        settings = Settings.builder().put("cluster.name", clusterName).build();
    }
    TransportClient client = new PreBuiltTransportClient(settings);
    for (String esAddr : esAddrs) {
        String[] ipport = esAddr.split(":");
        client.addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress(ipport[0], DataConvertHelper.toInt(ipport[1], 9300))));
    }
    result = client.admin().indices().exists(new IndicesExistsRequest(index)).actionGet().isExists();
    if (result) {
        result = client.admin().indices().delete(new DeleteIndexRequest(index)).actionGet().isAcknowledged();
    }
    client.admin().indices().create(new CreateIndexRequest(index)).actionGet().isAcknowledged();
    client.admin().indices().typesExists(new TypesExistsRequest(new String[] { index }, type)).actionGet().isExists();
    client.admin().indices().prepareAliases().addAlias(index, alias).get().isAcknowledged();
    client.admin().indices().prepareAliases().removeAlias(index, alias).get().isAcknowledged();
    client.prepareSearch(index).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).get(TimeValue.timeValueMillis(15000));
    Map<String, Object> m = new HashMap<String, Object>();
    m.put("user", "kimchy");
    m.put("postDate", new Date());
    m.put("message", "trying out Elasticsearch");
    BulkRequestBuilder bulkRequest = client.prepareBulk();
    bulkRequest.add(client.prepareIndex("twitter", "tweet", "1").setSource(m));
    BulkResponse bulkResponse = bulkRequest.get();
    if (bulkResponse.hasFailures()) {
        System.out.println("Failed");
    }
    client.close();
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) TypesExistsRequest(org.elasticsearch.action.admin.indices.exists.types.TypesExistsRequest) HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) Date(java.util.Date) TransportHookProxy(com.creditease.uav.hook.esclient.transport.TransportHookProxy) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) ConsoleLogger(com.creditease.monitor.log.ConsoleLogger) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) Settings(org.elasticsearch.common.settings.Settings) IndicesExistsRequest(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest)

Aggregations

IndicesExistsRequest (org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest)34 Test (org.junit.Test)13 CreateIndexRequest (org.elasticsearch.action.admin.indices.create.CreateIndexRequest)12 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)12 IOException (java.io.IOException)10 DeleteIndexResponse (org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse)10 IndicesExistsResponse (org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse)10 PutMappingResponse (org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse)9 Matchers.containsString (org.hamcrest.Matchers.containsString)9 UsingDataSet (com.lordofthejars.nosqlunit.annotation.UsingDataSet)2 SQLTransportIntegrationTest (io.crate.integrationtests.SQLTransportIntegrationTest)2 PartitionName (io.crate.metadata.PartitionName)2 BytesRef (org.apache.lucene.util.BytesRef)2 CreateIndexResponse (org.elasticsearch.action.admin.indices.create.CreateIndexResponse)2 TypesExistsRequest (org.elasticsearch.action.admin.indices.exists.types.TypesExistsRequest)2 IndicesAdminClient (org.elasticsearch.client.IndicesAdminClient)2 FailedException (backtype.storm.topology.FailedException)1 ConsoleLogger (com.creditease.monitor.log.ConsoleLogger)1 TransportHookProxy (com.creditease.uav.hook.esclient.transport.TransportHookProxy)1 TitanException (com.thinkaurelius.titan.core.TitanException)1