Java Code Examples for io.searchbox.client.JestResult#getResponseCode()

The following examples show how to use io.searchbox.client.JestResult#getResponseCode() . 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 6 votes vote down vote up
public boolean deleteIndex(String indexName) {
    if(StringUtils.isBlank(indexName)){
        logger.warn("index name is blank");
        return false;
    }
    JestResult jr = null;
    try {
        //logger.info("delete index "+indexName);
        jr = jestClient.execute(new DeleteIndex.Builder(indexName).build());
        if (jr.isSucceeded() || jr.getResponseCode()==404) {
            return true;
        }
        logger.error("delete index  error :" + jr.getErrorMessage());
    } catch (IOException e) {
        logger.error("", e);
    }

    return false;


}
 
Example 2
Source File: ElasticsearchHelperService.java    From xmfcn-spring-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * 删除索引
 *
 * @param indexName
 * @throws Exception
 */
public JestResult deleteIndex(EsModel es) {
    JestResult result = null;
    RetData aReturn = validateParms(es);
    if (aReturn.getCode() == ResultCodeMessage.FAILURE) {
        return result;
    }
    String message = es.getMessage();
    String indexName = es.getIndex();
    String type = es.getType();
    result = indicesExists(indexName);
    if (result == null) {
        return result;
    }
    int responseCode = result.getResponseCode();
    if (responseCode != 200) {
        return result;
    }
    try {
        result = jestClient.execute(new DeleteIndex.Builder(indexName).build());
    } catch (Exception e) {
        String exceptionMsg = StringUtil.getExceptionMsg(e);
        logger.info(exceptionMsg);

    }
    return result;
}
 
Example 3
Source File: ElasticsearchHelperService.java    From xmfcn-spring-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * 创建索引
 *
 * @param indexName
 * @throws Exception
 */
public JestResult createIndex(EsModel es) {
    JestResult result = null;
    RetData aReturn = validateParms(es);
    if (aReturn.getCode() == ResultCodeMessage.FAILURE) {
        return result;
    }
    String message = es.getMessage();
    String indexName = es.getIndex();
    String type = es.getType();
    result = indicesExists(indexName);
    if (result == null) {
        return result;
    }
    int responseCode = result.getResponseCode();
    try {
        if (responseCode == 404) {
            result = jestClient.execute(new CreateIndex.Builder(indexName).build());
            responseCode = result.getResponseCode();
            if (responseCode == 200) {
                result = createMapping(es);
            }
        }
    } catch (Exception e) {
        String exceptionMsg = StringUtil.getExceptionMsg(e);
        logger.info(exceptionMsg);

    }
    return result;
}
 
Example 4
Source File: ElasticsearchService.java    From xmfcn-spring-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * 保存单条数据到ES
 *
 * @param message
 * @return
 */
@RequestMapping("save")
public RetData save(@RequestBody EsModel es) {
    RetData retData = new RetData();
    RetData aReturn = elasticsearchHelperService.validateParms(es);
    if (aReturn.getCode() == ResultCodeMessage.FAILURE) {
        return aReturn;
    }
    String message = es.getMessage();
    String indexName = es.getIndex();
    String type = es.getType();
    try {
        JestResult jestResult = elasticsearchHelperService.createIndex(es);
        if (jestResult == null) {
            return aReturn;
        }
        int responseCode = jestResult.getResponseCode();
        if (responseCode != 200) {
            return aReturn;
        }
        Index index = new Index.Builder(message).index(indexName).type(type).build();
        DocumentResult result = jestClient.execute(index);
        responseCode = result.getResponseCode();
        if (responseCode == 200) {
            retData.setMessage(ResultCodeMessage.SUCCESS_MESSAGE);
            retData.setCode(ResultCodeMessage.SUCCESS);
        } else {
            retData.setMessage(result.getErrorMessage());
        }
    } catch (Exception e) {

        String msg = StringUtil.getExceptionMsg(e);
        StringBuilder builder = new StringBuilder();
        builder.append("saveBatch(保存集合数据到ES) 参数 es:").append(JSON.toJSONString(es)).append(" \n\n错误消息:").append(msg);
        logger.error(builder.toString());
        retData.setMessage(msg);
    }
    return retData;
}
 
Example 5
Source File: ElasticsearchService.java    From xmfcn-spring-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * getStatisticsCountByLevel(按日志级别统计时间内的数据)
 *
 * @param EsModel es
 *                startTime
 *                endTime
 * @return
 */
@RequestMapping("getStatisticsCountByLevel")
public JSONObject getStatisticsCountByLevel(@RequestBody EsModel es) {
    JSONObject resJson = null;
    try {
        Search search = elasticsearchHelperService.statisticsLevelCondition(es);
        if (search == null) {
            return resJson;
        }
        EsPage esPage = es.getEsPage();
        JestResult result = jestClient.execute(search);
        logger.info("getStatisticsCountByLevel " + result.getJsonString());
        int responseCode = -1;
        if (result != null) {
            responseCode = result.getResponseCode();
        }
        if (responseCode != 200) {
            logger.error("ES 搜索错误信息:" + result.getErrorMessage());
            return resJson;
        }
        resJson = elasticsearchHelperService.getLevelStatisticsResult(result);
    } catch (Exception e) {

        String msg = StringUtil.getExceptionMsg(e);
        StringBuilder builder = new StringBuilder();
        builder.append("getStatisticsCountByLevel(按日志级别统计时间内的数据) 参数 es:").append(JSON.toJSONString(es)).append(" \n\n错误消息:").append(msg);
        logger.error(builder.toString());
    }
    return resJson;
}
 
