ElasticSearch7.3.2-RestHighLevelClient实战

2023-12-03 12:58

本文主要是介绍ElasticSearch7.3.2-RestHighLevelClient实战,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、项目依赖

既然我们server安装7.3.2,那么我们客户端选择同样版本,高版本提供了更高层的api抽象封装。

<!--引入es -->
<dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId><version>7.3.2</version>
</dependency><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>7.3.2</version>
</dependency>

二、项目配置

  • application.properties配置
spring.elasticsearch.nodes=10.143.228.25:9200,10.143.228.26:9200,10.143.228.128:9200
spring.elasticsearch.username=elastic
spring.elasticsearch.password=elastic
  • config bean 配置
import com.google.common.collect.Lists;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.util.Arrays;
import java.util.List;/*** @description: elasticSearch配置* @Date : 2020/5/14 上午11:37* @Author : 石冬冬-Seig Heil*/
@Configuration
public class ElasticSearchConfig{/*** 使用的协议*/final static String schema = "http";/*** 连接超时时间*/final static int connectTimeOut = 1000;/*** 连接超时时间*/final static int socketTimeOut = 30000;/*** 获取连接的超时时间*/final static int connectionRequestTimeOut = 500;/*** 最大连接数*/final static int maxConnectNum = 100;/*** 最大路由连接数*/final static int maxConnectPerRoute = 100;@Value("${spring.elasticsearch.nodes}")String[] nodes;@Value("${spring.elasticsearch.username}")String username;@Value("${spring.elasticsearch.password}")String password;@Bean(name = "restHighLevelClient")public RestHighLevelClient client() {RestClientBuilder builder = RestClient.builder(hostList().toArray(new HttpHost[0]));final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();credentialsProvider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials(username, password));builder.setHttpClientConfigCallback(httpClientBuilder -> {httpClientBuilder.disableAuthCaching();return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);});// 异步httpclient连接延时配置builder.setRequestConfigCallback(requestConfigBuilder -> {requestConfigBuilder.setConnectTimeout(connectTimeOut);requestConfigBuilder.setSocketTimeout(socketTimeOut);requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeOut);return requestConfigBuilder;});// 异步httpclient连接数配置builder.setHttpClientConfigCallback(httpClientBuilder -> {httpClientBuilder.setMaxConnTotal(maxConnectNum);httpClientBuilder.setMaxConnPerRoute(maxConnectPerRoute);return httpClientBuilder;});return new RestHighLevelClient(builder);}List<HttpHost> hostList(){List<HttpHost> nodeList = Lists.newArrayList();Arrays.asList(nodes).stream().forEach(each -> {String[] array = each.split(":");nodeList.add(new HttpHost(array[0],Integer.valueOf(array[1]),schema));});return nodeList;}
}

三、示例介绍

3.1、创建索引

settings是修改分片和副本数的,mappings是修改字段和类型的。

@ApiOperation(value = "createIndex", notes = "createIndex")
@PostMapping("/createIndex/{indexName}")
public Result createIndex(@PathVariable String indexName, @RequestBody Map<String,Map<String,Object>> config) {try {CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName);createIndexRequest.settings(config.get("settings"));createIndexRequest.mapping(config.get("mapping"));CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);return Result.suc(JSONObject.toJSON(createIndexResponse));} catch (IOException e) {log.error("[createIndex]",e);return Result.fail(e);}
}

3.2、判断索引是否存在

public boolean exists(String index) {boolean exists;try {GetIndexRequest getIndexRequest = new GetIndexRequest(index);getIndexRequest.humanReadable(true);exists = client.indices().exists(getIndexRequest, DEFAULT);} catch (IOException e) {log.error("[ElasticSearch]exists,index={}",index, e);throw new RuntimeException("[ElasticSearch]exists异常");}return exists;
}

3.3、删除索引

public boolean deleteIndex(String... indices) {boolean acknowledged;try {DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(indices);AcknowledgedResponse delete = client.indices().delete(deleteIndexRequest,DEFAULT);acknowledged = delete.isAcknowledged();} catch (IOException e) {log.error("[ElasticSearch]deleteIndex,indices={}", JSON.toJSONString(indices), e);throw new RuntimeException("[ElasticSearch]deleteIndex异常");}return acknowledged;
}

3.4、插入文档

