Search in sources :

Example 1 with ESException

use of com.sakura.common.es.exception.ESException in project sakura-boot by yanjingfan.

the class ESTemplate method getObjectString.

@Deprecated
public // GET /login_log-2020.06.17/_search?_source=user_name,temp_duration&sort=temp_duration:asc&q=user_name:admin&q=temp_duration:[140 TO *]
String getObjectString(FilterInfo filterInfo) {
    String indexName = filterInfo.getIndexName();
    Integer page = filterInfo.getPageNum();
    // Long endTime = filterInfo.getEndTime();
    // Long startTime = filterInfo.getStartTime();
    Integer pageSize = filterInfo.getPageSize();
    List<QueryField> queryFields = filterInfo.getQueryFields();
    List<String> selectFields = filterInfo.getSelectFields();
    String sortFiled = filterInfo.getSortFiled();
    String rangeFiled = filterInfo.getRangeFiled();
    String sortMethod = filterInfo.getSortMethod();
    String scrollId = filterInfo.getScrollId();
    Boolean openScroll = filterInfo.getScroll();
    RestTemplate restTemplate = new RestTemplate();
    StringBuilder sb = new StringBuilder("http://");
    sb.append(ESurl).append("/");
    if (!StringUtils.isEmpty(scrollId) && openScroll) {
        sb.append("/_search/scroll?scroll_id=");
        sb.append(scrollId).append("&scroll=1m");
        try {
            return restTemplate.getForObject(sb.toString(), String.class);
        } catch (Exception e) {
            throw new ESException("ES深分页查询出错!");
        }
    } else {
        sb.append(indexName);
        sb.append("/_search?");
        if (!CollectionUtils.isEmpty(selectFields)) {
            sb.append("_source=");
            StringBuilder sBuilder = new StringBuilder();
            selectFields.forEach(field -> sBuilder.append(field).append(","));
            String substring = sBuilder.substring(0, sBuilder.length() - 1);
            sb.append(substring);
        }
        // 搜索
        if (!CollectionUtils.isEmpty(queryFields)) {
            queryFields.forEach(field -> {
                sb.append("&q=").append(field.getKey()).append(":").append(field.getValue());
            });
        }
        // 默认降序排序
        if (!StringUtils.isEmpty(sortFiled)) {
            sb.append("&sort=").append(sortFiled).append(":").append(sortMethod);
        }
        // 分页
        if (page != null && page > 0) {
            if (page * pageSize > 10000) {
                page--;
                sb.append("&from=").append(10000 - pageSize);
            } else {
                page--;
                sb.append("&from=").append(page * pageSize);
            }
        }
        if (pageSize != null) {
            sb.append("&size=").append(pageSize);
        }
        try {
            return restTemplate.getForObject(sb.toString(), String.class);
        } catch (Exception e) {
            throw new ESException("ES通用查询出错!");
        }
    }
}
Also used : QueryField(com.sakura.common.es.item.QueryField) ESException(com.sakura.common.es.exception.ESException) RestTemplate(org.springframework.web.client.RestTemplate) JSONException(com.alibaba.fastjson2.JSONException) ESException(com.sakura.common.es.exception.ESException)

Example 2 with ESException

use of com.sakura.common.es.exception.ESException in project sakura-boot by yanjingfan.

the class ESTemplate method postObjectString.

/**
 * es的post请求
 * {
 *     "query": {
 *         "bool": {
 *             "must": [
 *                 {
 *                     "match_phrase": {
 *                         "age": 10
 *                     }
 *                 },
 *                 {
 *                     "range": {
 *                         "time.keyword": {
 *                             "gte": "2022-01-01 11:20:32",
 *                             "lte": "2022-12-24 11:20:32"
 *                         }
 *                     }
 *                 }
 *             ]
 *         }
 *     },
 *     "sort": [
 *         {
 *             "time.keyword": {
 *                 "order": "asc"
 *             }
 *         }
 *     ]
 * }
 * @param filterInfo
 * @return
 */
