org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest Java Examples

The following examples show how to use org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CrudDemo.java    From javabase with Apache License 2.0 6 votes vote down vote up
/**
     * 索引 别名 就像一个快捷方式或软连接,可以指向一个或多个索引,也可以给任何需要索引名的 API 使用。别名带给我们极大的灵活性,
     * 比如 我们线上使用一个索引名为index_a的结构,里面有一个类型现在不满足要求,现在需要修改,因为elasticsearch不支持直接修改类型,所以我们必须要重新建立一个新的索引
     * 然后将久索引的数据拷贝过去。 但是如果让新的索引起作用,我们需要要修改引用代码,因为索引名称更换了,但是如果我们一开始创建索引的时候就给索引增加一个别名
     * 使用的时候都是使用index_alis 无论软连接的指向是什么索引,对外暴露的始终都是index_alis
     * @param indicesAdminClient
     */
    private static void createAliasIndex(IndicesAdminClient indicesAdminClient) {
//        1 创建索引 elsdb_alis_v1。
//        2 将别名 elsdb_alis 指向 elsdb_alis_v1
//        3 然后,我们决定修改索引elsdb_alis_v1中一个字段的映射。当然我们不能修改现存的映射,索引我们需要重新索引数据。首先,我们创建有新的映射的索引 elsdb_alis_v2。
//        4 然后我们从将数据从 elsdb_alis_v1 迁移到 elsdb_alis_v2,下面的过程在【重新索引】中描述过了。一旦我们认为数据已经被正确的索引了,我们就将别名指向新的索引。


        //创建索引
        if (checkExistsIndex(indicesAdminClient, INDEX_ALIAS_NAME_VERSION_ONE)) {
            deleteIndex(indicesAdminClient, INDEX_ALIAS_NAME_VERSION_ONE);
        }

        indicesAdminClient.prepareCreate(INDEX_ALIAS_NAME_VERSION_ONE).setSettings().execute().actionGet();
        //添加alias 所有别名
        indicesAdminClient.prepareAliases().addAlias(INDEX_ALIAS_NAME_VERSION_ONE,INDEX_ALIAS_NAME_ALIS).execute().actionGet();

        GetAliasesResponse getAliasesResponse = indicesAdminClient.getAliases(new GetAliasesRequest().indices(INDEX_ALIAS_NAME_ALIS)).actionGet();
        //log.info("getAliasesResponse index setting:{}", getAliasesResponse.getAliases());

        indicesAdminClient.prepareAliases().removeAlias(INDEX_ALIAS_NAME_VERSION_ONE,INDEX_ALIAS_NAME_ALIS).addAlias(INDEX_ALIAS_NAME_VERSION_TWO,INDEX_ALIAS_NAME_ALIS);
    }
 
Example #2
Source File: RestAliasAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected void doRequest(final RestRequest request, final RestChannel channel, final Client client) {
    final GetAliasesRequest getAliasesRequest = request.hasParam("alias") ?
            new GetAliasesRequest(request.param("alias")) :
            new GetAliasesRequest();
    getAliasesRequest.local(request.paramAsBoolean("local", getAliasesRequest.local()));

    client.admin().indices().getAliases(getAliasesRequest, new RestResponseListener<GetAliasesResponse>(channel) {
        @Override
        public RestResponse buildResponse(GetAliasesResponse response) throws Exception {
            Table tab = buildTable(request, response);
            return RestTable.buildResponse(tab, channel);
        }
    });
}
 
Example #3
Source File: EsIndexCacheImpl.java    From usergrid with Apache License 2.0 5 votes vote down vote up
private String[] getIndexesFromEs(final String aliasName){
    final AdminClient adminClient = this.provider.getClient().admin();
         //remove write alias, can only have one
    ImmutableOpenMap<String, List<AliasMetaData>> aliasMap =
        adminClient.indices().getAliases( new GetAliasesRequest( aliasName ) ).actionGet().getAliases();
    return aliasMap.keys().toArray( String.class );
}
 
Example #4
Source File: ElasticSearch7Client.java    From skywalking with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public List<String> retrievalIndexByAliases(String aliases) throws IOException {
    aliases = formatIndexName(aliases);

    GetAliasesRequest getAliasesRequest = new GetAliasesRequest(aliases);
    GetAliasesResponse alias = client.indices().getAlias(getAliasesRequest, RequestOptions.DEFAULT);
    return new ArrayList<>(alias.getAliases().keySet());
}
 
