最近在项目中有看到一种比较实用的ElasticSearch工具类封装方式,特此记录便于日后查阅。

        1、controller层

@RequestMapping(value = "/uri/page", method = RequestMethod.GET)
public DataResult page(@RequestParam(name = "pageIndex") Integer pageIndex,@RequestParam(name = "pageSize") Integer pageSize,@RequestParam(name = "uri") String uri,@RequestParam(name = "api") String api,@RequestParam(name = "method") String method) {try {List<Filter> filters = new ArrayList();if(StringUtils.isNotBlank(uri)){filters.add(new Filter("uri", FilterEnum.INCLUDE_NO_SPLIT.getType(), uri));}if(StringUtils.isNotBlank(api)){filters.add(new Filter("api", FilterEnum.EQUAL.getType(), api.toLowerCase()));}if(StringUtils.isNotBlank(method)){filters.add(new Filter("method", FilterEnum.EQUAL.getType(), method.toLowerCase()));}return new DataResult(SystemStatusCode.NORMAL.getValue(), "ok", topStatisticsService.findTopUriByCondition(filters, pageIndex, pageSize));} catch (Exception e) {LOGGER.error("获取uri top分页数据失败", e);return new DataResult(SystemStatusCode.ERROR.getValue(), "fail", e.getMessage());}
}

       2、FilterEnum

package com.fenqile.sgp.api.common;/*** @author sherrycao* @version 2019/3/12*/
public enum FilterEnum {INCLUDE(1, "包含"),EXCLUDE(2, "不包含"),EQUAL(3, "等于"),UNEQUAL(4, "不等于"),INCLUDE_NO_SPLIT(1, "包含,但不分词"),EQUAL_NO_SPLIT(5, "等于,但不分词");private int type;private String desc;FilterEnum(int type, String desc) {this.type = type;this.desc = desc;}public int getType() {return type;}public void setType(int type) {this.type = type;}public String getDesc() {return desc;}public void setDesc(String desc) {this.desc = desc;}
}

        3、Service层

public PageInfo<TreeComplexVo> findTopUriByCondition(List<Filter> filters, int pageIndex, int pageSize) throws Exception {PageInfo<TreeComplexVo> pageInfo = new PageInfo<>();// 获取所有indexList<String> indices = Arrays.asList(EsConstants.USER_TOP_URI_INDEX_PREFIX);for (String index : indices) {elasticSearchDao.createIndex(EsClient.getClient(), index);}SearchSourceBuilder searchSourceBuilder = structSearchSourceBuilder(filters, pageIndex, pageSize);searchSourceBuilder.sort("complexScore", SortOrder.DESC);List<TreeComplexVo> treeComplexVo = new ArrayList();String tableName = topProviderService.getEsTableName(EsConstants.TOP_URI, topProviderService.getEsTableIndex(EsConstants.TOP_URI));SearchResult searchResult = elasticSearchDao.search(EsClient.getClient(), indices, tableName, searchSourceBuilder.toString());List<SearchResult.Hit<JSONObject, Void>> hits = searchResult.getHits(JSONObject.class);for (SearchResult.Hit<JSONObject, Void> hit : hits) {treeComplexVo.add(JSON.parseObject(hit.source.toString(), TreeComplexVo.class));}Long total = searchResult.getTotal();pageInfo.setTotal(total);pageInfo.setPageNum(pageIndex);pageInfo.setPageSize(pageSize);pageInfo.setList(treeComplexVo);Long num = total % pageSize;int page;if (num == 0) {page = (int) (total / pageSize);} else {page = (int) (total / pageSize) + 1;}pageInfo.setPages(page);// 生成queryreturn pageInfo;
}@Override
public Boolean loadServiceCount( int pageSize, Map<String, Integer> appMethodCountMap) {//计算新的表索引,这样轮流覆盖,暂时做0,1轮训Integer newIndex = getEsTableIndex(EsConstants.TOP_SERVICE)+1;String tableName = getEsTableName(EsConstants.TOP_SERVICE, newIndex);try{//清理旧数据elasticSearchDao.deleteDocAll(EsClient.getClient(), EsConstants.USER_TOP_PROVIDER_INDEX_PREFIX, tableName);}catch (Exception e){LOGGER.error("清理es数据失败", e);}Map<String, ServiceCountVo> maps = new HashMap();for(EnvironmentType environmentType: EnvironmentType.values()){try{//切换当前的zkServiceService serviceService = (ServiceService)GovernanceConfig.getEnvServiceMap().get(environmentType.getType()).get(SERVICE_PROVIDER_NAME);int pageIndex = 0;int resultSize = 0;do {//查询分页数据pageIndex +=1;PageInfo pageInfo = serviceService.getServiceCount(pageIndex, pageSize);resultSize = appendSingleZkServiceCount(maps, environmentType, pageInfo, appMethodCountMap);//分页数据刚好是指定分页大小就继续查询}while(resultSize==pageSize);}catch(Exception e){LOGGER.error("分页获取zk数据失败", e);}}/*** 分页插入数据到es*/List results = new ArrayList(maps.values());int batchSize = results.size()%ES_STORE_BATCH_SIZE==0?results.size()/ES_STORE_BATCH_SIZE:1+results.size()/ES_STORE_BATCH_SIZE;for(int i=1;i<=batchSize;i++){int start = Math.max(0, i-1)*ES_STORE_BATCH_SIZE;int end = Math.min(results.size(), start+ES_STORE_BATCH_SIZE);elasticSearchDao.insertDocuments(EsClient.getClient(), results.subList(start, end), EsConstants.USER_TOP_PROVIDER_INDEX_PREFIX, tableName);}//成功后更新表索引,方便查询使用setEsTableIndex(EsConstants.TOP_SERVICE, newIndex);return true;
}

        4、EsClient层

