如何在SpringBoot中使用Elasticsearch7.2.0
本篇文章给大家分享的是有关如何在SpringBoot中使用Elasticsearch7.2.0,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
成都创新互联公司专注于永修网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供永修营销型网站建设,永修网站制作、永修网页设计、永修网站官网定制、成都微信小程序服务,打造永修网络公司原创品牌,更为您提供永修网站排名全网营销落地服务。
引入依赖:
org.elasticsearch elasticsearch ${elasticsearch.version} org.elasticsearch.client elasticsearch-rest-client ${elasticsearch.version} org.elasticsearch.client elasticsearch-rest-high-level-client ${elasticsearch.version}
application.yml配置:
elasticsearch: ip: 192.168.52.132:9200
客户端连接配置类
package com.dc.elastic.configuration; import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpHost; import org.apache.http.client.config.RequestConfig; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClientBuilder; import org.elasticsearch.client.RestHighLevelClient; import org.springframework.beans.factory.annotation.Autowired; 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.Objects; /** * 描述: 连接客户端 * * @author leo * @create 2019-08-25 13:22 */ @Configuration public class ElasticsearchRestClient { /** * 超时时间设为5分钟 */ private static final int TIME_OUT = 5 * 60 * 1000; private static final int ADDRESS_LENGTH = 2; private static final String HTTP_SCHEME = "http"; @Value("${elasticsearch.ip}") String[] ipAddress; @Bean public RestClientBuilder restClientBuilder() { System.err.println(ipAddress); HttpHost[] hosts = Arrays.stream(ipAddress) .map(this::makeHttpHost) .filter(Objects::nonNull) .toArray(HttpHost[]::new); return RestClient.builder(hosts); } @Bean(name = "highLevelClient") public RestHighLevelClient highLevelClient(@Autowired RestClientBuilder restClientBuilder) { restClientBuilder.setRequestConfigCallback( new RestClientBuilder.RequestConfigCallback() { @Override public RequestConfig.Builder customizeRequestConfig( RequestConfig.Builder requestConfigBuilder) { return requestConfigBuilder.setSocketTimeout(TIME_OUT); } }); //TODO 此处可以进行其它操作 return new RestHighLevelClient(restClientBuilder); } private HttpHost makeHttpHost(String s) { assert StringUtils.isNotEmpty(s); String[] address = s.split(":"); if (address.length == ADDRESS_LENGTH) { String ip = address[0]; int port = Integer.parseInt(address[1]); System.err.println(ip+"+"+port); return new HttpHost(ip, port, HTTP_SCHEME); } else { return null; } } }
测试controller
@RequestMapping("/test") public void test(@RequestParam String keyword) { Integer pageIndex = 1; Integer pageSize = 5; String indexName = "vw_ods"; Mapdata = new HashMap<>(); data.put("clearacctname", keyword); List
以上就是如何在SpringBoot中使用Elasticsearch7.2.0,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。
分享题目:如何在SpringBoot中使用Elasticsearch7.2.0
转载来于:http://scyanting.com/article/pdosdi.html