Elasticsearch类

package com.group.task.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.group.common.entity.dto.TglGroupUserDto;
import com.group.common.entity.vo.UserInfoVo;
import com.group.common.util.PageUtils;
import com.group.task.model.GroupUserUpateParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.Requests;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchPhraseQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.reindex.BulkByScrollResponse;
import org.elasticsearch.index.reindex.DeleteByQueryAction;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.metrics.max.InternalMax;
import org.elasticsearch.search.aggregations.metrics.max.MaxAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.sum.InternalSum;
import org.elasticsearch.search.aggregations.metrics.sum.SumAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;

/**
 * 描述: Elasticsearch 工具类
 *
 * @author lm
 **/
@Slf4j
@Component
public class ElasticsearchService {

    @Autowired
    private TransportClient transportClient;

    /**
     * 创建索引
     *
     * @param index
     * @return
     */
    public  boolean createIndex(String index) {
        if (!isIndexExist(index)) {
            log.info("Index不存在,index:{}", index);
        }
        //        transportClient.admin().indices().prepareAliases().execute().actionGet();
        CreateIndexResponse indexresponse = transportClient.admin().indices().prepareCreate(index).execute().actionGet();
        log.info("执行建立成功?:{}", indexresponse.isAcknowledged());
        return indexresponse.isAcknowledged();
    }

    /**
     * 创建mapping
     * 
     * @return
     */
    public  boolean createIKMapping(String indexName, String type, Map<String, Map<String, String>> proNames) {

        XContentBuilder mapping = null;

        try {
            mapping = jsonBuilder().startObject().startObject(type).startObject("properties");
            if (!proNames.isEmpty()) {

                for (Map.Entry<String, Map<String, String>> proName : proNames.entrySet()) {

                    if (null != proName.getKey()) {

                        mapping.startObject(proName.getKey());

                        if (!proName.getValue().isEmpty()) {

                            for (Map.Entry<String, String> filed : proName.getValue().entrySet()) {

                                String pro = filed.getKey();

                                String value = filed.getValue();

                                if (null != pro && null != value) {

                                    mapping.field(pro, value);

                                }

                            }
                        }
                        mapping.endObject();
                    }

                }
            }
            mapping.endObject().endObject().endObject();
            PutMappingRequest putmap = Requests.putMappingRequest(indexName).type(type).source(mapping);
            transportClient.admin().indices().putMapping(putmap).actionGet();
        } catch (IOException e) {
            log.info("创建ik mapping 失败:{}", e.toString());
            return false;
        }
        return true;
    }

    /**
     * 删除索引
     *
     * @param index
     * @return
     */
    public  boolean deleteIndex(String index) {
        if (!isIndexExist(index)) {
            log.info("index不存在:{}", index);
        }
        DeleteIndexResponse dResponse = transportClient.admin().indices().prepareDelete(index).execute().actionGet();
        if (dResponse.isAcknowledged()) {
            log.info("删除index成功,index:{}", index);
        } else {
            log.info("删除index失败,index:{}", index);
        }
        return dResponse.isAcknowledged();
    }

    /**
     * 判断索引是否存在
     *
     * @param index
     * @return
     */
    public  boolean isIndexExist(String index) {
        IndicesExistsResponse inExistsResponse = transportClient.admin().indices().exists(new IndicesExistsRequest(index))
                .actionGet();
        if (inExistsResponse.isExists()) {
            log.info("该index存在,index:{}", index);
        } else {
            log.info("该index不存在,index:{}", index);
        }
        return inExistsResponse.isExists();
    }

    /**
     * 数据批量添加
     *
     * @param list 要增加的数据集合
     * @param index 索引,类似数据库
     * @param type 类型,类似表
     * @return
     */
    public  <T> void batchAddData(List<T> list, String index, String type) {
        BulkRequestBuilder builder = transportClient.prepareBulk();
        list.forEach(e -> {
            builder.add(transportClient.prepareIndex(index, type, UUID.randomUUID().toString().replaceAll("-", "").toUpperCase())
                    .setSource(JSON.toJSONString(e), XContentType.JSON));
        });
        builder.execute().actionGet();
    }