package com.fenqile.sgp.web.config;import com.google.gson.GsonBuilder;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import com.fenqile.sgp.business.helper.HippoHelper;/*** @author sherrycao* @version 2019/3/6*/
public class EsClient {private static JestClient client;private static JestClient accessLogClient;private static JestClient businessLogClient;private EsClient() {}private static void build() {JestClientFactory factory = new JestClientFactory();factory.setHttpClientConfig(new HttpClientConfig.Builder(HippoHelper.getEsUrls()).multiThreaded(true)//一个route 默认不超过2个连接  路由是指连接到某个远程注解的个数。总连接数=route个数 * defaultMaxTotalConnectionPerRoute.defaultMaxTotalConnectionPerRoute(2)//所有route连接总数.maxTotalConnection(2).connTimeout(10000).readTimeout(10000).gson(new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create()).build());client = factory.getObject();}private static void buildAccessLogClient() {JestClientFactory factory = new JestClientFactory();factory.setHttpClientConfig(new HttpClientConfig.Builder(HippoHelper.getEsAccessLogUrls()).defaultCredentials("elastic","6018C23DD614E02D").multiThreaded(true)//一个route 默认不超过2个连接  路由是指连接到某个远程注解的个数。总连接数=route个数 * defaultMaxTotalConnectionPerRoute.defaultMaxTotalConnectionPerRoute(2)//所有route连接总数.maxTotalConnection(2).connTimeout(20000).readTimeout(20000).gson(new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create()).build());accessLogClient = factory.getObject();}private static void buildBusinessLogClient() {JestClientFactory factory = new JestClientFactory();factory.setHttpClientConfig(new HttpClientConfig.Builder(HippoHelper.getEsBusinessLogUrls()).defaultCredentials("elastic","6018C23DD614E02D").multiThreaded(true)//一个route 默认不超过2个连接  路由是指连接到某个远程注解的个数。总连接数=route个数 * defaultMaxTotalConnectionPerRoute.defaultMaxTotalConnectionPerRoute(2)//所有route连接总数.maxTotalConnection(2).connTimeout(20000).readTimeout(20000).gson(new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create()).build());businessLogClient = factory.getObject();}public static synchronized JestClient getClient() {if (client == null) {build();}return client;}public static synchronized JestClient getAccessLogClient() {if (accessLogClient == null) {buildAccessLogClient();}return accessLogClient;}public static synchronized JestClient getBusinessLogClient() {if (businessLogClient == null) {buildBusinessLogClient();}return businessLogClient;}
}

