spring boot 集成 elasticsearch7.10.0(demo)
上一篇介绍了6.5.3的集成,这次是7.10.0集成,使用elasticsearch-rest-high-level-client,可以看下和使用spring-data的区别还是挺大
说明一下使用两个版本主要是两个项目不一样,都是实际案例
1.开发环境:
springboot 2.2.8
elasticsearch 7.10.0 9200 9300 单机部署
2.pom文件
<elasticsearch.version>7.10.1elasticsearch.version> <dependency> <groupId>org.elasticsearchgroupId> <artifactId>elasticsearchartifactId> <version>${elasticsearch.version}version> dependency> <dependency> <groupId>org.elasticsearch.clientgroupId> <artifactId>elasticsearch-rest-high-level-clientartifactId> <version>${elasticsearch.version}version> dependency>
3.bootstrap文件
# elasticsearch配置文件
elasticsearch:
rest:
uris: http://127.0.0.1:9200
read-timeout: 90
username:
password:
4.获取properties配置
@Data @Component public class ElasticsearchProperties { private static final String INDEXNAME = "demo"; @Value("${spring.elasticsearch.rest.uris}") private String uris; @Value("${spring.elasticsearch.rest.username}") private String username; @Value("${spring.elasticsearch.rest.password}") private String password; }
5.初始化bean
@Configuration @Slf4j public class ElasticSearchConfiguration { @Autowired private ElasticsearchProperties elasticsearchProperties; @Bean(destroyMethod = "close") @Scope("singleton") public RestHighLevelClient restHighLevelClient() { String username = elasticsearchProperties.getUsername(); String password = elasticsearchProperties.getPassword(); String scheme = elasticsearchProperties.getUris().split(":")[0]; String[] hosts = elasticsearchProperties.getUris().split("\\//")[1].split(":"); String hostName = hosts[0]; Integer port = Integer.parseInt(hosts[1]); HttpHost httpHosts = new HttpHost(hostName, port, scheme); RestClientBuilder restClientBuilder = RestClient.builder(httpHosts); //es账号密码(默认用户名为elastic) if (StringUtils.isNoneBlank(username)) { final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); restClientBuilder.setHttpClientConfigCallback(httpClientBuilder -> { httpClientBuilder.disableAuthCaching(); httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); return httpClientBuilder; }); } RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder); return restHighLevelClient; } }
6.索引操作
@Service @Slf4j public class ElasticsearchIndex { @Autowired private RestHighLevelClient restHighLevelClient; /** * 创建索引 * @param indexName * @return */ @SneakyThrows public boolean createIndex(String indexName) { boolean flag = existIndex(indexName); if (!flag) { CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName); restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT); log.info("创建 {} 索引成功",indexName); flag = true; } else { log.info("{} 索引已经存在",indexName); } return flag; } /** * 检查索引是否存在 */ @SneakyThrows public boolean existIndex(String indexName) { GetIndexRequest getIndexRequest = new GetIndexRequest(indexName); return restHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT); } /** * 删除索引 * @param indexName */ @SneakyThrows public boolean deleteIndex(String indexName) { boolean flag = existIndex(indexName); if (flag) { DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(indexName); AcknowledgedResponse acknowledgedResponse = restHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT); log.info("删除 {} 索引成功",indexName); return acknowledgedResponse.isAcknowledged(); } log.info("删除 {} 索引失败",indexName); return false; } }
7.文档操作及封装对象
@Service @Slf4j public class ElasticsearchDocument { @Autowired private RestHighLevelClient restHighLevelClient; @Autowired private ElasticsearchIndex elasticsearchIndex; private static final String INDEXNAME = "demo"; @SneakyThrows public void insertDocument(EsDataesData) { String indexName = INDEXNAME; AqZzFile aqZzFile = esData.getData(); elasticsearchIndex.createIndex(indexName); IndexRequest indexRequest = new IndexRequest(indexName); indexRequest.id(aqZzFile.getId()); indexRequest.source(JSON.toJSONString(aqZzFile),XContentType.JSON); IndexResponse indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT); log.info("索引{} 插入数据成功,id是 {}", indexName, aqZzFile.getId()); } @SneakyThrows public void updateDocument(EsData esData) { String indexName = INDEXNAME; AqZzFile aqZzFile = esData.getData(); boolean flag = existsById(indexName, aqZzFile.getId()); if (flag) { UpdateRequest updateRequest = new UpdateRequest(indexName,aqZzFile.getId()); updateRequest.doc(JSON.toJSONString(aqZzFile), XContentType.JSON); UpdateResponse updateResponse = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT); log.info("{} 索引中id为 {} 的文档修改成功!", indexName, aqZzFile.getId()); } } @SneakyThrows public void deleteDocument(String indexName,String id) { boolean flag = existsById(indexName, id); if (flag) { DeleteRequest deleteRequest = new DeleteRequest(indexName,id); DeleteResponse deleteResponse = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT); log.info("{} 索引中id为 {} 的文档删除成功!", indexName, id); } } @SneakyThrows public boolean existsById(String indexName,String id){ GetRequest request = new GetRequest(indexName, id); //不获取返回的_source的上下文 request.fetchSourceContext(new FetchSourceContext(false)); request.storedFields("_none_"); boolean flag = restHighLevelClient.exists(request, RequestOptions.DEFAULT); log.info("{} 索引中 {} 的文档是否 {} 存在", indexName, id, flag); return flag; } @SneakyThrows public void searchDocument(EsData esData) { String indexName = INDEXNAME; AqZzFile aqZzFile = esData.getData(); SearchRequest searchRequest = new SearchRequest(indexName); //构建搜索条件 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); //高亮 HighlightBuilder highlightBuilder = new HighlightBuilder(); highlightBuilder.field(""); searchSourceBuilder.highlighter(highlightBuilder); //执行查询 searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT); SearchHits hits = searchResponse.getHits(); } /** * 查询并分页 * @return */ @SneakyThrows public List
@ApiModel(value = "es搜索",description = "es搜索") public class EsDataimplements Serializable { private static final long serialVersionUID = 5626216451006492696L; @ApiModelProperty(value = "每页条数") private Integer size = 10; @ApiModelProperty(value = "第几页开始") private Integer from = 0; @ApiModelProperty(value = "排序字段") private String sortField; @ApiModelProperty(value = "开始时间") private String startTime; @ApiModelProperty(value = "结束时间") private String endTime; private T data; }
8.controller
@RequestMapping(value = "/xxx/file") public class AqZzFileController { @Autowired private AqZzFileService aqZzFileService; @PostMapping("/getPageData") @ApiOperation(value = "自主学习-列表(分页)查询") public ResponseData getPageData(@RequestBody CommentParamcommentParam) { log.info("自主学习查询入参为: {} ", JSON.toJSONString(commentParam)); Page page = commentParam.getPage(); AqZzFile aqZzFile = commentParam.getData(); IPage > pageList = aqZzFileService.getAllPageData(aqZzFile, page); log.info("自主学习分页查询获取的数据为: {}", JSON.toJSONString(pageList)); if (Objects.nonNull(pageList)) { return ResponseData.ok(pageList); } return ResponseData.failed(ConstantCode.DATA_EMPTY); } @PostMapping @ApiOperation(value = "新增") public ResponseData save(@RequestBody AqZzFile aqZzFile){ log.info("文件管理新增入参为: {} ", JSON.toJSONString(aqZzFile)); try { aqZzFileService.saveOrUpdateEntity(aqZzFile); return ResponseData.ok(null); } catch (Exception e) { e.printStackTrace(); } return ResponseData.failed(ConstantCode.SERVER_ERROR); } @ApiOperation(value = "自主学习-按id查询数据") @GetMapping("/getDataById/{id}") public ResponseData getDataById(@PathVariable("id") String id){ log.info("数据字典按ID查询数据入参为: {} ", id); AqZzFile aqZzFile = aqZzFileService.getById(id); if (Objects.nonNull(aqZzFile)) { return ResponseData.ok(aqZzFile); } return ResponseData.failed(ConstantCode.DATA_EMPTY); } @PutMapping("/{id}") @ApiOperation(value = "修改") public ResponseData update(@PathVariable(name = "id") String id, @RequestBody AqZzFile aqZzFile) { log.info("文件管理管理修改入参为: {}, {} ", id, JSON.toJSONString(aqZzFile)); try { aqZzFile.setId(id); aqZzFileService.saveOrUpdateEntity(aqZzFile); return ResponseData.ok(null); } catch (Exception e) { e.printStackTrace(); } return ResponseData.failed(ConstantCode.SERVER_ERROR); } @DeleteMapping("/{id}") @ApiOperation(value = "按ID删除") public ResponseData remove(@PathVariable(name = "id") String id){ log.info("文件管理按ID删除入参为: {} ",id); try { aqZzFileService.deleteEntity(id); return ResponseData.ok(null); } catch (Exception e) { e.printStackTrace(); } return ResponseData.failed(ConstantCode.SERVER_ERROR); } @PostMapping("/getPageData/search") @ApiOperation(value = "自主学习-搜索引擎搜索") public ResponseData search(@RequestBody EsData
esData) { log.info("自主学习搜索引擎查询入参为: {} ", JSON.toJSONString(esData)); List > pageList = aqZzFileService.search(esData); log.info("自主学习搜索引擎查询获取的数据为: {}", JSON.toJSONString(pageList)); if (Objects.nonNull(pageList)) { return ResponseData.ok(pageList); } return ResponseData.failed(ConstantCode.DATA_EMPTY); } }
已经测试通过