当前位置:   article > 正文

SpringBoot基于ElasticSearch7.9.2和ElasticsearchRestTemplate的一些通用接口(可复用)_elasticsearchresttemplate.bulkupdate

elasticsearchresttemplate.bulkupdate

之前写过一篇SpringBoot集成ElasticSearch 7.9.2 教程和简单增删改查案例icon-default.png?t=M276https://blog.csdn.net/weixin_43753812/article/details/117733551

这里提供一个通用的操作接口和文档的方法类,大多数的使用场景都已经覆盖,话不多说,开整:

  1. 首先,创建一个自定义注解,结合elasticsearch提供的注解一起,配合实体类操作文档和索引。
  1. package com.cqvip.innocence.common.annotation;
  2. import java.lang.annotation.*;
  3. /**
  4. * @ClassName DocumentId
  5. * @Description es文档数据的id标识
  6. * @Author Innocence
  7. * @Date 2021/6/16 11:34
  8. * @Version 1.0
  9. */
  10. @Target(ElementType.FIELD)
  11. @Retention(RetentionPolicy.RUNTIME)
  12. @Documented
  13. @Inherited
  14. public @interface DocumentId {
  15. }

2.写一个与文档对应的DTO(这里只是示例,具体的实体根据对应需求建立)

  1. package com.cqvip.innocence.project.model.entity;
  2. import com.cqvip.innocence.common.annotation.DocumentId;
  3. import lombok.Data;
  4. import org.springframework.data.elasticsearch.annotations.Document;
  5. import org.springframework.data.elasticsearch.annotations.Field;
  6. import org.springframework.data.elasticsearch.annotations.FieldType;
  7. import java.io.Serializable;
  8. /**
  9. * @ClassName EsTestInfo
  10. * @Description es测试数据
  11. * @Author Innocence
  12. * @Date 2021/6/16 13:57
  13. * @Version 1.0
  14. */
  15. @Data
  16. @Document(indexName = "test_info")
  17. public class EsTestInfo implements Serializable {
  18. private static final long serialVersionUID = 6735913456541605819L;
  19. @DocumentId
  20. @Field(type = FieldType.Keyword)
  21. private String infoId;
  22. private String name;
  23. @Field(type = FieldType.Integer)
  24. private Integer age;
  25. @Field(type = FieldType.Keyword)
  26. private String sex;
  27. @Field(type = FieldType.Keyword)
  28. private String[] roles;
  29. }

 3.索引操作的接口和实现

  1. package com.cqvip.innocence.project.esservice;
  2. /**
  3. * @ClassName IndexService
  4. * @Description 索引的增删改查
  5. * @Author Innocence
  6. * @Date 2021/3/1 14:06
  7. * @Version 1.0
  8. */
  9. public interface IndexService<T>{
  10. /**
  11. * 根据类创建索引
  12. * @author Innocence
  13. * @date 2021/6/16
  14. * @param clazz
  15. * @return java.lang.Boolean
  16. */
  17. Boolean createIndexByClass(Class<T> clazz);
  18. /**
  19. * 根据名称创建索引
  20. * 不建议使用,因为很多设置需要自己写配置文件
  21. * @author Innocence
  22. * @date 2021/3/1
  23. * @param indexName
  24. * @return java.lang.Boolean
  25. */
  26. Boolean createIndexByName(String indexName);
  27. /**
  28. * 根据索引名判断索引是否存在
  29. * @author Innocence
  30. * @date 2021/6/16
  31. * @param indexName 索引名
  32. * @return java.lang.Boolean
  33. */
  34. Boolean isIndexExist(String indexName);
  35. /**
  36. * 根据索引名删除索引
  37. * @author Innocence
  38. * @date 2021/6/16
  39. * @param indexName 索引名
  40. * @return java.lang.Boolean
  41. */
  42. Boolean deleteIndexByName(String indexName);
  43. /**
  44. * 获取被标记的索引名
  45. * @author Innocence
  46. * @date 2021/6/16
  47. * @param clazz
  48. * @return java.lang.String
  49. */
  50. String getIndexName(Class<T> clazz);
  51. }
  1. package com.cqvip.innocence.project.esservice.impl;
  2. import cn.hutool.core.util.StrUtil;
  3. import com.cqvip.innocence.project.esservice.IndexService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.data.elasticsearch.annotations.Document;
  6. import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
  7. import org.springframework.data.elasticsearch.core.IndexOperations;
  8. import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
  9. import org.springframework.stereotype.Service;
  10. import java.lang.annotation.Annotation;
  11. /**
  12. * @ClassName IndexServiceImpl
  13. * @Description 抽象
  14. * @Author Innocence
  15. * @Date 2021/3/1 14:24
  16. * @Version 1.0
  17. */
  18. @Service
  19. public class IndexServiceImpl<T> implements IndexService<T> {
  20. @Autowired
  21. private ElasticsearchRestTemplate restTemplate;
  22. @Override
  23. public String getIndexName(Class<T> clazz){
  24. Annotation documentAnnotation = clazz.getDeclaredAnnotation(Document.class);
  25. if(documentAnnotation==null){
  26. return null;
  27. }
  28. String indexName = ((Document) documentAnnotation).indexName();
  29. if (StrUtil.isNotBlank(indexName)){
  30. return indexName;
  31. }
  32. return null;
  33. }
  34. @Override
  35. public Boolean createIndexByClass(Class<T> clazz) {
  36. Boolean indexExist = isIndexExist(getIndexName(clazz));
  37. if (indexExist){
  38. return false;
  39. }
  40. IndexOperations indexOps = restTemplate.indexOps(clazz);
  41. boolean result1 = indexOps.create();
  42. boolean result2 = indexOps.putMapping(indexOps.createMapping(clazz));
  43. return result1&result2;
  44. }
  45. @Override
  46. public Boolean createIndexByName(String indexName) {
  47. Boolean indexExist = isIndexExist(indexName);
  48. if (indexExist){
  49. return false;
  50. }
  51. IndexOperations indexOps = restTemplate.indexOps(IndexCoordinates.of(indexName));
  52. return indexOps.create();
  53. }
  54. @Override
  55. public Boolean isIndexExist(String indexName) {
  56. IndexOperations indexOps = restTemplate.indexOps(IndexCoordinates.of(indexName));
  57. return indexOps.exists();
  58. }
  59. @Override
  60. public Boolean deleteIndexByName(String indexName) {
  61. IndexOperations indexOps = restTemplate.indexOps(IndexCoordinates.of(indexName));
  62. return indexOps.delete();
  63. }
  64. }

4.文档操作的接口和实现类

  1. package com.cqvip.innocence.project.esservice;
  2. import org.elasticsearch.search.aggregations.bucket.terms.Terms;
  3. import org.elasticsearch.search.builder.SearchSourceBuilder;
  4. import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
  5. import org.springframework.data.domain.PageRequest;
  6. import org.springframework.data.elasticsearch.core.query.Query;
  7. import java.io.IOException;
  8. import java.util.List;
  9. import java.util.Map;
  10. /**
  11. * @ClassName DocumentService
  12. * @Description 适用于本项目的数据的增删改查
  13. * @Author Innocence
  14. * @Date 2021/3/1 15:43
  15. * @Version 1.0
  16. */
  17. public interface DocumentService<T> {
  18. /**
  19. * 设置高亮字段
  20. * @author Innocence
  21. * @date 2021/3/2
  22. * @param fields 需要设置的高亮字段
  23. * @return org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder
  24. */
  25. HighlightBuilder getHighlightBuilder(String[] fields);
  26. /**
  27. * 根据id判断文档是否存在于指定索引中
  28. * @author Innocence
  29. * @date 2021/3/1
  30. * @param id
  31. * @param clazz
  32. * @return java.lang.Boolean
  33. */
  34. Boolean isExist(String id, Class<T> clazz);
  35. /**
  36. * 单条数据插入
  37. * @author Innocence
  38. * @date 2021/3/1
  39. * @param t 需插入的数据
  40. * @return java.lang.String 返回文档id
  41. */
  42. String saveByEntity(T t);
  43. /**
  44. * 批量插入
  45. * @author Innocence
  46. * @date 2021/3/1
  47. * @param entities 待插入的数据实体集合
  48. * @return java.util.List<java.lang.String> 返回idList
  49. */
  50. List<String> saveBatchByEntities(List<T> entities) throws Exception;
  51. /**
  52. * 单条数据更新
  53. * @author Innocence
  54. * @date 2021/3/1
  55. * @param t 需要更新的实体数据
  56. * @return void
  57. */
  58. void updateByEntity(T t);
  59. /**
  60. * 批量更新
  61. * @author Innocence
  62. * @date 2021/3/1
  63. * @param entities 待更新的数据实体集合
  64. * @return void
  65. */
  66. void updateByEntities(List<T> entities);
  67. /**
  68. * 根据id删除数据
  69. * @author Innocence
  70. * @date 2021/3/1
  71. * @param id
  72. * @param clazz
  73. * @return java.lang.String 被删除的id
  74. */
  75. String deleteById(String id, Class<T> clazz);
  76. /**
  77. * 根据id批量删除数据
  78. * @author Innocence
  79. * @date 2021/3/12
  80. * @param ids
  81. * @param clazz
  82. * @return void
  83. */
  84. void deleteByIds(List<String> ids, Class<T> clazz);
  85. /**
  86. * 根据条件删除数据
  87. * @author Innocence
  88. * @date 2021/3/1
  89. * @param query 条件构造器
  90. * @param clazz
  91. * @return void
  92. */
  93. void deleteByQuery(Query query, Class<T> clazz);
  94. /**
  95. * 根据id查询数据 (基于注解形式设置了索引mapping)
  96. * @author Innocence
  97. * @date 2021/3/1
  98. * @param clazz
  99. * @return T
  100. */
  101. T getEntityById(String id,Class<T> clazz);
  102. /**
  103. * 根据id批量查询数据 (基于注解形式设置了索引mapping)
  104. * @author Innocence
  105. * @date 2021/8/24
  106. * @return java.util.List<T>
  107. */
  108. List<T> getEntityByIds(List<String> ids, Class<T> clazz);
  109. /**
  110. * 查询符合条件的总条数
  111. * @author Innocence
  112. * @date 2021/3/2
  113. * @return java.lang.Long
  114. */
  115. Long getCount(Query query,Class<T> clazz);
  116. /**
  117. * 查询符合条件的实体list
  118. * @author Innocence
  119. * @date 2021/3/2
  120. * @param query 构建的查询条件 主要使用NativeSearchQuery 来进行构造
  121. * @param isHighLight 是否映射高亮(true映射高亮,否则不映射)
  122. * @return java.util.List<T>
  123. */
  124. List<T> getInfoList(Query query, Class<T> clazz,Boolean isHighLight);
  125. /**
  126. * 查询符合条件的分页
  127. * @author Innocence
  128. * @date 2021/3/2
  129. * @param query 构建的查询条件 主要使用NativeSearchQuery 来进行构造
  130. * @return org.springframework.data.domain.Page<T>
  131. */
  132. Map<String,Object> getPageList(Query query, PageRequest pageRequest,Class<T> clazz);
  133. /**
  134. * 根据条件获取聚类信息
  135. * springboot-data 封装的聚类查询速度很慢,这里直接用client操作
  136. * @author Innocence
  137. * @date 2021/3/19
  138. * @param query, clazz
  139. * @return java.util.Map<java.lang.String,java.util.List<? extends org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket>>
  140. */
  141. Map<String, List<? extends Terms.Bucket>> getFacetByQuery(SearchSourceBuilder query, Class<T> clazz) throws IOException;
  142. }

 

  1. package com.cqvip.innocence.project.esservice.impl;
  2. import cn.hutool.core.util.StrUtil;
  3. import com.alibaba.fastjson.JSON;
  4. import com.cqvip.innocence.common.annotation.DocumentId;
  5. import com.cqvip.innocence.common.exception.ElasticServiceException;
  6. import com.cqvip.innocence.project.esservice.DocumentService;
  7. import com.cqvip.innocence.project.esservice.IndexService;
  8. import org.apache.commons.beanutils.PropertyUtils;
  9. import org.apache.lucene.search.IndexSearcher;
  10. import org.elasticsearch.action.search.SearchRequest;
  11. import org.elasticsearch.action.search.SearchResponse;
  12. import org.elasticsearch.client.RequestOptions;
  13. import org.elasticsearch.client.RestHighLevelClient;
  14. import org.elasticsearch.index.query.QueryBuilders;
  15. import org.elasticsearch.search.aggregations.Aggregation;
  16. import org.elasticsearch.search.aggregations.Aggregations;
  17. import org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms;
  18. import org.elasticsearch.search.aggregations.bucket.terms.Terms;
  19. import org.elasticsearch.search.builder.SearchSourceBuilder;
  20. import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
  21. import org.springframework.beans.factory.annotation.Autowired;
  22. import org.springframework.beans.factory.annotation.Qualifier;
  23. import org.springframework.data.domain.Page;
  24. import org.springframework.data.domain.PageImpl;
  25. import org.springframework.data.domain.PageRequest;
  26. import org.springframework.data.elasticsearch.core.*;
  27. import org.springframework.data.elasticsearch.core.document.Document;
  28. import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
  29. import org.springframework.data.elasticsearch.core.query.*;
  30. import org.springframework.stereotype.Service;
  31. import java.io.IOException;
  32. import java.lang.reflect.*;
  33. import java.lang.reflect.Field;
  34. import java.util.ArrayList;
  35. import java.util.HashMap;
  36. import java.util.List;
  37. import java.util.Map;
  38. /**
  39. * @ClassName DocumentServiceImpl
  40. * @Description TODO
  41. * @Author Innocence
  42. * @Date 2021/3/1 16:27
  43. * @Version 1.0
  44. */
  45. @Service
  46. public class DocumentServiceImpl<T> implements DocumentService<T> {
  47. @Autowired
  48. private ElasticsearchRestTemplate restTemplate;
  49. @Qualifier("highLevelClient")
  50. @Autowired
  51. private RestHighLevelClient highLevelClient;
  52. @Autowired
  53. private IndexService<T> indexService;
  54. /**
  55. * 反射获取es实体代表id的字段名
  56. * @author Innocence
  57. * @date 2021/6/17
  58. * @param clazz
  59. * @return java.lang.String
  60. */
  61. private String getPrimaryNameByClass(Class<T> clazz){
  62. Field[] fields = clazz.getDeclaredFields();
  63. for (Field f:fields) {
  64. DocumentId id = f.getAnnotation(DocumentId.class);
  65. if (id != null){
  66. return f.getName();
  67. }
  68. }
  69. return null;
  70. }
  71. /**
  72. * 根据注解获取实体类关联es属性的id值
  73. * @author Innocence
  74. * @date 2021/6/16
  75. * @return java.lang.String
  76. */
  77. private String getPrimaryValueByEntity(T t){
  78. Class<T> clazz = (Class<T>) t.getClass();
  79. Field[] fields = clazz.getDeclaredFields();
  80. for (Field f:fields) {
  81. Class<?> type = f.getType();
  82. if (type.getTypeName().equals(String.class.getTypeName())){
  83. DocumentId id = f.getAnnotation(DocumentId.class);
  84. if (id != null){
  85. try {
  86. String replace = f.getName()
  87. .replace(f.getName().substring(0, 1), f.getName().substring(0, 1).toUpperCase());
  88. Method method = clazz.getMethod("get" + replace);
  89. String invoke = (String) method.invoke(t);
  90. if (StrUtil.isNotBlank(invoke)){
  91. return invoke;
  92. }else {
  93. return null;
  94. }
  95. } catch (NoSuchMethodException e) {
  96. e.printStackTrace();
  97. } catch (IllegalAccessException e) {
  98. e.printStackTrace();
  99. } catch (InvocationTargetException e) {
  100. e.printStackTrace();
  101. }
  102. }
  103. }
  104. }
  105. return null;
  106. }
  107. /**
  108. * 类上无索引名的统一异常处理
  109. * @author Innocence
  110. * @date 2021/8/6
  111. * @param clazz
  112. * @return void
  113. */
  114. private String indexNameExceptionHandler(Class<T> clazz){
  115. String indexName = indexService.getIndexName(clazz);
  116. if (StrUtil.isBlank(indexName)){
  117. throw new ElasticServiceException("The index name on the current class does not exist!");
  118. }else {
  119. return indexName;
  120. }
  121. }
  122. /**
  123. * 映射高亮字段到原生属性
  124. * @author Innocence
  125. * @date 2021/3/2
  126. * @param searchHits
  127. * @return java.util.List<T>
  128. */
  129. private <T> List<T> mappingHighlight(List<SearchHit<T>> searchHits){
  130. List<T> infoList = new ArrayList<>();
  131. for (SearchHit<T> searchHit : searchHits) {
  132. T content = searchHit.getContent();
  133. Map<String, List<String>> highlightFields = searchHit.getHighlightFields();
  134. for (Map.Entry<String, List<String>> entry : highlightFields.entrySet()) {
  135. try {
  136. PropertyUtils.setProperty(content,entry.getKey(),entry.getValue().get(0));
  137. } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
  138. e.printStackTrace();
  139. }
  140. }
  141. infoList.add(content);
  142. }
  143. return infoList;
  144. }
  145. @Override
  146. public HighlightBuilder getHighlightBuilder(String[] fields) {
  147. HighlightBuilder highlightBuilder = new HighlightBuilder();
  148. for (String field : fields) {
  149. highlightBuilder.field(field);
  150. }
  151. highlightBuilder.requireFieldMatch(false); //如果要多个字段高亮,这项要为false
  152. highlightBuilder.preTags("<span style=\"color:red\">");
  153. highlightBuilder.postTags("</span>");
  154. //下面这两项,如果你要高亮如文字内容等有很多字的字段,必须配置,不然会导致高亮不全,文章内容缺失等
  155. highlightBuilder.fragmentSize(800000); //最大高亮分片数
  156. highlightBuilder.numOfFragments(0); //从第一个分片获取高亮片段
  157. return highlightBuilder;
  158. }
  159. @Override
  160. public Boolean isExist(String id,Class<T> clazz) {
  161. String name = indexNameExceptionHandler(clazz);
  162. return restTemplate.exists(id, IndexCoordinates.of(name));
  163. }
  164. @Override
  165. public String saveByEntity(T t) {
  166. String id = getPrimaryValueByEntity(t);
  167. String indexName = indexNameExceptionHandler((Class<T>) t.getClass());
  168. if (StrUtil.isBlank(id)){
  169. throw new ElasticServiceException("Document id cannot be empty!");
  170. }
  171. IndexQuery build = new IndexQueryBuilder().withId(id).withObject(t).build();
  172. String index = restTemplate.index(build, IndexCoordinates.of(indexName));
  173. //业务需要,新增后马上刷新,ElasticsearchRestTemplate是默认不立即刷新(立即刷新会影响性能)
  174. restTemplate.indexOps(t.getClass()).refresh();
  175. return index;
  176. }
  177. @Override
  178. public List<String> saveBatchByEntities(List<T> entities){
  179. Class<T> clazz =(Class<T>) entities.get(0).getClass();
  180. String indexName = indexNameExceptionHandler(clazz);
  181. List<IndexQuery> queryList = new ArrayList<>();
  182. for (T item:entities){
  183. String id = getPrimaryValueByEntity(item);
  184. if (StrUtil.isBlank(id)){
  185. throw new ElasticServiceException("Document id cannot be empty!");
  186. }
  187. IndexQuery build = new IndexQueryBuilder().withId(id).withObject(item).build();
  188. queryList.add(build);
  189. }
  190. List<String> idList = restTemplate.bulkIndex(queryList, IndexCoordinates.of(indexName));
  191. restTemplate.indexOps(IndexCoordinates.of(indexName)).refresh();
  192. return idList;
  193. }
  194. @Override
  195. public void updateByEntity(T t) {
  196. String indexName = indexNameExceptionHandler((Class<T>) t.getClass());
  197. Document document = Document.parse(JSON.toJSONString(t));
  198. document.setId(getPrimaryValueByEntity(t));
  199. UpdateQuery build = UpdateQuery.builder(document.getId())
  200. .withRefresh(UpdateQuery.Refresh.Wait_For) //更新后立即刷新可见(影响性能,按需注释或使用)
  201. .withDocument(document)
  202. .build();
  203. restTemplate.update(build, IndexCoordinates.of(indexName));
  204. }
  205. @Override
  206. public void updateByEntities(List<T> entities) {
  207. Class<T> clazz =(Class<T>) entities.get(0).getClass();
  208. String indexName = indexNameExceptionHandler(clazz);
  209. List<UpdateQuery> updateQueries = new ArrayList<>();
  210. entities.forEach(item->{
  211. Document document = Document.parse(JSON.toJSONString(item));
  212. document.setId(getPrimaryValueByEntity(item));
  213. UpdateQuery build = UpdateQuery.builder(document.getId())
  214. .withRefresh(UpdateQuery.Refresh.Wait_For)
  215. // .withDocAsUpsert(true) //不加默认false。true表示更新时不存在就插入
  216. .withDocument(document)
  217. .build();
  218. updateQueries.add(build);
  219. });
  220. restTemplate.bulkUpdate(updateQueries,IndexCoordinates.of(indexName));
  221. }
  222. @Override
  223. public String deleteById(String id, Class<T> clazz) {
  224. String indexName = indexNameExceptionHandler(clazz);
  225. return restTemplate.delete(id,IndexCoordinates.of(indexName));
  226. }
  227. @Override
  228. public void deleteByIds(List<String> ids, Class<T> clazz) {
  229. String indexName = indexNameExceptionHandler(clazz);
  230. StringQuery query = new StringQuery(QueryBuilders.termsQuery(getPrimaryNameByClass(clazz), ids).toString());
  231. restTemplate.delete(query,clazz,IndexCoordinates.of(indexName));
  232. }
  233. @Override
  234. public void deleteByQuery(Query query,Class<T> clazz) {
  235. String indexName = indexNameExceptionHandler(clazz);
  236. restTemplate.delete(query,clazz,IndexCoordinates.of(indexName));
  237. }
  238. @Override
  239. public T getEntityById(String id,Class<T> clazz) {
  240. return restTemplate.get(id,clazz);
  241. }
  242. @Override
  243. public List<T> getEntityByIds(List<String> ids, Class<T> clazz) {
  244. String indexName = indexNameExceptionHandler(clazz);
  245. NativeSearchQuery build = new NativeSearchQueryBuilder().withIds(ids).build();
  246. return restTemplate.multiGet(build, clazz, IndexCoordinates.of(indexName));
  247. }
  248. @Override
  249. public Long getCount(Query query,Class<T> clazz) {
  250. return restTemplate.count(query,clazz);
  251. }
  252. @Override
  253. public List<T> getInfoList(Query query,Class<T> clazz,Boolean isHighLight) {
  254. query.setTrackTotalHits(true);
  255. SearchHits<T> search = restTemplate.search(query,clazz);
  256. if (isHighLight){
  257. return mappingHighlight(search.getSearchHits());
  258. }
  259. List<T> infoList = new ArrayList<>();
  260. for (SearchHit<T> searchHit : search){
  261. infoList.add(searchHit.getContent());
  262. }
  263. return infoList;
  264. }
  265. @Override
  266. public Map<String, Object> getPageList(Query query, PageRequest pageRequest,Class<T> clazz) {
  267. query.setTrackTotalHits(true);
  268. query.setPageable(pageRequest);
  269. SearchHits<T> search = restTemplate.search(query,clazz);
  270. Aggregations aggregations = search.getAggregations();
  271. List<SearchHit<T>> searchHits = search.getSearchHits();
  272. List<T> esSourceInfos = mappingHighlight(searchHits);
  273. Page infos = new PageImpl(
  274. esSourceInfos,
  275. pageRequest,
  276. search.getTotalHits());
  277. Map<String, Object> map = new HashMap<>();
  278. map.put("page",infos);
  279. map.put("ag",formatFacet(aggregations));
  280. return map;
  281. }
  282. @Override
  283. public Map<String, List<? extends Terms.Bucket>> getFacetByQuery(SearchSourceBuilder query, Class<T> clazz) throws IOException {
  284. String indexName = indexNameExceptionHandler(clazz);
  285. SearchRequest request = new SearchRequest(indexName);
  286. SearchSourceBuilder builder = query;
  287. request.source(builder);
  288. SearchResponse response = highLevelClient.search(request, RequestOptions.DEFAULT);
  289. Aggregations aggregations = response.getAggregations();
  290. return formatFacet(aggregations);
  291. }
  292. /**
  293. * 格式化聚类数据
  294. * @author Innocence
  295. * @date 2021/3/18
  296. * @param aggregations
  297. * @return java.util.Map<java.lang.String,java.util.List<java.util.Map<java.lang.String,java.lang.Object>>>
  298. */
  299. private Map<String, List<? extends Terms.Bucket>> formatFacet(Aggregations aggregations){
  300. if (aggregations == null){
  301. return null;
  302. }
  303. Map<String, List<? extends Terms.Bucket>> map = new HashMap<>();
  304. List<Aggregation> list = aggregations.asList();
  305. list.forEach(item->{
  306. ParsedStringTerms newItem = (ParsedStringTerms) item;
  307. String name = newItem.getName();
  308. List<? extends Terms.Bucket> buckets = newItem.getBuckets();
  309. map.put(name,buckets);
  310. });
  311. return map;
  312. }
  313. }

 5.使用示例,使用@Autowired自动注入,并且带上泛型类

  1. package com.cqvip.innocence.tests;
  2. import com.cqvip.innocence.project.esservice.DocumentService;
  3. import com.cqvip.innocence.project.esservice.IndexService;
  4. import com.cqvip.innocence.project.model.entity.EsTestInfo;
  5. import org.apache.commons.lang3.builder.ToStringExclude;
  6. import org.elasticsearch.index.query.BoolQueryBuilder;
  7. import org.elasticsearch.index.query.QueryBuilders;
  8. import org.junit.jupiter.api.Test;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.boot.test.context.SpringBootTest;
  11. import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
  12. import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15. /**
  16. * @ClassName EsTest
  17. * @Description TODO
  18. * @Author Innocence
  19. * @Date 2021/6/16 14:05
  20. * @Version 1.0
  21. */
  22. @SpringBootTest
  23. public class EsTest {
  24. @Autowired
  25. private IndexService<EsTestInfo> indexService;
  26. @Autowired
  27. private DocumentService<EsTestInfo> documentService;
  28. @Test
  29. public void indexOperation(){
  30. Boolean index = indexService.createIndexByClass(EsTestInfo.class);
  31. System.out.println("index = " + index);
  32. }
  33. @Test
  34. public void saveByEntity(){
  35. EsTestInfo esTestInfo = new EsTestInfo();
  36. esTestInfo.setAge(20);
  37. esTestInfo.setInfoId("test_02");
  38. esTestInfo.setName("一库一库");
  39. esTestInfo.setSex("男");
  40. String entity = null;
  41. try {
  42. entity = documentService.saveByEntity(esTestInfo);
  43. } catch (Exception e) {
  44. e.printStackTrace();
  45. }
  46. System.out.println("entity = " + entity);
  47. }
  48. @Test
  49. public void isExist(){
  50. Boolean test_01 = documentService.isExist("test_01", EsTestInfo.class);
  51. System.out.println("test_01 = " + test_01);
  52. }
  53. @Test
  54. public void updateByEntity(){
  55. EsTestInfo info = documentService.getEntityById("test_01", EsTestInfo.class);
  56. info.setName(info.getName()+"_update");
  57. documentService.updateByEntity(info);
  58. }
  59. @Test
  60. public void deleteById(){
  61. String deleteById = documentService.deleteById("test_01", EsTestInfo.class);
  62. System.out.println("deleteById = " + deleteById);
  63. }
  64. @Test
  65. public void saveBatchByEntities(){
  66. List<EsTestInfo> list = new ArrayList<>();
  67. for (int i = 0; i < 10; i++) {
  68. EsTestInfo info = new EsTestInfo();
  69. info.setInfoId("test_batch_"+(i+1)+"");
  70. info.setName("name_batch_"+(i+1)+"");
  71. info.setSex("N");
  72. info.setAge((i+1)*10);
  73. info.setRoles(new String[]{"管理员","超级管理员"});
  74. list.add(info);
  75. }
  76. try {
  77. documentService.saveBatchByEntities(list);
  78. } catch (Exception e) {
  79. e.printStackTrace();
  80. }
  81. }
  82. @Test
  83. public void getInfoList(){
  84. NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
  85. BoolQueryBuilder boolQuery = QueryBuilders.boolQuery()
  86. .must(QueryBuilders.wildcardQuery("infoId","test_batch_*"));
  87. NativeSearchQuery build = queryBuilder.withQuery(boolQuery).build();
  88. List<EsTestInfo> infoList = documentService.getInfoList(build,EsTestInfo.class,true);
  89. }
  90. @Test
  91. public void count(){
  92. Long count = documentService.getCount(new NativeSearchQueryBuilder().build(),EsTestInfo.class);
  93. System.out.println("count = " + count);
  94. }
  95. @Test
  96. public void deleteByIds(){
  97. ArrayList<String> strings = new ArrayList<>();
  98. strings.add("test_01");
  99. strings.add("test_02");
  100. Class<EsTestInfo> clazz = EsTestInfo.class;
  101. documentService.deleteByIds(strings, clazz);
  102. }
  103. @Test
  104. public void update() {
  105. EsTestInfo info = new EsTestInfo();
  106. info.setName("wait_for");
  107. info.setInfoId("test_batch_wait_for_2");
  108. documentService.saveByEntity(info);
  109. NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
  110. BoolQueryBuilder boolQuery = QueryBuilders.boolQuery()
  111. .must(QueryBuilders.wildcardQuery("infoId","test_batch_*"));
  112. NativeSearchQuery build = queryBuilder.withQuery(boolQuery).build();
  113. List<EsTestInfo> infoList = documentService.getInfoList(build,EsTestInfo.class);
  114. System.out.println("infoList = " + infoList);
  115. }
  116. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/325740
推荐阅读
相关标签
  

闽ICP备14008679号