    /**
     * 批量处理群内成员数据
     * @param list
     * @param index
     * @param type
     */
    public  void blukGroupUserNameData(List<TglGroupUserDto> list, String index, String type) {
        try {
            BulkRequestBuilder bulkRequest = transportClient.prepareBulk();
            for (TglGroupUserDto json : list) {
                String jsonString = JSON.toJSON(json).toString();
                // index  type  _id
                bulkRequest.add(transportClient.prepareIndex(index, type,json.getUserId().toString())
                        .setSource(
                                jsonString, XContentType.JSON
                        )
                );
            }
            BulkResponse response = bulkRequest.get();
            log.info("[blukGroupUserNameData] size:{},response:{}",list.size(),response.status());
        }catch (Exception e){
            log.error("[blukGroupUserNameData] error:{},list:{}",e,Arrays.asList(list));
        }
    }

    /**
     * 批量添加群内用户
     * @param list
     * @param index
     * @param type
     */
    public  void blukGroupUserData(List<TglGroupUserDto> list, String index, String type) {
        try {
            BulkRequestBuilder bulkRequest = transportClient.prepareBulk();
            for (TglGroupUserDto json : list) {
                // index  type  _id
                bulkRequest.add(transportClient.prepareIndex(index, type,json.getIds())
                        .setSource(
                                XContentFactory.jsonBuilder()
                                        .startObject()
                                        .field("gid",json.getGid().intValue())
                                        .field("userId",json.getUserId().intValue())
                                        .field("ban",json.getBan())
                                        .endObject()
                        )
                );
            }
            BulkResponse response = bulkRequest.get();
            log.info("[blukGroupUserData] size:{},response:{}",list.size(),response.status());
        }catch (Exception e){
            log.error("[blukGroupUserData] error:{},list:{}",e,Arrays.asList(list));
        }
    }


    /**
     * 批量添加用户数据
     * @param list
     * @param index
     * @param type
     */
    public  void blukUserData(List<UserInfoVo> list, String index, String type) {
        try {
            BulkRequestBuilder bulkRequest = transportClient.prepareBulk();
            for (UserInfoVo json : list) {
                // index  type  _id
                bulkRequest.add(transportClient.prepareIndex(index, type,json.getUserId().toString()).setSource(
                        JSON.parseObject(JSON.toJSONString(json))
                ));
            }
            BulkResponse response = bulkRequest.get();
            log.info("[blukUserData] size:{},response:{}",list.size(),response.status());
        }catch (Exception e){
            log.error("[blukUserData] error:{},list:{}",e,Arrays.asList(list));
        }
    }

    /**
     * 批量更新群内用户
     * @param list
     * @param index
     * @param type
     */
    public  void blukUpateGroupUserData(List<GroupUserUpateParam> list, String index, String type) {
        try {
            BulkRequestBuilder bulkRequest = transportClient.prepareBulk();
            for (GroupUserUpateParam json : list) {
                // index  type  _id
                UpdateRequest updateRequest = new UpdateRequest();
                updateRequest.index(index).type(type).id(json.getIds()).doc(JSON.parseObject(JSON.toJSONString(json)));
                bulkRequest.add(updateRequest);
            }
            BulkResponse response = bulkRequest.get();
            log.info("[blukUpateGroupUserData] size:{},response:{}",list.size(),response.status());
        }catch (Exception e){
            log.error("[blukUpateGroupUserData] error:{},list:{}",e,Arrays.asList(list));
        }
    }


    /**
     * 数据添加
     *
     * @param json 要增加的数据
     * @param index 索引,类似数据库
     * @param type 类型,类似表
     * @return
     */
    public  String addData(String json, String index, String type) {
        IndexResponse response = transportClient
                .prepareIndex(index, type, UUID.randomUUID().toString().replaceAll("-", "").toUpperCase())
                .setSource(json, XContentType.JSON).get();
        log.info("添加数据 status:{},id:{}", response.status().getStatus(), response.getId());
        return response.getId();
    }




    /**
     * 数据添加
     *
     * @param jsonObject 要增加的数据
     * @param index 索引,类似数据库
     * @param type 类型,类似表
     * @param id 数据ID
     * @return
     */
    public  String addData(JSONObject jsonObject, String index, String type, String id) {
        IndexResponse response = transportClient.prepareIndex(index, type, id).setSource(jsonObject).get();
        log.info("添加数据 status:{},jsonObject:{}", response.status().getStatus(), jsonObject.toJSONString());
        return response.getId();
    }