@ApiOperation(value = "insertRecord", notes = "insertRecord")
@PostMapping("/insertRecord/{indexName}")
public Result insertRecord(@RequestBody Map<String,Object> param, @PathVariable String indexName) {IndexRequest indexRequest = new IndexRequest(indexName);String userJson = JSONObject.toJSONString(param);indexRequest.source(userJson, XContentType.JSON);try {IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);if (response != null) {ReplicationResponse.ShardInfo shardInfo = response.getShardInfo();if (shardInfo.getTotal() != shardInfo.getSuccessful()) {log.info("shardInfo={}", JSONObject.toJSON(shardInfo));}// 如果有分片副本失败,可以获得失败原因信息if (shardInfo.getFailed() > 0) {for (ReplicationResponse.ShardInfo.Failure failure : shardInfo.getFailures()) {String reason = failure.reason();log.info("副本失败原因,reason={}",reason);}}}return Result.suc(JSONObject.toJSON(response));} catch (IOException e) {log.info("[insertRecord]",e);return Result.fail(e);}
}

3.5、批量插入文档

@ApiOperation(value = "bulkInsertRecord", notes = "bulkInsertRecord")
@PostMapping("/bulkInsertRecord/{indexName}")
public Result bulkInsertRecord(@RequestBody List<Map<String,Object>> batch, @PathVariable String indexName) {BulkRequest bulkRequest = new BulkRequest();batch.forEach(each -> {IndexRequest indexRequest = new IndexRequest(indexName).source(each, XContentType.JSON);bulkRequest.add(indexRequest);});//同步try {BulkResponse response = client.bulk(bulkRequest,RequestOptions.DEFAULT);return Result.suc(JSONObject.toJSON(response));} catch (IOException e) {log.info("[updateRecord]",e);return Result.fail(e);}
}

3.6、更新文档

@ApiOperation(value = "updateRecord", notes = "updateRecord")
@PostMapping("/updateRecord/{indexName}")
public Result updateRecord(@PathVariable String indexName, @RequestParam String id,@RequestBody Map<String,Object> param) {UpdateRequest updateRequest = new UpdateRequest(indexName,id);updateRequest.doc(param);try {UpdateResponse response = client.update(updateRequest, RequestOptions.DEFAULT);return Result.suc(JSONObject.toJSON(response));} catch (IOException e) {log.info("[updateRecord]",e);return Result.fail(e);}
}

3.7、删除文档

@ApiOperation(value = "deleteRecord", notes = "deleteRecord")
@GetMapping("deleteRecord/{indexName}")
public Result deleteRecord(@RequestParam String id, @PathVariable String indexName) {DeleteRequest deleteRequest = new DeleteRequest(indexName);deleteRequest.id(id);try {DeleteResponse response = client.delete(deleteRequest, RequestOptions.DEFAULT);return Result.suc(JSONObject.toJSON(response));} catch (IOException e) {log.info("[updateRecord]",e);return Result.fail(e);}
}

3.8、查询文档

ApiOperation(value = "queryRecord", notes = "queryRecord")
@GetMapping("/queryRecord/{indexName}")
public Result queryRecord(@PathVariable String indexName) {SearchRequest searchRequest = new SearchRequest(indexName);SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();//如果用name直接查询,其实是匹配name分词过后的索引查到的记录(倒排索引);如果用name.keyword查询则是不分词的查询,正常查询到的记录RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("birthday").from("1991-01-01").to("2010-10-10").format("yyyy-MM-dd");//范围查询
//        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name.keyword", name);//精准查询PrefixQueryBuilder prefixQueryBuilder = QueryBuilders.prefixQuery("name.keyword", "张");//前缀查询
//        WildcardQueryBuilder wildcardQueryBuilder = QueryBuilders.wildcardQuery("name.keyword", "*三");//通配符查询
//        FuzzyQueryBuilder fuzzyQueryBuilder = QueryBuilders.fuzzyQuery("name", "三");//模糊查询FieldSortBuilder fieldSortBuilder = SortBuilders.fieldSort("age");//按照年龄排序fieldSortBuilder.sortMode(SortMode.MIN);//从小到大排序BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();boolQueryBuilder.must(rangeQueryBuilder).should(prefixQueryBuilder);//and or  查询sourceBuilder.query(boolQueryBuilder).sort(fieldSortBuilder);//多条件查询sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));searchRequest.source(sourceBuilder);try {SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);SearchHits hits = response.getHits();JSONArray jsonArray = new JSONArray();for (SearchHit hit : hits) {String sourceAsString = hit.getSourceAsString();JSONObject jsonObject = JSON.parseObject(sourceAsString);jsonArray.add(jsonObject);}return Result.suc();} catch (IOException e) {e.printStackTrace();return Result.suc();}
}

3.9、查询文档

通过封装QueryForm对象实现查询

@ApiOperation(value = "queryCondition", notes = "queryCondition")
@PostMapping("/queryCondition/{indexName}")
public Result queryCondition(@PathVariable String indexName,@RequestBody QueryForm form) {try {SearchRequest searchRequest = new SearchRequest(indexName);searchRequest.source(form.searchSourceBuilder());SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);SearchHits hits = response.getHits();JSONArray jsonArray = new JSONArray();for (SearchHit hit : hits) {String sourceAsString = hit.getSourceAsString();JSONObject jsonObject = JSON.parseObject(sourceAsString);jsonArray.add(jsonObject);}return Result.suc(jsonArray);} catch (IOException e) {return Result.suc();}
}@Data
static class QueryForm {private Integer id;private String age;private String name;private String address;private String birthday;private String birthdayRange;private String ageRange;SearchSourceBuilder searchSourceBuilder(){SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();if(id != null){sourceBuilder.query(QueryBuilders.termQuery("id", id));}if(name != null){sourceBuilder.query(QueryBuilders.termQuery("name", id));}if(age != null){sourceBuilder.query(QueryBuilders.termQuery("age", id));}if(address != null){sourceBuilder.query(QueryBuilders.termQuery("address", id));}if(birthday != null){sourceBuilder.query(QueryBuilders.termQuery("birthday", id));}if(birthdayRange != null){String[] array = birthdayRange.split("-");sourceBuilder.query(QueryBuilders.boolQuery().must(QueryBuilders.rangeQuery("birthday").from(array[0]).to(array[1]).format("yyyy-MM-dd")));}if(ageRange != null){String[] array = ageRange.split("-");sourceBuilder.query(QueryBuilders.boolQuery().must(QueryBuilders.rangeQuery("age").from(array[0]).to(array[1])));}return sourceBuilder;}
}

