io.searchbox.indices.aliases.AddAliasMapping Java Examples

The following examples show how to use io.searchbox.indices.aliases.AddAliasMapping. 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: Jest.java    From easy-sync with Apache License 2.0 5 votes vote down vote up
public boolean bindAliasAndIndex(String alias, String indexName) throws IOException {
    logger.info("begin to bind index "+indexName+" to alias "+alias);
    List<AliasMapping> list = new ArrayList<>();
    AliasIndices aliasIndices = new AliasIndices(alias);
    JestResult jestResult = jestClient.execute(aliasIndices);

    if (jestResult.isSucceeded()) {
        String[] indexNames = aliasIndices.parse(jestResult);
        if(indexNames.length==1 && indexNames[0].equalsIgnoreCase(indexName)){
            logger.warn("old index is same as new index : {}, ignore binding.",indexName);
            return true;
        }
        for (String s : indexNames) {
            logger.info("old index "+s);
            RemoveAliasMapping removeAliasMapping = new RemoveAliasMapping.Builder(s, alias).build();
            list.add(removeAliasMapping);
        }
    } else if (jestResult.getResponseCode() == 404) {
        logger.info("no alias found, ignore");
    } else {

        logger.error(jestResult.getErrorMessage());
        return false;
    }


    AddAliasMapping addAliasMapping = new AddAliasMapping.Builder(indexName, alias).build();
    list.add(addAliasMapping);
    ModifyAliases modifyAliases = new ModifyAliases.Builder(list).build();
    JestResult jr = jestClient.execute(modifyAliases);
    if (jr.isSucceeded()) {
        return true;

    } else {
        logger.error("modifyAliases  error :" + jr.getErrorMessage());
        return false;
    }


}