您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關如何在SpringBoot中使用Elasticsearch7.2.0,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
引入依賴:
<dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>${elasticsearch.version}</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-client</artifactId> <version>${elasticsearch.version}</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>${elasticsearch.version}</version> </dependency>
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"; Map<String, Object> data = new HashMap<>(); data.put("clearacctname", keyword); List<Map<String, Object>> result = new ArrayList<Map<String, Object>>(); SearchRequest searchRequest = new SearchRequest(indexName); // searchRequest.types(indexName); queryBuilder(pageIndex, pageSize, data, indexName, searchRequest); try { SearchResponse response = highLevelClient.search(searchRequest, RequestOptions.DEFAULT); for (SearchHit hit : response.getHits().getHits()) { Map<String, Object> map = hit.getSourceAsMap(); map.put("id", hit.getId()); result.add(map); // 取高亮結果 Map<String, HighlightField> highlightFields = hit.getHighlightFields(); HighlightField highlight = highlightFields.get("clearacctname"); Text[] fragments = highlight.fragments(); // 多值的字段會有多個值 String fragmentString = fragments[0].string(); System.out.println("高亮:" + fragmentString); } System.out.println("pageIndex:" + pageIndex); System.out.println("pageSize:" + pageSize); System.out.println(response.getHits().getTotalHits()); System.out.println(result.size()); for (Map<String, Object> map : result) { System.out.println(map.get("clearacctname")); } } catch (IOException e) { e.printStackTrace(); } } private void queryBuilder(Integer pageIndex, Integer pageSize, Map<String, Object> query, String indexName, SearchRequest searchRequest) { if (query != null && !query.keySet().isEmpty()) { SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); if (pageIndex != null && pageSize != null) { searchSourceBuilder.size(pageSize); if (pageIndex <= 0) { pageIndex = 0; } searchSourceBuilder.from((pageIndex - 1) * pageSize); } BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery(); query.keySet().forEach(key -> { boolBuilder.must(QueryBuilders.matchQuery(key, query.get(key))); }); searchSourceBuilder.query(boolBuilder); HighlightBuilder highlightBuilder = new HighlightBuilder(); HighlightBuilder.Field highlightTitle = new HighlightBuilder.Field("title").preTags("<strong>").postTags("</strong>"); highlightTitle.highlighterType("unified"); highlightBuilder.field(highlightTitle); searchSourceBuilder.highlighter(highlightBuilder); SearchRequest source = searchRequest.source(searchSourceBuilder); } }
以上就是如何在SpringBoot中使用Elasticsearch7.2.0,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。