3.10、源码

源码地址:

https://gitee.com/suze/springBoot/blob/master/src/main/java/org/suze/springboot/elasticsearch/controller/ElasticSearchController.java

源码下载:

git clone https://gitee.com/suze/springBoot

下面的是我的公众号二维码图片,欢迎关注。
秋夜无霜

这篇关于ElasticSearch7.3.2-RestHighLevelClient实战的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/449530

相关文章

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis

MyBatis 动态 SQL 优化之标签的实战与技巧(常见用法)

《MyBatis动态SQL优化之标签的实战与技巧(常见用法)》本文通过详细的示例和实际应用场景,介绍了如何有效利用这些标签来优化MyBatis配置,提升开发效率,确保SQL的高效执行和安全性,感... 目录动态SQL详解一、动态SQL的核心概念1.1 什么是动态SQL?1.2 动态SQL的优点1.3 动态S

Pandas使用SQLite3实战

《Pandas使用SQLite3实战》本文主要介绍了Pandas使用SQLite3实战,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录1 环境准备2 从 SQLite3VlfrWQzgt 读取数据到 DataFrame基础用法:读

Python实战之屏幕录制功能的实现

《Python实战之屏幕录制功能的实现》屏幕录制,即屏幕捕获,是指将计算机屏幕上的活动记录下来,生成视频文件,本文主要为大家介绍了如何使用Python实现这一功能,希望对大家有所帮助... 目录屏幕录制原理图像捕获音频捕获编码压缩输出保存完整的屏幕录制工具高级功能实时预览增加水印多平台支持屏幕录制原理屏幕

最新Spring Security实战教程之Spring Security安全框架指南

《最新SpringSecurity实战教程之SpringSecurity安全框架指南》SpringSecurity是Spring生态系统中的核心组件,提供认证、授权和防护机制,以保护应用免受各种安... 目录前言什么是Spring Security?同类框架对比Spring Security典型应用场景传统

最新Spring Security实战教程之表单登录定制到处理逻辑的深度改造(最新推荐)

《最新SpringSecurity实战教程之表单登录定制到处理逻辑的深度改造(最新推荐)》本章节介绍了如何通过SpringSecurity实现从配置自定义登录页面、表单登录处理逻辑的配置,并简单模拟... 目录前言改造准备开始登录页改造自定义用户名密码登陆成功失败跳转问题自定义登出前后端分离适配方案结语前言

OpenManus本地部署实战亲测有效完全免费(最新推荐)

《OpenManus本地部署实战亲测有效完全免费(最新推荐)》文章介绍了如何在本地部署OpenManus大语言模型,包括环境搭建、LLM编程接口配置和测试步骤,本文给大家讲解的非常详细,感兴趣的朋友一... 目录1.概况2.环境搭建2.1安装miniconda或者anaconda2.2 LLM编程接口配置2

基于Canvas的Html5多时区动态时钟实战代码

《基于Canvas的Html5多时区动态时钟实战代码》:本文主要介绍了如何使用Canvas在HTML5上实现一个多时区动态时钟的web展示,通过Canvas的API,可以绘制出6个不同城市的时钟,并且这些时钟可以动态转动,每个时钟上都会标注出对应的24小时制时间,详细内容请阅读本文,希望能对你有所帮助...

Spring AI与DeepSeek实战一之快速打造智能对话应用

《SpringAI与DeepSeek实战一之快速打造智能对话应用》本文详细介绍了如何通过SpringAI框架集成DeepSeek大模型,实现普通对话和流式对话功能,步骤包括申请API-KEY、项目搭... 目录一、概述二、申请DeepSeek的API-KEY三、项目搭建3.1. 开发环境要求3.2. mav

Python与DeepSeek的深度融合实战

《Python与DeepSeek的深度融合实战》Python作为最受欢迎的编程语言之一,以其简洁易读的语法、丰富的库和广泛的应用场景,成为了无数开发者的首选,而DeepSeek,作为人工智能领域的新星... 目录一、python与DeepSeek的结合优势二、模型训练1. 数据准备2. 模型架构与参数设置3