    /**
     * 数据添加
     *
     * @param jsonObject 要增加的数据
     * @param index 索引,类似数据库
     * @param type 类型,类似表
     * @return
     */
    public  String addData(JSONObject jsonObject, String index, String type) {
        log.info("添加数据 jsonObject:{},index:{},type:{}", jsonObject, index, type);
        return addData(jsonObject, index, type, UUID.randomUUID().toString().replaceAll("-", "").toUpperCase());
    }

    /**
     * 通过ID删除数据
     *
     * @param index 索引,类似数据库
     * @param type 类型,类似表
     * @param id 数据ID
     */
    public  void deleteDataById(String index, String type, String id) {
        DeleteResponse response = transportClient.prepareDelete(index, type, id).execute().actionGet();
        log.info("通过ID删除数据 status:{},id:{}", response.status().getStatus(), response.getId());
    }

    /**
     * 通过ID 更新数据
     *
     * @param jsonObject 要增加的数据
     * @param index 索引,类似数据库
     * @param type 类型,类似表
     * @param id 数据ID
     * @return
     */
    public  void updateDataById(JSONObject jsonObject, String index, String type, String id) {
        UpdateRequest updateRequest = new UpdateRequest();
        updateRequest.index(index).type(type).id(id).doc(jsonObject);
        transportClient.update(updateRequest);
        log.info("通过ID更新数据 jsonObject:{},index:{},type:{},ID:{}", jsonObject, index, type, id);
    }



    /**
     * 通过ID获取数据
     *
     * @param index 索引,类似数据库
     * @param type 类型,类似表
     * @param id 数据ID
     * @param fields 需要显示的字段,逗号分隔(缺省为全部字段)
     * @return
     */
    public  Map<String, Object> searchDataById(String index, String type, String id, String fields) {

        GetRequestBuilder getRequestBuilder = transportClient.prepareGet(index, type, id);

        if (StringUtils.isNotEmpty(fields)) {
            getRequestBuilder.setFetchSource(fields.split(","), null);
        }

        GetResponse getResponse = getRequestBuilder.execute().actionGet();

        return getResponse.getSource();
    }

    /**
     * 使用分词查询
     *
     * @param index 索引名称
     * @param type 类型名称,可传入多个type逗号分隔
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param size 文档大小限制
     * @param matchStr 过滤条件(xxx=111,aaa=222)
     * @return
     */
    public  List<Map<String, Object>> searchListData(String index, String type, long startTime, long endTime,
                                                           Integer size, String matchStr) {
        return searchListData(index, type, startTime, endTime, size, null, null, false, null, matchStr);
    }

    /**
     * 使用分词查询
     *
     * @param index 索引名称
     * @param type 类型名称,可传入多个type逗号分隔
     * @param size 文档大小限制
     * @param fields 需要显示的字段,逗号分隔(缺省为全部字段)
     * @param matchStr 过滤条件(xxx=111,aaa=222)
     * @return
     */
    public  List<Map<String, Object>> searchListData(String index, String type, Integer size, String fields,
                                                           String matchStr) {
        return searchListData(index, type, 0, 0, size, fields, null, false, null, matchStr);
    }

    /**
     * 使用分词查询
     *
     * @param index 索引名称
     * @param type 类型名称,可传入多个type逗号分隔
     * @param size 文档大小限制
     * @param fields 需要显示的字段,逗号分隔(缺省为全部字段)
     * @param sortField 排序字段
     * @param matchPhrase true 使用,短语精准匹配
     * @param matchStr 过滤条件(xxx=111,aaa=222)
     * @return
     */
    public  List<Map<String, Object>> searchListData(String index, String type, Integer size, String fields,
                                                           String sortField, boolean matchPhrase, String matchStr) {
        return searchListData(index, type, 0, 0, size, fields, sortField, matchPhrase, null, matchStr);
    }

    /**
     * 使用分词查询
     *
     * @param index 索引名称
     * @param type 类型名称,可传入多个type逗号分隔
     * @param size 文档大小限制
     * @param fields 需要显示的字段,逗号分隔(缺省为全部字段)
     * @param sortField 排序字段
     * @param matchPhrase true 使用,短语精准匹配
     * @param highlightField 高亮字段
     * @param matchStr 过滤条件(xxx=111,aaa=222)
     * @return
     */
    public  List<Map<String, Object>> searchListData(String index, String type, Integer size, String fields,
                                                           String sortField, boolean matchPhrase, String highlightField,
                                                           String matchStr) {
        return searchListData(index, type, 0, 0, size, fields, sortField, matchPhrase, highlightField, matchStr);
    }