Example #5
Source File: AliasMonitorService.java    From es-service-parent with Apache License 2.0 5 votes vote down vote up
private void doService() {
    List<IndexType> indexs = IndexType.getAllIndex();
    IndicesAdminClient adminClient = ESClient.getClient().admin().indices();
    for (IndexType indexType : indexs) {
        String indexname = indexType.getIndexName().toLowerCase();
        String lastIndexName = null;
        try {
            lastIndexName = updateLogger.getLastIndexName(indexType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (adminClient.prepareExists(indexType.index_type_1()).execute().actionGet()
                .isExists()
                && adminClient
                        .aliasesExist(
                                new GetAliasesRequest().indices(indexType.index_type_1())
                                        .aliases(indexname)).actionGet().isExists()) {
            if (adminClient.prepareExists(indexType.index_type_2()).execute().actionGet()
                    .isExists()
                    && adminClient
                            .aliasesExist(
                                    new GetAliasesRequest().indices(indexType.index_type_2())
                                            .aliases(indexname)).actionGet().isExists()) {
                log.error("indexname:{},{} alias:{}", indexType.index_type_1(),
                        indexType.index_type_2(), indexname);
                if (lastIndexName != null && indexType.index_type_1().equals(lastIndexName)) {
                    adminClient.delete(new DeleteIndexRequest(indexType.index_type_2()));// 删除索引
                } else {
                    adminClient.delete(new DeleteIndexRequest(indexType.index_type_1()));// 删除索引
                }
            }
        }
    }
}
 
Example #6
Source File: AwsRestHighLevelClient.java    From aws-athena-query-federation with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the indices from the domain.
 * @return a Set containing all the indices retrieved from the specified domain.
 * @throws IOException
 */
public Set<String> getAliases()
        throws IOException
{
    GetAliasesRequest getAliasesRequest = new GetAliasesRequest();
    GetAliasesResponse getAliasesResponse = indices().getAlias(getAliasesRequest, RequestOptions.DEFAULT);
    return getAliasesResponse.getAliases().keySet();
}
 
Example #7
Source File: RestGetAliasesAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) {
    final String[] aliases = request.paramAsStringArrayOrEmptyIfAll("name");
    final String[] indices = Strings.splitStringByCommaToArray(request.param("index"));
    final GetAliasesRequest getAliasesRequest = new GetAliasesRequest(aliases);
    getAliasesRequest.indices(indices);
    getAliasesRequest.indicesOptions(IndicesOptions.fromRequest(request, getAliasesRequest.indicesOptions()));
    getAliasesRequest.local(request.paramAsBoolean("local", getAliasesRequest.local()));

    client.admin().indices().getAliases(getAliasesRequest, new RestBuilderListener<GetAliasesResponse>(channel) {
        @Override
        public RestResponse buildResponse(GetAliasesResponse response, XContentBuilder builder) throws Exception {
            // empty body, if indices were specified but no aliases were
            if (indices.length > 0 && response.getAliases().isEmpty()) {
                return new BytesRestResponse(OK, builder.startObject().endObject());
            } else if (response.getAliases().isEmpty()) {
                String message = String.format(Locale.ROOT, "alias [%s] missing", toNamesString(getAliasesRequest.aliases()));
                builder.startObject()
                        .field("error", message)
                        .field("status", RestStatus.NOT_FOUND.getStatus())
                        .endObject();
                return new BytesRestResponse(RestStatus.NOT_FOUND, builder);
            }

            builder.startObject();
            for (ObjectObjectCursor<String, List<AliasMetaData>> entry : response.getAliases()) {
                builder.startObject(entry.key, XContentBuilder.FieldCaseConversion.NONE);
                builder.startObject(Fields.ALIASES);
                for (AliasMetaData alias : entry.value) {
                    AliasMetaData.Builder.toXContent(alias, builder, ToXContent.EMPTY_PARAMS);
                }
                builder.endObject();
                builder.endObject();
            }
            builder.endObject();
            return new BytesRestResponse(OK, builder);
        }
    });
}
 
Example #8
Source File: TransportAliasesExistAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Inject
public TransportAliasesExistAction(Settings settings, TransportService transportService, ClusterService clusterService,
                                   ThreadPool threadPool, ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver) {
    super(settings, AliasesExistAction.NAME, transportService, clusterService, threadPool, actionFilters, indexNameExpressionResolver, GetAliasesRequest.class);
}
 
Example #9
Source File: TransportAliasesExistAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
protected ClusterBlockException checkBlock(GetAliasesRequest request, ClusterState state) {
    return state.blocks().indicesBlockedException(ClusterBlockLevel.METADATA_READ, indexNameExpressionResolver.concreteIndices(state, request));
}
 
Example #10
Source File: TransportAliasesExistAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
protected void masterOperation(GetAliasesRequest request, ClusterState state, ActionListener<AliasesExistResponse> listener) {
    String[] concreteIndices = indexNameExpressionResolver.concreteIndices(state, request);
    boolean result = state.metaData().hasAliases(request.aliases(), concreteIndices);
    listener.onResponse(new AliasesExistResponse(result));
}
 
Example #11
Source File: AbstractClient.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public ActionFuture<AliasesExistResponse> aliasesExist(GetAliasesRequest request) {
    return execute(AliasesExistAction.INSTANCE, request);
}
 
Example #12
Source File: AbstractClient.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public void aliasesExist(GetAliasesRequest request, ActionListener<AliasesExistResponse> listener) {
    execute(AliasesExistAction.INSTANCE, request, listener);
}
 
Example #13
Source File: IndexTransaction.java    From es-service-parent with Apache License 2.0 4 votes vote down vote up
/**
 * 设置全量索引事务
 * <p>
 * 第一个if 为索引完全不存在,设置为全量索引
 * <p>
 * 第二个if 判断后缀为1的索引存在且否具备别名。即正在使用
 * <p>
 * 第三个if 判断后缀为2 的索引是否存在并具备别名,即正在使用
 * <p>
 * 其他,删除后缀为1的索引,并重建
 * 
 * @param transaction
 */
private void fullTransaction() {
    // 分片数
    Settings settings = ImmutableSettings.settingsBuilder()
            .put("number_of_shards", Constants.index_number_of_shards).build();
    IndicesAdminClient adminClient = ESClient.getClient().admin().indices();

    // 索引不存在,直接新创建索引
    if (!adminClient.prepareExists(indexType.index_type_1()).execute().actionGet().isExists()
            && !adminClient.prepareExists(indexType.index_type_2()).execute().actionGet()
                    .isExists()) {
        try {
            ESClient.getClient().admin().indices().prepareCreate(indexType.index_type_1())
                    .setSettings(settings).execute().actionGet();
            currentIndexName = indexType.index_type_1();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return;
    }

    // 存在索引indexname_1且indexname的别名指向它,同时indexname_2索引存在,则删除indexname_2然后重建indexname_2
    if (adminClient.prepareExists(indexType.index_type_1()).execute().actionGet().isExists()
            && adminClient
                    .aliasesExist(
                            new GetAliasesRequest().indices(indexType.index_type_1()).aliases(
                                    indexType.getIndexName())).actionGet().isExists()) {
        if (adminClient.prepareExists(indexType.index_type_2()).execute().actionGet()
                .isExists()) {
            adminClient.delete(new DeleteIndexRequest(indexType.index_type_2()));
        }
        adminClient.prepareCreate(indexType.index_type_2()).setSettings(settings).execute()
                .actionGet();
        currentIndexName = indexType.index_type_2();
        return;
    }
    // 存在索引 indexname_2且indexname的别名指向它,同时indexname_1索引存在,则删除indexname_1然后重建indexname_1
    if (adminClient.prepareExists(indexType.index_type_2()).execute().actionGet().isExists()
            && adminClient
                    .aliasesExist(
                            new GetAliasesRequest().indices(indexType.index_type_2()).aliases(
                                    indexType.getIndexName())).actionGet().isExists()) {
        if (adminClient.prepareExists(indexType.index_type_1()).execute().actionGet()
                .isExists()) {
            adminClient.delete(new DeleteIndexRequest(indexType.index_type_1()));
        }
        adminClient.prepareCreate(indexType.index_type_1()).setSettings(settings).execute()
                .actionGet();
        currentIndexName = indexType.index_type_1();
        return;
    }

    // 创建indexname_1索引,存在索引 indexname_1则删除然后重建
    if (adminClient.prepareExists(indexType.index_type_1()).execute().actionGet().isExists()) {
        adminClient.delete(new DeleteIndexRequest(indexType.index_type_1()));
    }
    adminClient.prepareCreate(indexType.index_type_1()).setSettings(settings).execute()
            .actionGet();
    currentIndexName = indexType.index_type_1();
}
 
Example #14
Source File: IndexTransaction.java    From es-service-parent with Apache License 2.0 4 votes vote down vote up
/**
 * 设置增量索引
 * <p>
 * 第一个if ,为索引完全不存在,设置为全量索引
 * <p>
 * 第二个if ,判断后缀为1的索引存在且否具备别名。即正在使用
 * <p>
 * 第三个if,判断后缀为2 的索引是否存在并具备别名,即正在使用
 * <p>
 * 其他,重置状态为全量
 * 
 * @param transaction
 */
private void incrementTransaction() {

    // 分片数
    Settings settings = ImmutableSettings.settingsBuilder()
            .put("number_of_shards", Constants.index_number_of_shards).build();
    IndicesAdminClient adminClient = ESClient.getClient().admin().indices();

    // 索引不存在,直接新创建索引
    if (!adminClient.prepareExists(indexType.index_type_1()).execute().actionGet().isExists()
            && !adminClient.prepareExists(indexType.index_type_2()).execute().actionGet()
                    .isExists()) {
        adminClient.prepareCreate(indexType.index_type_1()).setSettings(settings).execute()
                .actionGet();
        currentIndexName = indexType.index_type_1();
        needFull = true;
        return;
    }
    // 存在索引 indexname_1且indexname的别名指向它,则设置有效索引为indexname_1
    if (adminClient.prepareExists(indexType.index_type_1()).execute().actionGet().isExists()
            && adminClient
                    .aliasesExist(
                            new GetAliasesRequest().indices(indexType.index_type_1()).aliases(
                                    indexType.getIndexName())).actionGet().isExists()) {
        currentIndexName = indexType.index_type_1();
        needFull = false;
        return;
    }

    // 存在索引 indexname_2且indexname的别名指向它,则设置有效索引为indexname_2
    if (adminClient.prepareExists(indexType.index_type_2()).execute().actionGet().isExists()
            && adminClient
                    .aliasesExist(
                            new GetAliasesRequest().indices(indexType.index_type_2()).aliases(
                                    indexType.getIndexName())).actionGet().isExists()) {
        currentIndexName = indexType.index_type_2();
        needFull = false;
        return;
    }

    // 存在索引indexname_1和indexname_2,重建indexname_1,currentIndexName指向indexname_1
    if (adminClient.prepareExists(indexType.index_type_1()).execute().actionGet().isExists()
            && adminClient.prepareExists(indexType.index_type_2()).execute().actionGet()
                    .isExists()) {
        adminClient.delete(new DeleteIndexRequest(indexType.index_type_1()));
        adminClient.prepareCreate(indexType.index_type_1()).setSettings(settings).execute()
                .actionGet();
        currentIndexName = indexType.index_type_1();
        needFull = true;
        return;
    }

    // 存在索引 indexname_1,且不存在indexname_2,重建index_name_2,currentIndexName指向indexname_2
    if (adminClient.prepareExists(indexType.index_type_1()).execute().actionGet().isExists()) {
        adminClient.prepareCreate(indexType.index_type_2()).setSettings(settings).execute()
                .actionGet();
        currentIndexName = indexType.index_type_2();
        needFull = true;
        return;
    } else {
        // 创建indexname_1,currentIndexName指向indexname_1
        adminClient.prepareCreate(indexType.index_type_1()).setSettings(settings).execute()
                .actionGet();
        currentIndexName = indexType.index_type_1();
        needFull = true;
    }
}
 
Example #15
Source File: AbstractClient.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public void getAliases(GetAliasesRequest request, ActionListener<GetAliasesResponse> listener) {
    execute(GetAliasesAction.INSTANCE, request, listener);
}
 
Example #16
Source File: IndexCreationFactory.java    From james-project with Apache License 2.0 4 votes vote down vote up
private boolean aliasExist(ReactorElasticSearchClient client, AliasName aliasName) throws IOException {
    return client.indices()
        .existsAlias(new GetAliasesRequest().aliases(aliasName.getValue()), RequestOptions.DEFAULT);
}
 
Example #17
Source File: AbstractClient.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public ActionFuture<GetAliasesResponse> getAliases(GetAliasesRequest request) {
    return execute(GetAliasesAction.INSTANCE, request);
}
 
Example #18
Source File: IndicesAdminClient.java    From Elasticsearch with Apache License 2.0 2 votes vote down vote up
/**
 * Check to existence of index aliases.
 *
 * @param request The result future
 */
ActionFuture<AliasesExistResponse> aliasesExist(GetAliasesRequest request);
 
Example #19
Source File: IndicesAdminClient.java    From Elasticsearch with Apache License 2.0 2 votes vote down vote up
/**
 * Check the existence of specified index aliases.
 *
 * @param request  The index aliases request
 * @param listener A listener to be notified with a result
 */
void aliasesExist(GetAliasesRequest request, ActionListener<AliasesExistResponse> listener);
 
Example #20
Source File: IndicesAdminClient.java    From Elasticsearch with Apache License 2.0 2 votes vote down vote up
/**
 * Get specific index aliases that exists in particular indices and / or by name.
 *
 * @param request  The index aliases request
 * @param listener A listener to be notified with a result
 */
void getAliases(GetAliasesRequest request, ActionListener<GetAliasesResponse> listener);
 
Example #21
Source File: IndicesAdminClient.java    From Elasticsearch with Apache License 2.0 2 votes vote down vote up
/**
 * Get specific index aliases that exists in particular indices and / or by name.
 *
 * @param request The result future
 */
ActionFuture<GetAliasesResponse> getAliases(GetAliasesRequest request);