springboot集成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.1</elasticsearch.version>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
springboot原理pdf<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
3.bootstrap⽂件
# elasticsearch配置⽂件
elasticsearch:
rest:
uris: 127.0.0.1:9200
read-timeout: 90
username:
password:
4.获取properties配置
@Data
@Component
public class ElasticsearchProperties {
private static final String INDEXNAME = "demo";
@Value("${st.uris}")
private String uris;
@Value("${st.username}")
private String username;
@Value("${st.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 = Username();
String password = Password();
String scheme = Uris().split(":")[0];
String[] hosts = Uris().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.索引操作
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 = Data();
IndexRequest indexRequest = new IndexRequest(indexName);
indexRequest.Id());
indexRequest.JSONString(aqZzFile),XContentType.JSON);
IndexResponse indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
log.info("索引{} 插⼊数据成功,id是 {}", indexName, Id());
}
@SneakyThrows
public void updateDocument(EsData<AqZzFile> esData) {
String indexName = INDEXNAME;
AqZzFile aqZzFile = Data();
boolean flag = existsById(indexName, Id());
if (flag) {
UpdateRequest updateRequest = new UpdateRequest(Id());
updateRequest.JSONString(aqZzFile), XContentType.JSON);
UpdateResponse updateResponse = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
log.info("{} 索引中id为 {} 的⽂档修改成功!", indexName, Id());
@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 = ists(request, RequestOptions.DEFAULT);
log.info("{} 索引中 {} 的⽂档是否 {} 存在", indexName, id, flag);
return flag;
}
@SneakyThrows
public void searchDocument(EsData<AqZzFile> esData) {
String indexName = INDEXNAME;
AqZzFile aqZzFile = Data();
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 = Hits();
}
/**
* 查询并分页
* @return
*/
@SneakyThrows
public List<Map<String, Object>> searchListData(EsData<AqZzFile> esData) {
String indexName = INDEXNAME;
Integer from = From();
Integer size = Size();
String highlightField = "title";
String startTime = StartTime();
String endTime = EndTime();
String sortField = SortField();
AqZzFile aqZzFile = Data();
SearchRequest searchRequest = new SearchRequest(indexName);
//构建查询条件
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
if (Null(aqZzFile)) {
//模糊匹配
if (StringUtils.Title())) {
searchSourceBuilder.query(QueryBuilders.matchQuery("title",Title()));
}
//匹配(精准匹配)
if (StringUtils.JzCode())) {
searchSourceBuilder.Query("jzCode", JzCode()));
}
//匹配(精准匹配)
if (StringUtils.JxCode())) {
searchSourceBuilder.Query("jxCode", JxCode()));
}
}
//时间范围匹配
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);
}
//⾼亮
HighlightBuilder highlight = new HighlightBuilder();
highlight.field(highlightField);
//关闭多个⾼亮
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("==" + Hits().getTotalHits());
if (searchResponse.status().getStatus() == 200) {
// 解析对象
return setSearchResponse(searchResponse, highlightField);
}
return null;
}
/**
* ⾼亮结果集特殊处理
* map转对象 JSONObject.JSONString(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 : Hits().getHits()) {
Map<String, HighlightField> high = HighlightFields();
HighlightField title = (highlightField);
//原来的结果
Map<String, Object> sourceAsMap = SourceAsMap();
//解析⾼亮字段,将原来的字段换为⾼亮字段
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;
}
@RequestMapping(value = "/xxx/file")
public class AqZzFileController {
@Autowired
private AqZzFileService aqZzFileService;
@PostMapping("/getPageData")
@ApiOperation(value = "⾃主学习-列表(分页)查询")
public ResponseData getPageData(@RequestBody CommentParam<AqZzFile> commentParam) {
log.info("⾃主学习查询⼊参为: {} ", JSONString(commentParam));
Page page = Page();
AqZzFile aqZzFile = Data();
IPage<List<AqZzFile>> pageList = AllPageData(aqZzFile, page);
log.info("⾃主学习分页查询获取的数据为: {}", JSONString(pageList));
if (Null(pageList)) {
return ResponseData.ok(pageList);
}
}
@PostMapping
@ApiOperation(value = "新增")
public ResponseData save(@RequestBody AqZzFile aqZzFile){
log.info("⽂件管理新增⼊参为: {} ", JSONString(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 = ById(id);
if (Null(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, JSONString(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("⾃主学习搜索引擎查询⼊参为: {} ", JSONString(esData));
List<Map<String, Object>> pageList = aqZzFileService.search(esData);
log.info("⾃主学习搜索引擎查询获取的数据为: {}", JSONString(pageList));
if (Null(pageList)) {
return ResponseData.ok(pageList);
}
return ResponseData.failed(ConstantCode.DATA_EMPTY);
}
}
已经测试通过

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。