    /**
     * 使用分词查询
     *
     * @param index 索引名称
     * @param type 类型名称,可传入多个type逗号分隔
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param size 文档大小限制
     * @param fields 需要显示的字段,逗号分隔(缺省为全部字段)
     * @param sortField 排序字段
     * @param matchPhrase true 使用,短语精准匹配
     * @param highlightField 高亮字段
     * @param matchStr 过滤条件(xxx=111,aaa=222)
     * @return
     */
    public  List<Map<String, Object>> searchListData(String index, String type, long startTime, long endTime,
                                                           Integer size, String fields, String sortField,
                                                           boolean matchPhrase, String highlightField,
                                                           String matchStr) {

        SearchRequestBuilder searchRequestBuilder = transportClient.prepareSearch(index);
        if (StringUtils.isNotEmpty(type)) {
            searchRequestBuilder.setTypes(type.split(","));
        }
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();

        if (startTime > 0 && endTime > 0) {
            boolQuery.must(QueryBuilders.rangeQuery("timestamp").format("epoch_millis").from(startTime).to(endTime)
                    .includeLower(true).includeUpper(true));
        }

        //搜索的的字段
        if (StringUtils.isNotEmpty(matchStr)) {
            for (String s : matchStr.split(",")) {
                String[] ss = s.split("=");
                if (ss.length > 1) {
                    if (matchPhrase == Boolean.TRUE) {
                        boolQuery.must(QueryBuilders.matchPhraseQuery(s.split("=")[0], s.split("=")[1]));
                    } else {
                        boolQuery.must(QueryBuilders.matchQuery(s.split("=")[0], s.split("=")[1]));
                    }
                }

            }
        }

        // 高亮(xxx=111,aaa=222)
        if (StringUtils.isNotEmpty(highlightField)) {
            HighlightBuilder highlightBuilder = new HighlightBuilder();

            //highlightBuilder.preTags("<span style='color:red' >");//设置前缀
            //highlightBuilder.postTags("</span>");//设置后缀

            // 设置高亮字段
            highlightBuilder.field(highlightField);
            searchRequestBuilder.highlighter(highlightBuilder);
        }

        searchRequestBuilder.setQuery(boolQuery);

        if (StringUtils.isNotEmpty(fields)) {
            searchRequestBuilder.setFetchSource(fields.split(","), null);
        }
        searchRequestBuilder.setFetchSource(true);

        if (StringUtils.isNotEmpty(sortField)) {
            searchRequestBuilder.addSort(sortField, SortOrder.DESC);
        }

        if (size != null && size > 0) {
            searchRequestBuilder.setSize(size);
        }

        //打印的内容 可以在 Elasticsearch head 和 Kibana  上执行查询
        log.info("\n{}", searchRequestBuilder);

        SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();

        long totalHits = searchResponse.getHits().totalHits;
        long length = searchResponse.getHits().getHits().length;

        log.info("共查询到[{}]条数据,处理数据条数[{}]", totalHits, length);

        if (searchResponse.status().getStatus() == 200) {
            // 解析对象
            return setSearchResponse(searchResponse, Arrays.asList(highlightField));
        }

        return null;

    }

    public  PageUtils searchDataPage(String index, String type, int currentPage, int pageSize, long startTime,
                                           long endTime, String fields, String sortField, boolean matchPhrase,
                                           String highlightField, String matchStr) {
        return searchDataPage(index, type, currentPage, pageSize, startTime, endTime, fields, sortField, matchPhrase,
                highlightField, matchStr, false);
    }

