Search in sources :

Example 1 with JSONConfig

use of cn.hutool.json.JSONConfig in project kms by mahonelau.

the class KmDocServiceImpl method saveDocToEs.

private Result<?> saveDocToEs(KmDocEsVO kmDocEsVO, String indexId) {
    try {
        boolean indexExistFlag = true;
        if (indexId != null && !indexId.isEmpty()) {
            // 通过索引id查询
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            QueryBuilder queryBuilder = QueryBuilders.idsQuery().addIds(indexId);
            searchSourceBuilder.query(queryBuilder);
            // 超时 10S
            searchSourceBuilder.timeout(new TimeValue(KMConstant.SearchTimeOutSeconds, TimeUnit.SECONDS));
            SearchRequest searchRequest = new SearchRequest();
            searchRequest.source(searchSourceBuilder);
            searchRequest.indices(KMConstant.DocIndexAliasName);
            SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
            if (searchResponse.status() != RestStatus.OK) {
                return Result.error("从ES查询文档索引失败");
            } else {
                long c = searchResponse.getHits().getTotalHits().value;
                if (c == 0) {
                    indexExistFlag = false;
                } else {
                    // 更新ES记录
                    UpdateRequest updateRequest = new UpdateRequest(KMConstant.DocIndexAliasName, indexId);
                    updateRequest.timeout(TimeValue.timeValueHours(KMConstant.SaveTimeOutHours));
                    updateRequest.doc(new JSONObject(kmDocEsVO, new JSONConfig().setDateFormat(DatePattern.NORM_DATE_PATTERN)).toString(), XContentType.JSON);
                    updateRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
                    UpdateResponse updateResponse = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
                    if (!updateResponse.status().equals(RestStatus.OK)) {
                        return Result.error("更新ES发生错误,返回码[" + updateResponse.status().toString() + "]");
                    } else {
                        return Result.OK();
                    }
                }
            }
        } else {
            indexExistFlag = false;
        }
        if (!indexExistFlag) {
            // 插入数据,index不存在则自动根据匹配到的template创建。index没必要每天创建一个,如果是为了灵活管理,最低建议每月一个 yyyyMM。
            IndexRequest indexRequest = new IndexRequest(KMConstant.DocIndexName);
            // 考虑大文件,允许1小时超时时间,前提是异步执行入库ES
            indexRequest.timeout(TimeValue.timeValueHours(KMConstant.SaveTimeOutHours));
            indexRequest.source(new JSONObject(kmDocEsVO, new JSONConfig().setDateFormat(DatePattern.NORM_DATE_PATTERN)).toString(), XContentType.JSON);
            IndexResponse response = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
            if (!response.status().equals(RestStatus.CREATED)) {
                return Result.error("入库ES发生错误,返回码[" + response.status().toString() + "]");
            } else {
                return Result.OK(response.getId());
            }
        } else {
            return Result.error("未知错误");
        }
    } catch (Exception e) {
        return Result.error("操作ES发生异常:" + e.getMessage());
    }
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) IndexRequest(org.elasticsearch.action.index.IndexRequest) ParseException(java.text.ParseException) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) UpdateResponse(org.elasticsearch.action.update.UpdateResponse) JSONObject(cn.hutool.json.JSONObject) IndexResponse(org.elasticsearch.action.index.IndexResponse) JSONConfig(cn.hutool.json.JSONConfig) TimeValue(org.elasticsearch.common.unit.TimeValue)

Example 2 with JSONConfig

use of cn.hutool.json.JSONConfig in project kykms by mahonelau.

the class KmSearchRecordServiceImpl method saveToEs.