Example 6
Source File: ElasticsearchService.java    From xmfcn-spring-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * getStatisticsCountByDay(按天统计时间内的数据)
 *
 * @param EsModel es
 *                startTime
 *                endTime
 * @return
 */
@RequestMapping("getStatisticsCountByDay")
public JSONObject getStatisticsCountByDay(@RequestBody EsModel es) {
    JSONObject resJson = null;
    try {
        Search search = elasticsearchHelperService.statisticsDayCondition(es);
        if (search == null) {
            return resJson;
        }
        EsPage esPage = es.getEsPage();
        JestResult result = jestClient.execute(search);
        logger.info("getStatisticsCountByDay :" + result.getJsonString());
        int responseCode = -1;
        if (result != null) {
            responseCode = result.getResponseCode();
        }
        if (responseCode != 200) {
            logger.error("ES 搜索错误信息:" + result.getErrorMessage());
            return resJson;
        }
        resJson = elasticsearchHelperService.getDayStatisticsResult(result);
    } catch (Exception e) {

        String msg = StringUtil.getExceptionMsg(e);
        StringBuilder builder = new StringBuilder();
        builder.append("getStatisticsCountByDay(按天统计时间内的数据) 参数 es:").append(JSON.toJSONString(es)).append(" \n\n错误消息:").append(msg);
        logger.error(builder.toString());
    }
    return resJson;
}
 
Example 7
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;
    }


}
 
Example 8
Source File: ElasticsearchService.java    From xmfcn-spring-cloud with Apache License 2.0 4 votes vote down vote up
/**
 * saveBatch(保存集合数据到ES)
 *
 * @param message
 * @return
 */
@RequestMapping("saveBatch")
public RetData saveBatch(@RequestBody EsModel es) {
    RetData retData = new RetData();
    RetData aReturn = elasticsearchHelperService.validateParms(es);
    if (aReturn.getCode() == ResultCodeMessage.FAILURE) {
        return aReturn;
    }
    String message = es.getMessage();
    String indexName = es.getIndex();
    String type = es.getType();
    try {
        List<JSONObject> list = es.getList();
        if (list == null || list.size() <= 0) {
            retData.setMessage("list 消息不能为空");
            return retData;
        }
        JSONObject object = list.get(0);
        es.setMessage(JSON.toJSONString(object));
        JestResult jestResult = elasticsearchHelperService.createIndex(es);
        if (jestResult == null) {
            return aReturn;
        }
        int responseCode = jestResult.getResponseCode();
        if (responseCode != 200) {
            return aReturn;
        }
        Bulk.Builder bulk = new Bulk.Builder();
        for (JSONObject entity : list) {
            Index index = new Index.Builder(entity).index(indexName).type(type).build();
            bulk.addAction(index);
        }
        BulkResult result = jestClient.execute(bulk.build());
        responseCode = result.getResponseCode();
        if (responseCode == 200) {
            retData.setMessage(ResultCodeMessage.SUCCESS_MESSAGE);
            retData.setCode(ResultCodeMessage.SUCCESS);
        } else {
            String errorMessage = "saveBatch(保存集合数据到ES):" + result.getErrorMessage();
            logger.error(errorMessage);
            retData.setMessage(result.getErrorMessage());
        }
    } catch (Exception e) {

        String msg = StringUtil.getExceptionMsg(e);
        StringBuilder builder = new StringBuilder();
        builder.append("saveBatch(保存集合数据到ES) 参数 es:").append(JSON.toJSONString(es)).append(" \n\n错误消息:").append(msg);
        logger.error(builder.toString());
        retData.setMessage(msg);
    }
    return retData;
}
 
Example 9
Source File: Jest.java    From easy-sync with Apache License 2.0 3 votes vote down vote up
public boolean indexExists(String indexName) throws IOException {

        AliasIndices aliasIndices = new AliasIndices(indexName);
        JestResult jestResult = jestClient.execute(aliasIndices);

        //because index and alias can not be the same,    alias found , means index not exists
        if (jestResult.isSucceeded()) {
            return  false;
        } else if (jestResult.getResponseCode() == 404) {

        } else {

           throw new IOException(jestResult.getResponseCode()+","+jestResult.getErrorMessage());

        }



        jestResult = jestClient.execute(new IndicesExists.Builder(indexName).build());
            if (jestResult.isSucceeded()) {
                return true;
            }else  if(jestResult.getResponseCode()==404){
                    return false;

            }else{
                throw new IOException(jestResult.getResponseCode()+","+jestResult.getErrorMessage());
            }




    }