    /**
     * 使用分词查询,并分页
     *
     * @param index 索引名称
     * @param type 类型名称,可传入多个type逗号分隔
     * @param currentPage 当前页
     * @param pageSize 每页显示条数
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param fields 需要显示的字段,逗号分隔(缺省为全部字段)
     * @param sortField 排序字段
     * @param matchPhrase true 使用,短语精准匹配
     * @param highlightField 高亮字段
     * @param matchStr 过滤条件(xxx=111,aaa=222)
     * @param relation 过滤条件的关系(true 关系与,false关系或)
     * @return
     */
    public  PageUtils searchDataPage(String index, String type, int currentPage, int pageSize, long startTime,
                                           long endTime, String fields, String sortField, boolean matchPhrase,
                                           String highlightField, String matchStr, boolean relation) {
        SearchRequestBuilder searchRequestBuilder = transportClient.prepareSearch(index);
        if (StringUtils.isNotEmpty(type)) {
            searchRequestBuilder.setTypes(type.split(","));
        }
        searchRequestBuilder.setSearchType(SearchType.QUERY_THEN_FETCH);

        // 需要显示的字段,逗号分隔(缺省为全部字段)
        if (StringUtils.isNotEmpty(fields)) {
            searchRequestBuilder.setFetchSource(fields.split(","), null);
        }

        //排序字段
        if (StringUtils.isNotEmpty(sortField)) {
            searchRequestBuilder.addSort(sortField, SortOrder.DESC);
        }

        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();

        if (startTime > 0 && endTime > 0) {
            boolQuery.must(QueryBuilders.rangeQuery("timestamp").format("epoch_millis").from(startTime).to(endTime)
                    .includeLower(true).includeUpper(true));
        }

        // 查询字段
        if (StringUtils.isNotEmpty(matchStr)) {
            for (String s : matchStr.split(",")) {
                String[] ss = s.split("=");
                if (matchPhrase == Boolean.TRUE) {
                    if (relation) {
                        boolQuery.must(
                                QueryBuilders.matchPhraseQuery(s.split("=")[0], QueryParser.escape(s.split("=")[1])));
                    } else {
                        boolQuery.should(
                                QueryBuilders.matchPhraseQuery(s.split("=")[0], QueryParser.escape(s.split("=")[1])));
                    }
                } else {
                    if (relation) {
                        boolQuery.must(QueryBuilders.matchQuery(s.split("=")[0], QueryParser.escape(s.split("=")[1])));
                    } else {
                        //                        boolQuery.should(QueryBuilders.matchQuery(s.split("=")[0], QueryParser.escape(s.split("=")[1])));
                        boolQuery.should(QueryBuilders.queryStringQuery(QueryParser.escape(s.split("=")[1])));
                        //                        boolQuery.should(QueryBuilders.matchQuery(s.split("=")[0], QueryParser.escape(s.split("=")[1])));

                    }
                }
            }
        }

        // 高亮(xxx=111,aaa=222)
        if (StringUtils.isNotEmpty(highlightField)) {
            HighlightBuilder highlightBuilder = new HighlightBuilder();

            //highlightBuilder.preTags("<span style='color:red' >");//设置前缀
            //highlightBuilder.postTags("</span>");//设置后缀

            // 设置高亮字段

            List<String> highlightFields = Arrays.asList(highlightField.split(","));

            for (String item : highlightFields) {
                HighlightBuilder.Field field = new HighlightBuilder.Field(item);
                field.fragmentSize(150);
                field.numOfFragments(1);
                field.order("score");
                highlightBuilder.field(field);
                highlightBuilder.requireFieldMatch(false);

            }
            searchRequestBuilder.highlighter(highlightBuilder);
        }

        searchRequestBuilder.setQuery(boolQuery);

        // 分页应用
        searchRequestBuilder.setFrom((currentPage - 1) * pageSize).setSize(pageSize);

        // 设置是否按查询匹配度排序
        // searchRequestBuilder.setExplain(true);

        //打印的内容 可以在 Elasticsearch head 和 Kibana  上执行查询
        log.info("\n{}", searchRequestBuilder);

        // 执行搜索,返回搜索响应信息
        SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();

        long totalHits = searchResponse.getHits().totalHits;
        long length = searchResponse.getHits().getHits().length;

        log.debug("共查询到[{}]条数据,处理数据条数[{}]", totalHits, length);

        if (searchResponse.status().getStatus() == 200) {
            // 解析对象
            List<Map<String, Object>> sourceList = setSearchResponse(searchResponse,
                    Arrays.asList(highlightField.split(",")));

            return  new PageUtils(sourceList, (int) totalHits,currentPage,pageSize);//PagedList.newMe(currentPage, pageSize, (int) totalHits, sourceList);
        }

        return null;

    }

