SpringBoot整合Elasticsearch7.2.0的实现方法
程序员文章站
2022-07-12 08:29:44
spring boot 2.1.x整合elasticsearch最新版的一处问题
新版本的spring boot 2的spring-boot-starter-data-e...
spring boot 2.1.x整合elasticsearch最新版的一处问题
新版本的spring boot 2的spring-boot-starter-data-elasticsearch中支持的elasticsearch版本是2.x,但elasticsearch实际上已经发展到7.2.x版本了,为了更好的使用elasticsearch的新特性,所以弃用了spring-boot-starter-data-elasticsearch依赖,而改为直接使用spring-data-elasticsearch,以便启用对新版本支持,目前的版本对应关系如下
elasticsearch(es)有两种连接方式:transport、rest。transport通过tcp方式访问es(只支持java),rest方式通过http api 访问es(没有语言限制)。
es官方建议使用rest方式, transport 在7.0版本中不建议使用,在8.x的版本中废弃。
引入依赖:
<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); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇: PHP常用函数之格式化时间操作示例