步骤:
1、修改pom文件
将
<!--ES--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency>
替换为
<!--OPENSEARCH--> <dependency> <groupId>org.opensearch.client</groupId> <artifactId>opensearch-rest-high-level-client</artifactId> <version>2.6.0</version> </dependency>
2、修改config配置文件,将elasticsearch 的相关类替换为opensearch
将引入的类改为opensearch
import org.opensearch.client.RestClient; import org.opensearch.client.RestHighLevelClient; @Bean(value = "RestHighLevelClient", destroyMethod = "close") public RestHighLevelClient restHighLevelClient(){ //解析hostlist配置信息 String[] split = hostlist.split(","); //创建HttpHost数组,其中存放es主机和端口的配置信息 HttpHost[] httpHostArray = new HttpHost[split.length]; for(int i=0;i<split.length;i++){ String item = split[i]; httpHostArray[i] = new HttpHost(item.split(":")[0], Integer.parseInt(item.split(":")[1]), "http"); } //创建RestHighLevelClient客户端 return new RestHighLevelClient(RestClient.builder(httpHostArray).setHttpClientConfigCallback(httpClientBuilder -> { httpClientBuilder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE); BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials(userName, password)); httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); return httpClientBuilder; })); }
3、代码中操作 opensearch的接口基本不需要改动,只需要将引入的类改为opensearch即可文章来源:https://www.toymoban.com/news/detail-764144.html
import org.opensearch.action.search.SearchRequest; import org.opensearch.action.search.SearchResponse; import org.opensearch.client.RequestOptions; import org.opensearch.client.RestHighLevelClient; import org.opensearch.index.query.BoolQueryBuilder; import org.opensearch.index.query.QueryBuilders;
4、使用OpenSearch 需要jdk11 ,否则编译会报错文章来源地址https://www.toymoban.com/news/detail-764144.html
到了这里,关于springboot项目 用OpenSearch 替换 ElasticSearch的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!