    /**
     * 高亮结果集 特殊处理 cd
     * 
     * @param searchResponse
     * @param highlightFields
     */
    private  List<Map<String, Object>> setSearchResponse(SearchResponse searchResponse,
                                                               List<String> highlightFields) {
        List<Map<String, Object>> sourceList = new ArrayList<Map<String, Object>>();

        for (SearchHit searchHit : searchResponse.getHits().getHits()) {
            searchHit.getSourceAsMap().put("id", searchHit.getId());

            if (highlightFields != null && highlightFields.size() > 0) {
                for (String highlightField : highlightFields) {
                    System.out.println("遍历 高亮结果集,覆盖 正常结果集" + searchHit.getSourceAsString());
                    if (searchHit.getHighlightFields().get(highlightField) != null) {
                        Text[] text = searchHit.getHighlightFields().get(highlightField).getFragments();
                        StringBuffer stringBuffer = new StringBuffer();
                        if (text != null && text.length > 0) {
                            stringBuffer.append(text[0].string());
                            //遍历 高亮结果集,覆盖 正常结果集
                            searchHit.getSourceAsMap().put(highlightField, stringBuffer.toString());
                            stringBuffer.setLength(0);
                        }
                    }
                }
            }
            sourceList.add(searchHit.getSourceAsMap());
        }

        return sourceList;
    }

    public  List<Map<String, Object>> getSymbol(String index, String exchangeName) {

        MatchPhraseQueryBuilder mpq3 = QueryBuilders.matchPhraseQuery("exchange", exchangeName);
        QueryBuilder qb = QueryBuilders.boolQuery().must(mpq3); // .must(mpq2);

        SearchResponse searchResponse = transportClient.prepareSearch(index).setQuery(qb).setFrom(0).setSize(1000).execute()
                .actionGet();

        List<Map<String, Object>> maps = new ArrayList<>();
        SearchHit[] searchHits = searchResponse.getHits().getHits();
        for (SearchHit searchHit : searchHits) {
            Map<String, Object> map = searchHit.getSourceAsMap();
            maps.add(map);
        }

        return maps;
    }

    public  List<Map<String, Object>> getSymbolByCoin(String index, String exchangeName, String coin) {

        MatchPhraseQueryBuilder mpq2 = QueryBuilders.matchPhraseQuery("exchange", exchangeName);
        MatchPhraseQueryBuilder mpq3 = QueryBuilders.matchPhraseQuery("coin", coin);
        QueryBuilder qb = QueryBuilders.boolQuery().must(mpq2).must(mpq3);

        SearchResponse searchResponse = transportClient.prepareSearch(index).setQuery(qb).setFrom(0).setSize(1000).execute()
                .actionGet();

        List<Map<String, Object>> maps = new ArrayList<>();
        SearchHit[] searchHits = searchResponse.getHits().getHits();
        for (SearchHit searchHit : searchHits) {
            Map<String, Object> map = searchHit.getSourceAsMap();
            maps.add(map);
        }

        return maps;
    }

    public  Long getMaxTimestamp(String coin, String token, String exchangeName, String field) {

        MaxAggregationBuilder aggregation = AggregationBuilders.max("maxTimestamp").field(field);
        SearchRequestBuilder searchRequestBuilder = transportClient.prepareSearch("quotation-kline");
        searchRequestBuilder.addAggregation(aggregation);

        //        MatchPhraseQueryBuilder mpq1 = QueryBuilders.matchPhraseQuery("coin", coin);
        MatchPhraseQueryBuilder mpq2 = QueryBuilders.matchPhraseQuery("symbol", coin + token);
        MatchPhraseQueryBuilder mpq3 = QueryBuilders.matchPhraseQuery("exchange", exchangeName);
        QueryBuilder qb2 = QueryBuilders.boolQuery().must(mpq2).must(mpq3); // .must(mpq2);
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(qb2);

        SearchResponse searchResponse = searchRequestBuilder.setQuery(qb2).setFrom(0).setSize(1).execute().actionGet();

        InternalMax internalMax = searchResponse.getAggregations().get("maxTimestamp");

        System.out.println(internalMax.getValueAsString());

        if (!internalMax.getValueAsString().equals("null") && !internalMax.getValueAsString().equals("-Infinity")) {
            BigDecimal big = new BigDecimal(internalMax.getValue());
            return big.longValue();
        }

        return null;

    }

