All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.holly.unit.es.EsOperator Maven / Gradle / Ivy

There is a newer version: 1.0.7
Show newest version
package com.holly.unit.es;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.holly.unit.core.util.RestUtil;
import com.holly.unit.es.api.EsApi;
import com.holly.unit.es.api.config.EsConfig;
import com.holly.unit.es.util.EsBaseUrlUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.*;

/**
 * 类描述: ES操作类
 *
 * @author fxr
 * @version 1.0.0
 * @date 2022/4/2 12:53
 */
@Slf4j
public class EsOperator implements EsApi {

    private EsConfig esConfig;

    public EsOperator(EsConfig esConfig) {
        this.esConfig = esConfig;
        log.debug("HollyElasticsearchTemplate BaseURL:" + esConfig.getClusterNodes());
        String baseUrl = esConfig.getClusterNodes();
        boolean checkEnabled = esConfig.isCheckEnabled();
        if (StrUtil.isNotEmpty(baseUrl)) {
            // 验证配置的ES地址是否有效
            if (checkEnabled) {
                try {
                    RestUtil.get(EsBaseUrlUtil.getBaseUrl(esConfig.getClusterNodes()).toString());
                    log.info("ElasticSearch 服务连接成功");
                } catch (Exception e) {
                    log.warn("ElasticSearch 服务连接失败,原因:配置未通过。可能是BaseURL未配置或配置有误,也可能是Elasticsearch服务未启动。接下来将会拒绝执行任何方法!");
                }
            }
        }
    }

    @Override
    public  ResponseEntity _cat(String urlAfter, Class responseType) {
        String url = EsBaseUrlUtil.getBaseUrl(esConfig.getClusterNodes()).append("/_cat").append(urlAfter).append("?").append(esConfig.getFormatJson()).toString();
        return RestUtil.request(url, HttpMethod.GET, null, null, null, responseType);
    }

    @Override
    public JSONArray getIndices() {
        return getIndices(null);
    }

    @Override
    public JSONArray getIndices(String indexName) {
        StringBuilder urlAfter = new StringBuilder("/indices");
        if (!StrUtil.isEmpty(indexName)) {
            urlAfter.append("/").append(indexName.trim().toLowerCase());
        }
        return _cat(urlAfter.toString(), JSONArray.class).getBody();
    }

    @Override
    public boolean indexExists(String indexName) {
        try {
            JSONArray array = getIndices(indexName);
            return array != null;
        } catch (org.springframework.web.client.HttpClientErrorException ex) {
            if (HttpStatus.NOT_FOUND == ex.getStatusCode()) {
                return false;
            } else {
                throw ex;
            }
        }
    }

    @Override
    public JSONObject getDataById(String indexName, String typeName, String dataId) {
        String url = EsBaseUrlUtil.getBaseUrl(indexName, typeName).append("/").append(dataId).toString();
        log.info("url:" + url);
        JSONObject result = RestUtil.get(url);
        boolean found = result.getBoolean("found");
        if (found) {
            return result.getJSONObject("_source");
        } else {
            return null;
        }
    }

    @Override
    public boolean createIndex(String indexName) {
        String url = EsBaseUrlUtil.getBaseUrl(indexName).toString();
        try {
            return RestUtil.put(url).getBoolean("acknowledged");
        } catch (org.springframework.web.client.HttpClientErrorException ex) {
            if (HttpStatus.BAD_REQUEST == ex.getStatusCode()) {
                log.warn("索引创建失败:" + indexName + " 已存在,无需再创建");
            } else {
                ex.printStackTrace();
            }
        }
        return false;
    }

    @Override
    public boolean removeIndex(String indexName) {
        String url = EsBaseUrlUtil.getBaseUrl(indexName).toString();
        try {
            return RestUtil.delete(url).getBoolean("acknowledged");
        } catch (org.springframework.web.client.HttpClientErrorException ex) {
            if (HttpStatus.NOT_FOUND == ex.getStatusCode()) {
                log.warn("索引删除失败:" + indexName + " 不存在,无需删除");
            } else {
                ex.printStackTrace();
            }
        }
        return false;
    }

    @Override
    public JSONObject getIndexMapping(String indexName, String typeName) {
        String url = EsBaseUrlUtil.getBaseUrl(indexName, typeName).append("/_mapping?").append(esConfig.getFormatJson()).toString();
        log.info("getIndexMapping-url:" + url);
        try {
            return RestUtil.get(url);
        } catch (org.springframework.web.client.HttpClientErrorException e) {
            String message = e.getMessage();
            if (message != null && message.contains("404 Not Found")) {
                return null;
            }
            throw e;
        }
    }

