/akkad-springboot/springboot-elasticsearch/es-footman/src/main/java/xyz/wongs/weathertop/base/service/BaseElasticService.java

https://github.com/king-angmar/weathertop · Java · 293 lines · 152 code · 17 blank · 124 comment · 4 complexity · 09bfdc1e21db599a24887c5f19eb2c59 MD5 · raw file

  1. package xyz.wongs.weathertop.base.service;
  2. import com.alibaba.fastjson.JSON;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
  5. import org.elasticsearch.action.bulk.BulkRequest;
  6. import org.elasticsearch.action.delete.DeleteRequest;
  7. import org.elasticsearch.action.index.IndexRequest;
  8. import org.elasticsearch.action.search.SearchRequest;
  9. import org.elasticsearch.action.search.SearchResponse;
  10. import org.elasticsearch.action.support.IndicesOptions;
  11. import org.elasticsearch.client.RequestOptions;
  12. import org.elasticsearch.client.RestHighLevelClient;
  13. import org.elasticsearch.client.indices.CreateIndexRequest;
  14. import org.elasticsearch.client.indices.CreateIndexResponse;
  15. import org.elasticsearch.client.indices.GetIndexRequest;
  16. import org.elasticsearch.common.settings.Settings;
  17. import org.elasticsearch.common.xcontent.XContentType;
  18. import org.elasticsearch.index.query.QueryBuilder;
  19. import org.elasticsearch.index.reindex.DeleteByQueryRequest;
  20. import org.elasticsearch.search.SearchHit;
  21. import org.elasticsearch.search.builder.SearchSourceBuilder;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.stereotype.Component;
  24. import xyz.wongs.weathertop.base.entiy.ElasticEntity;
  25. import java.util.ArrayList;
  26. import java.util.Collection;
  27. import java.util.List;
  28. @Slf4j
  29. @Component
  30. public class BaseElasticService {
  31. @Autowired
  32. RestHighLevelClient restHighLevelClient;
  33. /**
  34. * @author WCNGS@QQ.COM
  35. * @See
  36. * @date 2019/10/17 17:30
  37. * @param idxName 索引名称
  38. * @param idxSQL 索引描述
  39. * @return void
  40. * @throws
  41. * @since
  42. */
  43. public void createIndex(String idxName,String idxSQL){
  44. try {
  45. if (!this.indexExist(idxName)) {
  46. log.error(" idxName={} 已经存在,idxSql={}",idxName,idxSQL);
  47. return;
  48. }
  49. CreateIndexRequest request = new CreateIndexRequest(idxName);
  50. buildSetting(request);
  51. request.mapping(idxSQL, XContentType.JSON);
  52. // request.settings() 手工指定Setting
  53. CreateIndexResponse res = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
  54. if (!res.isAcknowledged()) {
  55. throw new RuntimeException("初始化失败");
  56. }
  57. } catch (Exception e) {
  58. e.printStackTrace();
  59. System.exit(0);
  60. }
  61. }
  62. /** 制定配置项的判断索引是否存在,注意与 isExistsIndex 区别
  63. * @author WCNGS@QQ.COM
  64. * @See
  65. * @date 2019/10/17 17:27
  66. * @param idxName index名
  67. * @return boolean
  68. * @throws
  69. * @since
  70. */
  71. public boolean indexExist(String idxName) throws Exception {
  72. GetIndexRequest request = new GetIndexRequest(idxName);
  73. //TRUE-返回本地信息检索状态,FALSE-还是从主节点检索状态
  74. request.local(false);
  75. //是否适应被人可读的格式返回
  76. request.humanReadable(true);
  77. //是否为每个索引返回所有默认设置
  78. request.includeDefaults(false);
  79. //控制如何解决不可用的索引以及如何扩展通配符表达式,忽略不可用索引的索引选项,仅将通配符扩展为开放索引,并且不允许从通配符表达式解析任何索引
  80. request.indicesOptions(IndicesOptions.lenientExpandOpen());
  81. return restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
  82. }
  83. /** 断某个index是否存在
  84. * @author WCNGS@QQ.COM
  85. * @See
  86. * @date 2019/10/17 17:27
  87. * @param idxName index名
  88. * @return boolean
  89. * @throws
  90. * @since
  91. */
  92. public boolean isExistsIndex(String idxName) throws Exception {
  93. return restHighLevelClient.indices().exists(new GetIndexRequest(idxName),RequestOptions.DEFAULT);
  94. }
  95. /** 设置分片
  96. * @author WCNGS@QQ.COM
  97. * @See
  98. * @date 2019/10/17 19:27
  99. * @param request
  100. * @return void
  101. * @throws
  102. * @since
  103. */
  104. public void buildSetting(CreateIndexRequest request){
  105. request.settings(Settings.builder().put("index.number_of_shards",3)
  106. .put("index.number_of_replicas",2));
  107. }
  108. /**
  109. * @author WCNGS@QQ.COM
  110. * @See
  111. * @date 2019/10/17 17:27
  112. * @param idxName index
  113. * @param entity 对象
  114. * @return void
  115. * @throws
  116. * @since
  117. */
  118. public void insertOrUpdateOne(String idxName, ElasticEntity entity) {
  119. IndexRequest request = new IndexRequest(idxName);
  120. log.error("Data : id={},entity={}",entity.getId(),JSON.toJSONString(entity.getData()));
  121. request.id(entity.getId());
  122. // request.source(entity.getData(), XContentType.JSON);
  123. request.source(JSON.toJSONString(entity.getData()), XContentType.JSON);
  124. try {
  125. restHighLevelClient.index(request, RequestOptions.DEFAULT);
  126. } catch (Exception e) {
  127. throw new RuntimeException(e);
  128. }
  129. }
  130. /**
  131. * @author WCNGS@QQ.COM
  132. * @See
  133. * @date 2019/10/17 17:27
  134. * @param idxName index
  135. * @param entity 对象
  136. * @return void
  137. * @throws
  138. * @since
  139. */
  140. public void deleteOne(String idxName, ElasticEntity entity) {
  141. DeleteRequest request = new DeleteRequest(idxName);
  142. request.id(entity.getId());
  143. try {
  144. restHighLevelClient.delete(request,RequestOptions.DEFAULT);
  145. } catch (Exception e) {
  146. throw new RuntimeException(e);
  147. }
  148. }
  149. /** 批量插入数据
  150. * @author WCNGS@QQ.COM
  151. * @See
  152. * @date 2019/10/17 17:26
  153. * @param idxName index
  154. * @param list 带插入列表
  155. * @return void
  156. * @throws
  157. * @since
  158. */
  159. public void insertBatch(String idxName, List<ElasticEntity> list) {
  160. BulkRequest request = new BulkRequest();
  161. list.forEach(item -> request.add(new IndexRequest(idxName).id(item.getId())
  162. .source(JSON.toJSONString(item.getData()), XContentType.JSON)));
  163. try {
  164. restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
  165. } catch (Exception e) {
  166. throw new RuntimeException(e);
  167. }
  168. }
  169. /** 批量插入数据
  170. * @author WCNGS@QQ.COM
  171. * @See
  172. * @date 2019/10/17 17:26
  173. * @param idxName index
  174. * @param list 带插入列表
  175. * @return void
  176. * @throws
  177. * @since
  178. */
  179. public void insertBatchTrueObj(String idxName, List<ElasticEntity> list) {
  180. BulkRequest request = new BulkRequest();
  181. list.forEach(item -> request.add(new IndexRequest(idxName).id(item.getId())
  182. .source(item.getData(), XContentType.JSON)));
  183. try {
  184. restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
  185. } catch (Exception e) {
  186. throw new RuntimeException(e);
  187. }
  188. }
  189. /** 批量删除
  190. * @author WCNGS@QQ.COM
  191. * @See
  192. * @date 2019/10/17 17:14
  193. * @param idxName index
  194. * @param idList 待删除列表
  195. * @return void
  196. * @throws
  197. * @since
  198. */
  199. public <T> void deleteBatch(String idxName, Collection<T> idList) {
  200. BulkRequest request = new BulkRequest();
  201. idList.forEach(item -> request.add(new DeleteRequest(idxName, item.toString())));
  202. try {
  203. restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
  204. } catch (Exception e) {
  205. throw new RuntimeException(e);
  206. }
  207. }
  208. /**
  209. * @author WCNGS@QQ.COM
  210. * @See
  211. * @date 2019/10/17 17:14
  212. * @param idxName index
  213. * @param builder 查询参数
  214. * @param c 结果类对象
  215. * @return java.util.List<T>
  216. * @throws
  217. * @since
  218. */
  219. public <T> List<T> search(String idxName, SearchSourceBuilder builder, Class<T> c) {
  220. SearchRequest request = new SearchRequest(idxName);
  221. request.source(builder);
  222. try {
  223. SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
  224. SearchHit[] hits = response.getHits().getHits();
  225. List<T> res = new ArrayList<>(hits.length);
  226. for (SearchHit hit : hits) {
  227. res.add(JSON.parseObject(hit.getSourceAsString(), c));
  228. }
  229. return res;
  230. } catch (Exception e) {
  231. throw new RuntimeException(e);
  232. }
  233. }
  234. /** 删除index
  235. * @author WCNGS@QQ.COM
  236. * @See
  237. * @date 2019/10/17 17:13
  238. * @param idxName
  239. * @return void
  240. * @throws
  241. * @since
  242. */
  243. public void deleteIndex(String idxName) {
  244. try {
  245. if (!this.indexExist(idxName)) {
  246. log.error(" idxName={} 已经存在",idxName);
  247. return;
  248. }
  249. restHighLevelClient.indices().delete(new DeleteIndexRequest(idxName), RequestOptions.DEFAULT);
  250. } catch (Exception e) {
  251. throw new RuntimeException(e);
  252. }
  253. }
  254. /**
  255. * @author WCNGS@QQ.COM
  256. * @See
  257. * @date 2019/10/17 17:13
  258. * @param idxName
  259. * @param builder
  260. * @return void
  261. * @throws
  262. * @since
  263. */
  264. public void deleteByQuery(String idxName, QueryBuilder builder) {
  265. DeleteByQueryRequest request = new DeleteByQueryRequest(idxName);
  266. request.setQuery(builder);
  267. //设置批量操作数量,最大为10000
  268. request.setBatchSize(10000);
  269. request.setConflicts("proceed");
  270. try {
  271. restHighLevelClient.deleteByQuery(request, RequestOptions.DEFAULT);
  272. } catch (Exception e) {
  273. throw new RuntimeException(e);
  274. }
  275. }
  276. }