        5、ElasticSearchDao层

package com.fenqile.sgp.business.dao;import io.searchbox.client.JestClient;
import io.searchbox.client.JestResult;
import io.searchbox.core.*;
import io.searchbox.indices.CreateIndex;
import io.searchbox.indices.DeleteIndex;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;/*** @author sherrycao* @version 2019/3/11*/
@Service
public class ElasticSearchDao {private static final Logger LOGGER = LoggerFactory.getLogger(ElasticSearchDao.class);/*** 插入文档* @param document* @param indexName* @param typeName*/public void insertDocument(JestClient client, Object document, String indexName, String typeName) {try {Bulk.Builder bulk = new Bulk.Builder().defaultIndex(indexName).defaultType(typeName);Index index = new Index.Builder(document).build();bulk.addAction(index);BulkResult bulkResult = client.execute(bulk.build());bulkResult.isSucceeded();} catch (IOException e) {LOGGER.error("写入es异常, indexName {}, typeName {}", indexName, typeName);LOGGER.error("", e);}}/*** 批量插入文档* @param documents* @param indexName* @param typeName*/public void insertDocuments(JestClient client, List<Object> documents, String indexName, String typeName) {Bulk.Builder bulk = new Bulk.Builder().defaultIndex(indexName).defaultType(typeName);for (Object document : documents) {Index index = new Index.Builder(document).build();bulk.addAction(index);}try {BulkResult bulkResult = client.execute(bulk.build());bulkResult.isSucceeded();} catch (IOException e) {LOGGER.error("批量写入es异常, indexName {}, typeName {}", indexName, typeName);LOGGER.error("", e);}}/*** 指定id* @param client* @param documentMap* @param indexName* @param typeName*/public void insertDocuments(JestClient client, Map<String, Object> documentMap, String indexName, String typeName) {Bulk.Builder bulk = new Bulk.Builder().defaultIndex(indexName).defaultType(typeName);Iterator documentEntry = documentMap.entrySet().iterator();while(documentEntry.hasNext()){Map.Entry<String, Object> entry = (Map.Entry)documentEntry.next();Index index = new Index.Builder(entry.getValue()).id(entry.getKey()).build();bulk.addAction(index);}try {BulkResult bulkResult = client.execute(bulk.build());bulkResult.isSucceeded();} catch (IOException e) {LOGGER.error("批量写入es异常, indexName {}, typeName {}", indexName, typeName);LOGGER.error("", e);}}public SearchResult search(JestClient client, String indexName, String typeName, String query) throws Exception {Search search = new Search.Builder(query).addIndex(indexName).addType(typeName).build();return client.execute(search);}/*** 使用type查询* @param client* @param indexName* @param typeName* @param query* @return* @throws Exception*/public SearchResult search(JestClient client, List<String> indexName, String typeName, String query) throws Exception {LOGGER.info(query);Search search = new Search.Builder(query).addIndices(indexName).addType(typeName).build();LOGGER.info(search.toString());LOGGER.info(search.getPathToResult());return client.execute(search);}/*** 不使用type查询* @param client* @param indexName* @param query* @return* @throws Exception*/public SearchResult search(JestClient client, List<String> indexName, String query) throws Exception {LOGGER.info(query);Search search = new Search.Builder(query).addIndices(indexName).build();LOGGER.info(search.toString());LOGGER.info(search.getPathToResult());return client.execute(search);}public Boolean createIndex(JestClient client, String indexName) {try {JestResult jr = client.execute(new CreateIndex.Builder(indexName).build());return jr.isSucceeded();} catch (IOException e) {LOGGER.error("", e);}return false;}public boolean deleteDoc(JestClient client,String indexId, String indexName, String indexType) {Delete.Builder builder = new Delete.Builder(indexId);builder.refresh(true);Delete delete = builder.index(indexName).type(indexType).build();try {JestResult result = client.execute(delete);if (result != null && !result.isSucceeded()) {throw new RuntimeException(result.getErrorMessage()+"删除文档失败!");}} catch (Exception e) {LOGGER.error("",e);return false;}return true;}public boolean deleteDocAll(JestClient client,String indexName, String indexType) {SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();searchSourceBuilder.query(boolQueryBuilder);DeleteByQuery.Builder builder = new DeleteByQuery.Builder(searchSourceBuilder.toString());builder.refresh(true);DeleteByQuery deleteByQuery = builder.addIndex(indexName).addType(indexType).build();try {JestResult result = client.execute(deleteByQuery);if (result != null && !result.isSucceeded()) {throw new RuntimeException(result.getErrorMessage()+"删除文档失败!");}} catch (Exception e) {LOGGER.error("",e);return false;}return true;}/*** 删除类型* @param indexName* @param indexType*/public boolean deleteType(JestClient client, String indexName, String indexType) {DeleteIndex deleteIndex = new DeleteIndex.Builder(indexName).type(indexType).build();try {JestResult result = client.execute(deleteIndex);if (result != null && result.isSucceeded()) {throw new RuntimeException(result.getErrorMessage()+"删除类型失败!");}} catch (Exception e) {LOGGER.error("",e);return false;}return true;}/*** 删除索引* @param indexName*/public boolean deleteIndex(JestClient client, String indexName) {DeleteIndex deleteIndex = new DeleteIndex.Builder(indexName).build();try {JestResult result = client.execute(deleteIndex);if (result != null && result.isSucceeded()) {throw new RuntimeException(result.getErrorMessage()+"删除索引失败!");}} catch (Exception e) {LOGGER.error("",e);return false;}return true;}/*** 插入或更新文档* @param id* @param indexObject* @param indexName* @param indexType* @return*/public boolean insertOrUpdateDoc(JestClient client,String id, Object indexObject, String indexName, String indexType) {Index.Builder builder = new Index.Builder(indexObject);builder.id(id);builder.refresh(true);Index index = builder.index(indexName).type(indexType).build();try {JestResult result = client.execute(index);if (result != null && !result.isSucceeded()) {throw new RuntimeException(result.getErrorMessage()+"插入更新索引失败!");}} catch (Exception e) {LOGGER.error("",e);return false;}return true;}
}

到此 ElasticSearch工具类封装介绍完成。

ElasticSearch工具类封装相关推荐

  1. Redis工具类封装讲解和实战

    Redis工具类封装讲解和实战     简介:高效开发方式 Redis工具类封装讲解和实战         1.常用客户端 https://redisdesktop.com/download      ...

  2. IOS开发基础之音频工具类封装AVAudioPlayer

    IOS开发基础之音频工具类封装AVAudioPlayer 源码在我的主页下面 ,项目名称是AVAudioPlayer 关键性代码 工具类的封装 // // LJAudioTool.h // AVAud ...

  3. 【JavaScript学习】JavaScript 常用工具类封装

    文章目录 1.JavaScript 常用工具类封装 (1)获得浏览器地址所有参数 (2)将json转为get参数 (3)格式校验工具类 (4)数组操作工具类 (5)表单取值工具类 (6)时间转换工具类 ...

  4. Android 图片处理工具类封装2

    http://www.2cto.com/kf/201312/263638.html Android 图片处理工具类封装 2013-12-10     0个评论   来源:Wiker Yong 的专栏  ...

  5. Redis工具类封装

    Redis工具类封装 使用redis也好几年了,总是拷贝来拷贝去的,这次干脆放在这把,每次来这拷贝,不用在工程里面找来找去了. /*** Redis工具类* @author Huangliniao* ...

  6. XmlMapper详解及工具类封装

    一.XmlMapper说明 1.依赖包引入 <dependency><groupId>com.fasterxml.jackson.dataformat</groupId& ...

  7. mac json工具_工具类封装的思路 | 钉钉群机器人为例

    大家好,我是小刀 大家好, 我是小刀,算起来好像有半个月没写文章了,是不是都快忘了我呀 这半个月虽然文章没写,但是鼓捣了不少工具类,搞着搞着发现,这些工具类虽然功能不一样,但是封装的思路和组织的方式很 ...

  8. Python Excel工具类封装, 实现excel表头加颜色

    封装Excel工具类 我们常用的excel工具类,读有xlrd,写有xlwt.有读有写,新一代库有pandas,openpyxl等等. 大家用法都差不多,今天博主就介绍新手最爱,我也爱的xlrd和xl ...

  9. java递归生成树结构_突破CRUD | 万能树Java工具类封装(源码)

    0.学完本文你或许可以收获 感受一个树工具从初始逐步优化完善的过程 树工具封装的设计思考与实现思路 最后收获一款拿来即用的树工具源代码 对于前端树组件有一定了解和使用过的同学可直接跳跃到第3章节开始. ...

最新文章

  1. 使用Hash直接登录Windows
  2. MySQL--安装及配置
  3. Codeforces Beta Round #16 (Div. 2 Only)【未完结】
  4. 基本HTTP协议流程是什么?
  5. Bootstrap学习笔记(三) 网格系统
  6. 优化Java序列化– Java,XML,JSON,Kryo,POF
  7. iOS开发之复制字符串到剪贴板
  8. java中文乱码decode_Java WEB开发中的中文乱码问题解决
  9. nyoj--20-吝啬的国度
  10. timimg学习数据删了_如何评价Timing这个督促人学习的软件?
  11. 处理器架构 (十三) ARMv6架构下 各微架构的不同
  12. 视频教程-汇编语言程序设计IV-其他
  13. 【python】语句
  14. 10.24程序员节专辑——程序员最爱的数字,1024的秘密
  15. 家庭监控,网络摄像头(OpenWRT平台下Mjpg-Streamer+Ngrok实现方案)
  16. 给自己一个618消费的理由 飞利浦B8905回音壁有料分享
  17. 【贪玩巴斯】一文通过操作实例——学会 知网专业检索 2022年3月21日
  18. Unity用户手册2019.3(中文版)1.3.1 常见资源类型
  19. 算法笔记随笔:分数的化简,四则运算和输出
  20. jQuery里面的选择属性和修改属性

热门文章

  1. MySQL InnoDB限制
  2. 孙溟㠭讲篆刻(二):金石之上的四两拨千斤
  3. 【解释型语言】JavaScriptES6
  4. 同步电路与跨时钟域电路设计1——单bit信号的跨时钟域传输(同步器)
  5. 以太坊是什么——以太坊创始人 Vitalik Buterin 的亲自解答
  6. 赵本山风光嫁女 林肯悍马排队场面奢华
  7. 中级计算机维修工证书,计算机维修工(中级)
  8. 在量化交易方面,美国究竟比中国领先多久?
  9. 上门美发APP开发,上门美发平台与实体美发店的区别
  10. 如何培养高情商的孩子读后感