    @Override
    public  Map getIndexMappingFormat(String indexName, String typeName, Class clazz) {
        JSONObject mapping = this.getIndexMapping(indexName, typeName);
        Map map = new HashMap<>();
        if (mapping == null) {
            return map;
        }
        // 获取字段属性
        JSONObject properties = mapping.getJSONObject(indexName)
                .getJSONObject("mappings")
                .getJSONObject(typeName)
                .getJSONObject("properties");
        // 封装成 java类型
        for (String key : properties.keySet()) {
            T entity = properties.getJSONObject(key).toJavaObject(clazz);
            map.put(key, entity);
        }
        return map;
    }

    @Override
    public boolean save(String indexName, String typeName, String dataId, JSONObject data) {
        return this.saveOrUpdate(indexName, typeName, dataId, data);
    }

    @Override
    public boolean update(String indexName, String typeName, String dataId, JSONObject data) {
        return this.saveOrUpdate(indexName, typeName, dataId, data);
    }

    @Override
    public boolean saveOrUpdate(String indexName, String typeName, String dataId, JSONObject data) {
        String url = EsBaseUrlUtil.getBaseUrl(indexName, typeName).append("/").append(dataId).append("?refresh=wait_for").toString();
        try {
            // 去掉 data 中为空的值
            Set keys = data.keySet();
            List emptyKeys = new ArrayList<>(keys.size());
            for (String key : keys) {
                String value = data.getString(key);
                //1、剔除空值
                if (StrUtil.isEmpty(value) || "[]".equals(value)) {
                    emptyKeys.add(key);
                }
                //2、剔除上传控件值(会导致ES同步失败,报异常failed to parse field [ge_pic] of type [text] )
                if (StrUtil.isNotEmpty(value) && value.indexOf("[{")!=-1) {
                    emptyKeys.add(key);
                    log.info("-------剔除上传控件字段------------key: "+ key);
                }
            }
            for (String key : emptyKeys) {
                data.remove(key);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            String result = RestUtil.put(url, data).getString("result");
            return "created".equals(result) || "updated".equals(result);
        } catch (Exception e) {
            log.error(e.getMessage() + "\n-- url: " + url + "\n-- data: " + data.toJSONString());
            //TODO 打印接口返回异常json
            return false;
        }
    }

    @Override
    public boolean delete(String indexName, String typeName, String dataId) {
        String url = EsBaseUrlUtil.getBaseUrl(indexName, typeName).append("/").append(dataId).toString();
        try {
            return "deleted".equals(RestUtil.delete(url).getString("result"));
        } catch (org.springframework.web.client.HttpClientErrorException ex) {
            if (HttpStatus.NOT_FOUND == ex.getStatusCode()) {
                return false;
            } else {
                throw ex;
            }
        }
    }

    @Override
    public JSONObject search(String indexName, String typeName, JSONObject queryObject) {
        String url = EsBaseUrlUtil.getBaseUrl(indexName, typeName).append("/_search").toString();

        log.info("url:" + url + " ,search: " + queryObject.toJSONString());
        JSONObject res = RestUtil.post(url, queryObject);
        log.info("url:" + url + " ,return res: \n" + res.toJSONString());
        return res;
    }

    @Override
    public JSONObject buildQuery(List _source, JSONObject query, int from, int size) {
        JSONObject json = new JSONObject();
        if (_source != null) {
            json.put("_source", _source);
        }
        json.put("query", query);
        json.put("from", from);
        json.put("size", size);
        return json;
    }

    @Override
    public JSONObject buildBoolQuery(JSONArray must, JSONArray mustNot, JSONArray should) {
        JSONObject bool = new JSONObject();
        if (must != null) {
            bool.put("must", must);
        }
        if (mustNot != null) {
            bool.put("must_not", mustNot);
        }
        if (should != null) {
            bool.put("should", should);
        }
        JSONObject json = new JSONObject();
        json.put("bool", bool);
        return json;
    }

    @Override
    public JSONObject buildQueryString(String field, String... args) {
        if (field == null) {
            return null;
        }
        StringBuilder sb = new StringBuilder(field).append(":(");
        if (args != null) {
            for (String arg : args) {
                sb.append(arg).append(" ");
            }
        }
        sb.append(")");
        return this.buildQueryString(sb.toString());
    }

    @Override
    public JSONObject buildQueryString(String query) {
        JSONObject queryString = new JSONObject();
        queryString.put("query", query);
        JSONObject json = new JSONObject();
        json.put("query_string", queryString);
        return json;
    }

    @Override
    public JSONObject buildRangeQuery(String field, Object min, Object max, boolean containMin, boolean containMax) {
        JSONObject inner = new JSONObject();
        if (min != null) {
            if (containMin) {
                inner.put("gte", min);
            } else {
                inner.put("gt", min);
            }
        }
        if (max != null) {
            if (containMax) {
                inner.put("lte", max);
            } else {
                inner.put("lt", max);
            }
        }
        JSONObject range = new JSONObject();
        range.put(field, inner);
        JSONObject json = new JSONObject();
        json.put("range", range);
        return json;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy