Elasticsearch7.7.0+springboot 2.1.6.RELEASE集成测试一个小demo,详细可参考https://www.jianshu.com/p/1fbfde2aefa5
1.加依赖 因为spring-boot-starter-data-elasticsearch,最新版的starter现在依然是6.x版本号,并没有集成elasticsearch7.7.0,导致使用过程中有很多版本冲突 采用elasticsearch-rest-high-level-client方式来集成!
<!-- es 7.7.0版本 --><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>7.7.0</version><exclusions><exclusion><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId></exclusion><exclusion><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-client</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-client</artifactId><version>7.7.0</version></dependency><dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId><version>7.7.0</version></dependency>
2.yml加配置
es:host: 192.168.2.101 #elasticsearch 服务地址port: 9200scheme: http
3.编写EsUtil工具类及ElasticEntity类
import cn.****.entity.ElasticEntity;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.*;@Component
public class DemoEs {@Value("${es.host}")public String host;@Value("${es.port}")public int port;@Value("${es.scheme}")public String scheme;public static final String INDEX_NAME = "post-index";/*** 根据自己的业务去定义(这里是测试使用)**/public static final String CREATE_INDEX = "{\n" +" \"properties\": {\n" +" \"id\":{\n" +" \"type\":\"integer\"\n" +" },\n" +" \"userId\":{\n" +" \"type\":\"integer\"\n" +" },\n" +" \"name\":{\n" +" \"type\":\"text\",\n" +" \"analyzer\": \"ik_max_word\",\n" +" \"search_analyzer\": \"ik_smart\"\n" +" },\n" +" \"url\":{\n" +" \"type\":\"text\",\n" +" \"index\": true,\n" +" \"analyzer\": \"ik_max_word\",\n" +" \"search_analyzer\": \"ik_smart\"\n" +" }\n" +" }\n" +" }";public static RestHighLevelClient client = null;@PostConstructpublic void init() {try {if (client != null) {client.close();}client = new RestHighLevelClient(RestClient.builder(new HttpHost(host, port, scheme)));if (this.indexExist(INDEX_NAME)) {return;}CreateIndexRequest request = new CreateIndexRequest(INDEX_NAME);request.settings(Settings.builder().put("index.number_of_shards", 3).put("index.number_of_replicas", 2));request.mapping(CREATE_INDEX, XContentType.JSON);CreateIndexResponse res = client.indices().create(request, RequestOptions.DEFAULT);if (!res.isAcknowledged()) {throw new RuntimeException("初始化失败");}} catch (Exception e) {e.printStackTrace();System.exit(0);}}/*** Description: 判断某个index是否存在** @param index index名* @return boolean*/public boolean indexExist(String index) throws Exception {GetIndexRequest request = new GetIndexRequest(index);request.local(false);request.humanReadable(true);request.includeDefaults(false);return client.indices().exists(request, RequestOptions.DEFAULT);}/*** Description: 插入/更新一条记录** @param index index* @param entity 对象*/public void insertOrUpdateOne(String index, ElasticEntity entity) {IndexRequest request = new IndexRequest(index);request.id(entity.getId());request.source(JSON.toJSONString(entity.getData()), XContentType.JSON);try {client.index(request, RequestOptions.DEFAULT);} catch (Exception e) {throw new RuntimeException(e);}}/*** Description: 批量插入数据** @param index index* @param list 带插入列表*/public void insertBatch(String index, List<ElasticEntity> list) {BulkRequest request = new BulkRequest();list.forEach(item -> request.add(new IndexRequest(index).id(item.getId()).source(JSON.toJSONString(item.getData()), XContentType.JSON)));try {client.bulk(request, RequestOptions.DEFAULT);} catch (Exception e) {throw new RuntimeException(e);}}/*** Description: 批量删除** @param index index* @param idList 待删除列表*/public <T> void deleteBatch(String index, Collection<T> idList) {BulkRequest request = new BulkRequest();idList.forEach(item -> request.add(new DeleteRequest(index, item.toString())));try {client.bulk(request, RequestOptions.DEFAULT);} catch (Exception e) {throw new RuntimeException(e);}}/*** Description: 搜索** @param index index* @param builder 查询参数* @param c 结果类对象* @return java.util.ArrayList*/public <T> List<T> search(String index, SearchSourceBuilder builder, Class<T> c) {SearchRequest request = new SearchRequest(index);request.source(builder);try {SearchResponse response = client.search(request, RequestOptions.DEFAULT);SearchHit[] hits = response.getHits().getHits();List<T> res = new ArrayList<>(hits.length);for (SearchHit hit : hits) {res.add(JSON.parseObject(hit.getSourceAsString(), c));}System.err.println("查询符合满足条件的总条数=" + response.getHits().getTotalHits());return res;} catch (Exception e) {throw new RuntimeException(e);}}/*** 常用分页查询 from size** @param index* @param page* @param builder* @param c* @param <T>* @return* @throws IOException*/public <T> Page<T> searchPage(String index, Page page, SearchSourceBuilder builder, Class<T> c) throws IOException {SearchRequest searchRequest = new SearchRequest(index);searchRequest.source(builder);SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);long totalCount = response.getHits().getTotalHits().value;page.setTotal(totalCount);//数据总页数SearchHit[] hits = response.getHits().getHits();List<T> res = new ArrayList<>(hits.length);for (SearchHit hit : hits) {res.add(JSON.parseObject(hit.getSourceAsString(), c));}page.setRecords(res);return page;}/*** Description: 删除index** @param index index* @return void*/public void deleteIndex(String index) {try {client.indices().delete(new DeleteIndexRequest(index), RequestOptions.DEFAULT);} catch (Exception e) {throw new RuntimeException(e);}}/*** Description: delete by query** @param index index* @param builder builder*/public void deleteByQuery(String index, QueryBuilder builder) {DeleteByQueryRequest request = new DeleteByQueryRequest(index);request.setQuery(builder);//设置批量操作数量,最大为10000request.setBatchSize(10000);request.setConflicts("proceed");try {client.deleteByQuery(request, RequestOptions.DEFAULT);} catch (Exception e) {throw new RuntimeException(e);}}}
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;/*** 数据存储对象*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ElasticEntity<T> {/*** 主键标识,用户ES持久化*/private String id;/*** JSON对象,实际存储数据*/private T data;
}
4.编写service
@Service
public class PostSearchService {@Autowiredprivate EsUtil esUtil;@Autowiredprivate PostService postService;//业务的service /*** 初始化ES查询数据* @return*/public int initEsData() {List<PostDocument> postDocuments = postService.selectPostDocumentList();if(!CollectionUtils.isEmpty(postDocuments)){List<ElasticEntity> list = new ArrayList<>();postDocuments.forEach(item -> list.add(new ElasticEntity<>(item.getId().toString(), item)));esUtil.insertBatch(EsUtil.INDEX_NAME, list);}return postDocuments.size();}
}/*** 业务实体类(跟EsUtil中CREATE_INDEX的json对象相对应 )* set get方法略*/class PostDocument{private Integer id;private Integer userId;private String name;private String url;}
5.编写controller
@ResponseBody@PostMapping("/initEsData")public ResponseData initEsData() {int total = PostSearchService .initEsData();return ResponseData.success("ES索引初始化成功,共 " + total + " 条记录!");}