private void saveToEs(KmSearchRecordEsVO kmSearchRecordEsVO) {
    try {
        // 插入数据,index不存在则自动根据匹配到的template创建。index没必要每天创建一个,如果是为了灵活管理,最低建议每月一个 yyyyMM。
        String indexSuffix = KMDateUtils.formatDateyyyyMM(DateUtils.getDate());
        IndexRequest indexRequest = new IndexRequest(KMConstant.KMSearchRecordIndexName + "_" + indexSuffix);
        indexRequest.timeout(TimeValue.timeValueHours(KMConstant.SaveTimeOutMinutes));
        indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
        indexRequest.source(new JSONObject(kmSearchRecordEsVO, new JSONConfig().setDateFormat(DatePattern.NORM_DATETIME_PATTERN)).toString(), XContentType.JSON);
        IndexResponse response = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
        if (!response.status().equals(RestStatus.CREATED)) {
            log.error("入库ES发生错误,返回码:" + response.status().toString());
        } else
            log.debug("搜索记录入库ES成功");
    } catch (Exception e) {
        log.error("入库ES发生错误", e);
    }
}
Also used : JSONObject(cn.hutool.json.JSONObject) IndexResponse(org.elasticsearch.action.index.IndexResponse) JSONConfig(cn.hutool.json.JSONConfig) IndexRequest(org.elasticsearch.action.index.IndexRequest) IOException(java.io.IOException)

Example 3 with JSONConfig

use of cn.hutool.json.JSONConfig in project kms by mahonelau.

the class KmDocVisitRecordServiceImpl method saveToEs.

private void saveToEs(KmDocVisitRecordEsVO kmDocVisitRecordEsVO) {
    try {
        // 插入数据,index不存在则自动根据匹配到的template创建。index没必要每天创建一个,如果是为了灵活管理,最低建议每月一个 yyyyMM。
        String indexSuffix = KMDateUtils.formatDateyyyyMM(DateUtils.getDate());
        IndexRequest indexRequest = new IndexRequest(KMConstant.DocVisitIndexName + "_" + indexSuffix);
        indexRequest.timeout(TimeValue.timeValueHours(KMConstant.SaveTimeOutMinutes));
        indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
        indexRequest.source(new JSONObject(kmDocVisitRecordEsVO, new JSONConfig().setDateFormat(DatePattern.NORM_DATETIME_PATTERN)).toString(), XContentType.JSON);
        IndexResponse response = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
        if (!response.status().equals(RestStatus.CREATED)) {
            log.error("入库ES发生错误,返回码:" + response.status().toString());
        } else
            log.debug("访问记录入库ES成功");
    } catch (Exception e) {
        log.error("入库ES发生错误", e);
    }
}
Also used : JSONObject(cn.hutool.json.JSONObject) IndexResponse(org.elasticsearch.action.index.IndexResponse) JSONConfig(cn.hutool.json.JSONConfig) IndexRequest(org.elasticsearch.action.index.IndexRequest) IOException(java.io.IOException)

Example 4 with JSONConfig

use of cn.hutool.json.JSONConfig in project kms by mahonelau.

the class KmSearchRecordServiceImpl method saveToEs.

private void saveToEs(KmSearchRecordEsVO kmSearchRecordEsVO) {
    try {
        // 插入数据,index不存在则自动根据匹配到的template创建。index没必要每天创建一个,如果是为了灵活管理,最低建议每月一个 yyyyMM。
        String indexSuffix = KMDateUtils.formatDateyyyyMM(DateUtils.getDate());
        IndexRequest indexRequest = new IndexRequest(KMConstant.KMSearchRecordIndexName + "_" + indexSuffix);
        indexRequest.timeout(TimeValue.timeValueHours(KMConstant.SaveTimeOutMinutes));
        indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
        indexRequest.source(new JSONObject(kmSearchRecordEsVO, new JSONConfig().setDateFormat(DatePattern.NORM_DATETIME_PATTERN)).toString(), XContentType.JSON);
        IndexResponse response = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
        if (!response.status().equals(RestStatus.CREATED)) {
            log.error("入库ES发生错误,返回码:" + response.status().toString());
        } else
            log.debug("搜索记录入库ES成功");
    } catch (Exception e) {
        log.error("入库ES发生错误", e);
    }
}
Also used : JSONObject(cn.hutool.json.JSONObject) IndexResponse(org.elasticsearch.action.index.IndexResponse) JSONConfig(cn.hutool.json.JSONConfig) IndexRequest(org.elasticsearch.action.index.IndexRequest) IOException(java.io.IOException)