    public  Map<String, Object> getLastRecord(String coin, String token, String exchangeName, Long timestamp) {

        SearchRequestBuilder searchRequestBuilder = transportClient.prepareSearch("quotation-kline");

        //        MatchPhraseQueryBuilder mpq1 = QueryBuilders.matchPhraseQuery("coin", coin);
        MatchPhraseQueryBuilder mpq2 = QueryBuilders.matchPhraseQuery("symbol", coin + token);
        MatchPhraseQueryBuilder mpq3 = QueryBuilders.matchPhraseQuery("exchange", exchangeName);
        MatchPhraseQueryBuilder mpq4 = QueryBuilders.matchPhraseQuery("timestamp", timestamp);
        QueryBuilder qb2 = QueryBuilders.boolQuery().must(mpq2).must(mpq3).must(mpq4); // .must(mpq2);
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(qb2);

        SearchResponse searchResponse = searchRequestBuilder.setQuery(qb2).setFrom(0).setSize(1).execute().actionGet();
        SearchHit[] searchHits = searchResponse.getHits().getHits();

        if (searchHits.length > 0) {
            return searchResponse.getHits().getHits()[0].getSourceAsMap();
        } else {
            return null;
        }
    }

    public  BigDecimal getSumData(String coin, String token, String exchangeName, String field) {

        SumAggregationBuilder aggregation = AggregationBuilders.sum("sumData").field(field);
        SearchRequestBuilder searchRequestBuilder = transportClient.prepareSearch("quotation-trade");
        searchRequestBuilder.addAggregation(aggregation);

        MatchPhraseQueryBuilder mpq1 = QueryBuilders.matchPhraseQuery("coin", coin);
        MatchPhraseQueryBuilder mpq2 = QueryBuilders.matchPhraseQuery("symbol", coin + token);
        MatchPhraseQueryBuilder mpq3 = QueryBuilders.matchPhraseQuery("exchange", exchangeName);
        QueryBuilder qb2 = QueryBuilders.boolQuery().must(mpq1).must(mpq2).must(mpq3); // .must(mpq2);
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(qb2);

        SearchResponse searchResponse = searchRequestBuilder.setQuery(qb2).setFrom(0).setSize(1).execute().actionGet();

        InternalSum internalSum = searchResponse.getAggregations().get("sumData");
        BigDecimal big = null;

        if (internalSum.getValueAsString().equals("null")) {
            big = new BigDecimal(internalSum.getValue());
        }

        return big;

    }

    public  BigDecimal getExchangeSumData(String exchangeName, String field) {

        SumAggregationBuilder aggregation = AggregationBuilders.sum("sumData").field(field);
        SearchRequestBuilder searchRequestBuilder = transportClient.prepareSearch("quotation-trade");
        searchRequestBuilder.addAggregation(aggregation);

        MatchPhraseQueryBuilder mpq = QueryBuilders.matchPhraseQuery("exchange", exchangeName);
        QueryBuilder qb2 = QueryBuilders.boolQuery().must(mpq); // .must(mpq2);

        SearchResponse searchResponse = searchRequestBuilder.setQuery(qb2).setFrom(0).setSize(1).execute().actionGet();

        InternalSum internalSum = searchResponse.getAggregations().get("sumData");

        BigDecimal big = new BigDecimal(internalSum.getValue());
        big = big.setScale(4, BigDecimal.ROUND_HALF_UP);

        return big;

    }

    /**
     * 删除 用户和群的关系数据
     * @param tglGroupUserDto
     */
    public void deleteGroupUserEs(TglGroupUserDto tglGroupUserDto){
        DeleteByQueryAction.INSTANCE.newRequestBuilder(transportClient)
                .filter(QueryBuilders.termQuery("userId", tglGroupUserDto.getUserId()))
                .filter(QueryBuilders.termQuery("gid", tglGroupUserDto.getGid()))
                .source("groupuserrelation")
                .execute(new ActionListener<BulkByScrollResponse>() {
                    public void onResponse(BulkByScrollResponse response) {
                        long deleted = response.getDeleted();
                       log.info("[deleteGroupUserEs] delete:{},dto:{}",deleted,tglGroupUserDto);
                    }
                    public void onFailure(Exception e) {
                        log.error("[deleteGroupUserEs] delete is error , dto:{}",tglGroupUserDto);
                    }
                });
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值