上一篇介绍了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.1</elasticsearch.version> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>${elasticsearch.version}</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <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(EsData<AqZzFile> esData) { 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<AqZzFile> 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<AqZzFile> 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<Map<String, Object>> searchListData(EsData<AqZzFile> esData) { String indexName = INDEXNAME; Integer from = esData.getFrom(); Integer size = esData.getSize(); String highlightField = "title"; String startTime = esData.getStartTime(); String endTime = esData.getEndTime(); String sortField = esData.getSortField(); AqZzFile aqZzFile = esData.getData(); SearchRequest searchRequest = new SearchRequest(indexName); //构建查询条件 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); if (Objects.nonNull(aqZzFile)) { //模糊匹配 if (StringUtils.isNoneBlank(aqZzFile.getTitle())) { searchSourceBuilder.query(QueryBuilders.matchQuery("title",aqZzFile.getTitle())); } //匹配(精准匹配) if (StringUtils.isNoneBlank(aqZzFile.getJzCode())) { searchSourceBuilder.query(QueryBuilders.termQuery("jzCode", aqZzFile.getJzCode())); } //匹配(精准匹配) if (StringUtils.isNoneBlank(aqZzFile.getJxCode())) { searchSourceBuilder.query(QueryBuilders.termQuery("jxCode", aqZzFile.getJxCode())); } } //时间范围匹配 if (StringUtils.isNoneBlank(startTime) && StringUtils.isNoneBlank(endTime)) { searchSourceBuilder.query(QueryBuilders.rangeQuery("creatTime").from(DateTime.of(startTime,"yyyy-MM-dd hh:mm:ss")).to(DateTime.of(endTime,"yyyy-MM-dd hh:mm:ss"))); } //设置确定结果要从哪个索引开始搜索的from选项,默认为0 from = from <= 1 ? 0 : (from -1) * size; searchSourceBuilder.from(from); searchSourceBuilder.size(size); if (StringUtils.isNotEmpty(sortField)) { //排序字段,注意如果proposal_no是text类型会默认带有keyword性质,需要拼接.keyword searchSourceBuilder.sort(sortField + ".keyword", SortOrder.ASC); } //高亮 if (StringUtils.isNoneBlank(highlightField)) { HighlightBuilder highlight = new HighlightBuilder(); highlight.field(highlightField); //关闭多个高亮 highlight.requireFieldMatch(false); highlight.preTags("<span style='color:red'>"); highlight.postTags("</span>"); searchSourceBuilder.highlighter(highlight); } //不返回源数据。只有条数之类的数据。 //builder.fetchSource(false); searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT); log.info("==" + searchResponse.getHits().getTotalHits()); if (searchResponse.status().getStatus() == 200) { // 解析对象 return setSearchResponse(searchResponse, highlightField); } return null; } /** * 高亮结果集 特殊处理 * map转对象 JSONObject.parseObject(JSONObject.toJSONString(map), Content.class) * @param searchResponse * @param highlightField */ public List<Map<String, Object>> setSearchResponse(SearchResponse searchResponse, String highlightField) { //解析结果 ArrayList<Map<String, Object>> list = new ArrayList<>(); for (SearchHit hit : searchResponse.getHits().getHits()) { Map<String, HighlightField> high = hit.getHighlightFields(); HighlightField title = high.get(highlightField); hit.getSourceAsMap().put("id", hit.getId()); //原来的结果 Map<String, Object> sourceAsMap = hit.getSourceAsMap(); //解析高亮字段,将原来的字段换为高亮字段 if (title != null) { Text[] texts = title.fragments(); String nTitle = ""; for (Text text : texts) { nTitle += text; } //替换 sourceAsMap.put(highlightField, nTitle); } list.add(sourceAsMap); } return list; } }
@ApiModel(value = "es搜索",description = "es搜索") public class EsData<T> implements 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 CommentParam<AqZzFile> commentParam) { log.info("自主学习查询入参为: {} ", JSON.toJSONString(commentParam)); Page page = commentParam.getPage(); AqZzFile aqZzFile = commentParam.getData(); IPage<List<AqZzFile>> 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<AqZzFile> esData) { log.info("自主学习搜索引擎查询入参为: {} ", JSON.toJSONString(esData)); List<Map<String, Object>> pageList = aqZzFileService.search(esData); log.info("自主学习搜索引擎查询获取的数据为: {}", JSON.toJSONString(pageList)); if (Objects.nonNull(pageList)) { return ResponseData.ok(pageList); } return ResponseData.failed(ConstantCode.DATA_EMPTY); } }
已经测试通过