public String postObjectString(FilterInfo filterInfo) {
    String indexName = filterInfo.getIndexName();
    String end = filterInfo.getEnd();
    Integer page = filterInfo.getPageNum();
    String start = filterInfo.getStart();
    Integer pageSize = filterInfo.getPageSize();
    List<QueryField> queryFields = filterInfo.getQueryFields();
    List<String> selectFields = filterInfo.getSelectFields();
    String sortFiled = filterInfo.getSortFiled();
    Boolean sortIsStr = filterInfo.getSortIsStr();
    String rangeFiled = filterInfo.getRangeFiled();
    Boolean rangeIsStr = filterInfo.getRangeIsStr();
    String sortMethod = filterInfo.getSortMethod();
    String scrollId = filterInfo.getScrollId();
    Boolean openScroll = filterInfo.getScroll();
    RestTemplate restTemplate = new RestTemplate();
    StringBuilder sb = new StringBuilder("http://");
    sb.append(ESurl).append("/");
    if (!StringUtils.isEmpty(scrollId) && openScroll) {
        sb.append("/_search/scroll?scroll_id=");
        sb.append(scrollId).append("&scroll=1m");
        try {
            return restTemplate.getForObject(sb.toString(), String.class);
        } catch (Exception e) {
            throw new ESException("ES深分页查询出错!");
        }
    } else {
        sb.append(indexName);
        sb.append("/_search?");
        if (!CollectionUtils.isEmpty(selectFields)) {
            sb.append("_source=");
            StringBuilder sBuilder = new StringBuilder();
            selectFields.forEach(field -> sBuilder.append(field).append(","));
            String substring = sBuilder.substring(0, sBuilder.length() - 1);
            sb.append(substring);
        }
        JSONObject queryObject = new JSONObject();
        // 搜索
        JSONObject boolObject = new JSONObject();
        JSONObject mustObject = new JSONObject();
        JSONArray must = new JSONArray();
        if (!CollectionUtils.isEmpty(queryFields)) {
            queryFields.stream().forEach(item -> {
                JSONObject matchPhrase = new JSONObject();
                JSONObject match = new JSONObject();
                match.put(item.getKey(), item.getValue());
                matchPhrase.put("match_phrase", match);
                must.add(matchPhrase);
            });
        }
        // 默认降序排序,目前测试的时候,只有数值类的字段允许排序
        if (!StringUtils.isEmpty(sortFiled)) {
            JSONObject sortFiledObject = new JSONObject();
            JSONObject orderObject = new JSONObject();
            orderObject.put("order", sortMethod);
            // 排序字段为字符串时,后缀添加.keyword
            if (sortIsStr) {
                sortFiled = sortFiled + ".keyword";
            }
            sortFiledObject.put(sortFiled, orderObject);
            queryObject.put("sort", sortFiledObject);
        }
        // 范围查询
        if (!StringUtils.isEmpty(rangeFiled)) {
            JSONObject rangeObject = new JSONObject();
            JSONObject rangeFiledObject = new JSONObject();
            JSONObject range = new JSONObject();
            range.put("gte", start);
            range.put("lte", end);
            // 范围查询字段为字符串时,后缀添加.keyword
            if (rangeIsStr) {
                rangeFiled = rangeFiled + ".keyword";
            }
            rangeFiledObject.put(rangeFiled, range);
            rangeObject.put("range", rangeFiledObject);
            must.add(rangeObject);
        }
        // 分页
        if (page != null && page > 0) {
            if (page * pageSize > 10000) {
                page--;
                sb.append("&from=").append(10000 - pageSize);
            } else {
                page--;
                sb.append("&from=").append(page * pageSize);
            }
        }
        if (pageSize != null) {
            sb.append("&size=").append(pageSize);
        }
        mustObject.put("must", must);
        boolObject.put("bool", mustObject);
        queryObject.put("query", boolObject);
        return sendPostRequest(sb.toString(), queryObject);
    }
}
Also used : QueryField(com.sakura.common.es.item.QueryField) ESException(com.sakura.common.es.exception.ESException) JSONObject(com.alibaba.fastjson2.JSONObject) RestTemplate(org.springframework.web.client.RestTemplate) JSONArray(com.alibaba.fastjson2.JSONArray) JSONException(com.alibaba.fastjson2.JSONException) ESException(com.sakura.common.es.exception.ESException)

Aggregations

JSONException (com.alibaba.fastjson2.JSONException)2 ESException (com.sakura.common.es.exception.ESException)2 QueryField (com.sakura.common.es.item.QueryField)2 RestTemplate (org.springframework.web.client.RestTemplate)2 JSONArray (com.alibaba.fastjson2.JSONArray)1 JSONObject (com.alibaba.fastjson2.JSONObject)1