Example 5 with JSONConfig

use of cn.hutool.json.JSONConfig in project kykms by mahonelau.

the class KmDocServiceImpl method saveDocToEs.

private Result<?> saveDocToEs(KmDocEsVO kmDocEsVO, String indexId) {
    try {
        boolean indexExistFlag = true;
        if (indexId != null && !indexId.isEmpty()) {
            // 通过索引id查询
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            QueryBuilder queryBuilder = QueryBuilders.idsQuery().addIds(indexId);
            searchSourceBuilder.query(queryBuilder);
            // 超时 10S
            searchSourceBuilder.timeout(new TimeValue(KMConstant.SearchTimeOutSeconds, TimeUnit.SECONDS));
            SearchRequest searchRequest = new SearchRequest();
            searchRequest.source(searchSourceBuilder);
            searchRequest.indices(KMConstant.DocIndexAliasName);
            SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
            if (searchResponse.status() != RestStatus.OK) {
                return Result.error("从ES查询文档索引失败");
            } else {
                long c = searchResponse.getHits().getTotalHits().value;
                if (c == 0) {
                    indexExistFlag = false;
                } else {
                    // 更新ES记录
                    UpdateRequest updateRequest = new UpdateRequest(KMConstant.DocIndexAliasName, indexId);
                    updateRequest.timeout(TimeValue.timeValueHours(KMConstant.SaveTimeOutHours));
                    updateRequest.doc(new JSONObject(kmDocEsVO, new JSONConfig().setDateFormat(DatePattern.NORM_DATE_PATTERN)).toString(), XContentType.JSON);
                    updateRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
                    UpdateResponse updateResponse = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
                    if (!updateResponse.status().equals(RestStatus.OK)) {
                        return Result.error("更新ES发生错误,返回码[" + updateResponse.status().toString() + "]");
                    } else {
                        return Result.OK();
                    }
                }
            }
        } else {
            indexExistFlag = false;
        }
        if (!indexExistFlag) {
            // 插入数据,index不存在则自动根据匹配到的template创建。index没必要每天创建一个,如果是为了灵活管理,最低建议每月一个 yyyyMM。
            IndexRequest indexRequest = new IndexRequest(KMConstant.DocIndexName);
            // 考虑大文件,允许1小时超时时间,前提是异步执行入库ES
            indexRequest.timeout(TimeValue.timeValueHours(KMConstant.SaveTimeOutHours));
            indexRequest.source(new JSONObject(kmDocEsVO, new JSONConfig().setDateFormat(DatePattern.NORM_DATE_PATTERN)).toString(), XContentType.JSON);
            IndexResponse response = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
            if (!response.status().equals(RestStatus.CREATED)) {
                return Result.error("入库ES发生错误,返回码[" + response.status().toString() + "]");
            } else {
                return Result.OK(response.getId());
            }
        } else {
            return Result.error("未知错误");
        }
    } catch (Exception e) {
        return Result.error("操作ES发生异常:" + e.getMessage());
    }
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) IndexRequest(org.elasticsearch.action.index.IndexRequest) ParseException(java.text.ParseException) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) UpdateResponse(org.elasticsearch.action.update.UpdateResponse) JSONObject(cn.hutool.json.JSONObject) IndexResponse(org.elasticsearch.action.index.IndexResponse) JSONConfig(cn.hutool.json.JSONConfig) TimeValue(org.elasticsearch.common.unit.TimeValue)

Aggregations

JSONConfig (cn.hutool.json.JSONConfig)6 JSONObject (cn.hutool.json.JSONObject)6 IndexRequest (org.elasticsearch.action.index.IndexRequest)6 IndexResponse (org.elasticsearch.action.index.IndexResponse)6 IOException (java.io.IOException)4 ParseException (java.text.ParseException)2 SearchRequest (org.elasticsearch.action.search.SearchRequest)2 SearchResponse (org.elasticsearch.action.search.SearchResponse)2 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)2 UpdateResponse (org.elasticsearch.action.update.UpdateResponse)2 TimeValue (org.elasticsearch.common.unit.TimeValue)2 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)2 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)2 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)2