1、maven依赖
<dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>6.4.3</version> </dependency>
2、properties
es_host_list=127.0.0.1:9202,127.0.0.2:9202,127.0.0.3:9202
3、config配置
import org.apache.http.HttpHost; import org.apache.http.client.config.RequestConfig; import org.apache.http.impl.nio.client.HttpAsyncClientBuilder; 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 org.springframework.context.annotation.PropertySource; @Configuration @PropertySource("classpath:setting.properties") public class ElasticsearchConfig { @Value("${es_host_list}") private String hostlist; public static int CONNECT_TIMEOUT_MILLIS = 5000; public static int SOCKET_TIMEOUT_MILLIS = 30000; public static int CONNECTION_REQUEST_TIMEOUT_MILLIS = 5000; public static int MAX_CONN_PER_ROUTE = 10; public static int MAX_CONN_TOTAL = 30; @Bean public RestHighLevelClient restHighLevelClient(){ //解析hostlist配置信息 String hostlist = this.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"); } RestClientBuilder builder = RestClient.builder(httpHostArray); // 异步httpclient连接延时配置 builder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() { @Override public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder builder) { builder.setConnectTimeout(CONNECT_TIMEOUT_MILLIS); builder.setSocketTimeout(SOCKET_TIMEOUT_MILLIS); builder.setConnectionRequestTimeout(CONNECTION_REQUEST_TIMEOUT_MILLIS); return builder; } }); // 异步httpclient连接数配置 builder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { @Override public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { httpClientBuilder.setMaxConnTotal(MAX_CONN_PER_ROUTE); httpClientBuilder.setMaxConnPerRoute(MAX_CONN_TOTAL); return httpClientBuilder; } }); //创建RestHighLevelClient客户端 return new RestHighLevelClient(RestClient.builder(httpHostArray)); } //项目主要使用RestHighLevelClient,对于低级的客户端暂时不用 @Bean public RestClient restClient(){ String hostlist = this.hostlist; //解析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"); } return RestClient.builder(httpHostArray).build(); } }
4、es工具类
import com.google.common.collect.Lists; import com.sf.cms.common.SettingConstants; import com.sf.cms.address.remoteAPI.model.AddrEsVo; import com.sf.cms.address.service.CmsAddressService; import com.sf.gis.app.auth.model.Dict; import com.sf.gis.app.auth.service.DictService; import com.sf.gis.app.common.utils.JacksonUtil; import com.sf.gis.app.framework.page.PageBean; import lombok.extern.slf4j.Slf4j; import net.sf.json.JSONObject; import org.apache.commons.lang3.StringUtils; import org.elasticsearch.action.DocWriteResponse; import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.delete.DeleteRequest; import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.get.GetRequest; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.client.IndicesClient; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.query.*; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Service; import java.io.IOException; import java.util.Arrays; import java.util.List; import java.util.Map; @Service @Slf4j public class EsService { @Autowired(required = false) RestHighLevelClient restHighLevelClient; @Autowired CmsAddressService addressService; @Autowired DictService dictService; private static final String ADDRESS_INDEX = "cms-address-%s"; private static final String ADDRESS_INDEX_TYPE = "cityCode-%s"; private static final String[] ADDRESS_FILEDS = new String[]{"addressId", "address", "type"}; public void creatAddrIndex(String cityCode){ JSONObject properties = new JSONObject(); JSONObject fileds = new JSONObject(); JSONObject filed = new JSONObject(); filed.put("type", "text"); JSONObject intFiled = new JSONObject(); intFiled.put("type", "integer"); fileds.put("addressId", filed); fileds.put("address", filed); fileds.put("type", intFiled); properties.put("properties", fileds); String index = String.format(ADDRESS_INDEX, cityCode); String type = String.format(ADDRESS_INDEX_TYPE, cityCode); //创建索引请求对象 CreateIndexRequest createIndexRequest = new CreateIndexRequest(index); //设置索引参数 createIndexRequest.settings(Settings.builder().put("number_of_shards", 5).put("number_of_replicas", 0)); createIndexRequest.mapping(type, properties.toString(), XContentType.JSON); //链接客户端 IndicesClient client = restHighLevelClient.indices(); //创建响应对象 try { CreateIndexResponse response = client.create(createIndexRequest); //响应 boolean acknowledged = response.isAcknowledged(); log.info("creatAddrIndex res:{}",acknowledged); }catch (Exception e){ log.error("es crateate Index error:{}", e.getMessage(), e); } } public void delAddrIndex(String cityCode){ String index = String.format(ADDRESS_INDEX, cityCode); //创建索引请求对象 DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(index); //链接客户端 IndicesClient client = restHighLevelClient.indices(); try { //删除索引 AcknowledgedResponse delete = client.delete(deleteIndexRequest); //响应 boolean acknowledged = delete.isAcknowledged(); log.info("delAddrIndex res:{}",acknowledged); }catch (Exception e){ log.error("es delAddrIndex error:{}", e.getMessage(), e); } } public void saveAddr(String cityCode, AddrEsVo addrEsVo){ String index = String.format(ADDRESS_INDEX, cityCode); String type = String.format(ADDRESS_INDEX_TYPE, cityCode); //准备json数据 //创建索引请求对象 IndexRequest indexRequest = new IndexRequest(index, type, addrEsVo.getAddressId()); //指定索引添加文档 try { indexRequest.source(JacksonUtil.toJson(addrEsVo), XContentType.JSON); } catch (IOException e) { log.error(e.getMessage(),e); } try { //链接客户端 IndexResponse indexResponse = restHighLevelClient.index(indexRequest); //获取响应结果 DocWriteResponse.Result result = indexResponse.getResult(); log.info("saveAddr res:{}",result); }catch (Exception e){ log.error("es saveAddr error:{}", e.getMessage(), e); } } public void batchInsertAddrs(String cityCode, List<AddrEsVo> addrEsVos) { String index = String.format(ADDRESS_INDEX, cityCode); String type = String.format(ADDRESS_INDEX_TYPE, cityCode); List<AddrEsVo> tempVos = Lists.newArrayList(); log.info("begin batchInsertAddrs cityCode:{} size:{}",cityCode,addrEsVos.size()); for (int i = 1; i <= addrEsVos.size(); i++) { tempVos.add(addrEsVos.get(i - 1)); if (i % 200 == 0 || i == addrEsVos.size()) { BulkRequest bulkRequest = new BulkRequest(); IndexRequest indexRequest = null; for (AddrEsVo addrEsVo : tempVos) { indexRequest = new IndexRequest(index, type, addrEsVo.getAddressId()); try { indexRequest.source(JacksonUtil.toJson(addrEsVo), XContentType.JSON); } catch (IOException e) { log.error(e.getMessage(),e); } bulkRequest.add(indexRequest); } try { restHighLevelClient.bulk(bulkRequest); } catch (Exception e) { log.error("es batchInsertAddrs error:{}", e.getMessage(), e); } tempVos.clear(); } } log.info("end batchInsertAddrs cityCode:{} size:{}",cityCode,addrEsVos.size()); } public AddrEsVo queryAddr(String cityCode, String addressId) { String index = String.format(ADDRESS_INDEX, cityCode); String type = String.format(ADDRESS_INDEX_TYPE, cityCode); try { GetRequest getRequest = new GetRequest(index, type, addressId); GetResponse getResponse = restHighLevelClient.get(getRequest); boolean exists = getResponse.isExists(); if (exists) { Map<String, Object> sourceAsMap = getResponse.getSourceAsMap(); return resToVo(sourceAsMap); } } catch (Exception e) { log.error("es queryAddr error:{}", e.getMessage(), e); } return null; } public void delEsAddr(String cityCode, String addressId) { String index = String.format(ADDRESS_INDEX, cityCode); String type = String.format(ADDRESS_INDEX_TYPE, cityCode); List<Dict> esQueryAddr = dictService.getDictByType(SettingConstants.ES_QUERY_CITYS); List<String> esCitys = Lists.newArrayList(); if(!esQueryAddr.isEmpty()){ String esQueryCitys = esQueryAddr.get(0).getValue(); if(StringUtils.isNotBlank(esQueryCitys)){ esCitys = Arrays.asList(esQueryCitys.split(",")); } } if (esCitys.contains(cityCode)) { try { DeleteRequest getRequest = new DeleteRequest(index, type, addressId); DeleteResponse deleteResponse = restHighLevelClient.delete(getRequest); log.info("delEsAddr cityCode:{},addressId:{} status:{}", cityCode, addressId, deleteResponse.status()); } catch (Exception e) { log.error("es delEsAddr error:{}", e.getMessage(), e); } } } public List<AddrEsVo> queryAll(String cityCode) { String index = String.format(ADDRESS_INDEX, cityCode); String type = String.format(ADDRESS_INDEX_TYPE, cityCode); List<AddrEsVo> addrEsVos = Lists.newArrayList(); //搜索请求对象 SearchRequest searchRequest = new SearchRequest(index); //指定类型 searchRequest.types(type); //搜索源构建对象 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); //搜索方式 //matchAllQuery搜索全部 searchSourceBuilder.query(QueryBuilders.matchAllQuery()); //设置源字段过虑,第一个参数结果集包括哪些字段,第二个参数表示结果集不包括哪些字段 searchSourceBuilder.fetchSource(ADDRESS_FILEDS, new String[]{}); //向搜索请求对象中设置搜索源 searchRequest.source(searchSourceBuilder); try { //执行搜索,向ES发起http请求 SearchResponse searchResponse = restHighLevelClient.search(searchRequest); //搜索结果 SearchHits hits = searchResponse.getHits(); //匹配到的总记录数 long totalHits = hits.getTotalHits(); //得到匹配度高的文档 SearchHit[] searchHits = hits.getHits(); AddrEsVo addrEsVo = null; for (SearchHit hit : searchHits) { //文档的主键 String id = hit.getId(); //源文档内容 Map<String, Object> sourceAsMap = hit.getSourceAsMap(); addrEsVo = resToVo(sourceAsMap); if (addrEsVo != null) { addrEsVos.add(addrEsVo); } } } catch (Exception e) { log.error("es queryAddr error:{}", e.getMessage(), e); } return addrEsVos; } public PageBean<AddrEsVo> queryAddrByPage(String cityCode, String address, Integer addressType, Integer pageNo, Integer pageSize) { String index = String.format(ADDRESS_INDEX, cityCode); String type = String.format(ADDRESS_INDEX_TYPE, cityCode); log.info("begin queryAddrByPage cityCode:{} addr:{} addressType:{},pageNo:{}",cityCode,address,addressType,pageNo); List<AddrEsVo> addrEsVos = Lists.newArrayList(); int total = 0; try { //搜索请求对象 SearchRequest searchRequest = new SearchRequest(index); //指定类型 searchRequest.types(type); //搜索源构建对象 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); //设置分页参数 //计算出记录起始下标 int from = (pageNo - 1) * pageSize; searchSourceBuilder.from(from);//起始记录下标,从0开始 searchSourceBuilder.size(pageSize);//每页显示的记录数 BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery(); //搜索方式 MatchPhraseQueryBuilder queryBuilder = QueryBuilders.matchPhraseQuery("address", address); boolBuilder.must(queryBuilder); if (addressType != null) { TermQueryBuilder addrQueryBuilder = QueryBuilders.termQuery("type", addressType); boolBuilder.must(addrQueryBuilder); } searchSourceBuilder.query(boolBuilder); //设置源字段过虑,第一个参数结果集包括哪些字段,第二个参数表示结果集不包括哪些字段 searchSourceBuilder.fetchSource(ADDRESS_FILEDS, new String[]{}); //向搜索请求对象中设置搜索源 searchRequest.source(searchSourceBuilder); //执行搜索,向ES发起http请求 SearchResponse searchResponse = restHighLevelClient.search(searchRequest); TimeValue timeValue = searchResponse.getTook(); //搜索结果 SearchHits hits = searchResponse.getHits(); //匹配到的总记录数 total = (int)hits.getTotalHits(); //得到匹配度高的文档 SearchHit[] searchHits = hits.getHits(); AddrEsVo addrEsVo = null; for (SearchHit hit : searchHits) { //文档的主键 String id = hit.getId(); //源文档内容 Map<String, Object> sourceAsMap = hit.getSourceAsMap(); addrEsVo = resToVo(sourceAsMap); if (addrEsVo != null) { addrEsVos.add(addrEsVo); } } log.info("end queryAddrByPage cityCode:{} addr:{} addressType:{},total:{},pageNo:{},pageSize:{},time:{}",cityCode,address,addressType,total,pageNo,pageSize,timeValue.toString()); } catch (Exception e) { log.error("es queryAddrByPage error:{}", e.getMessage(), e); } return new PageBean<AddrEsVo>(pageNo, pageSize, total, addrEsVos); } private AddrEsVo resToVo(Map<String, Object> sourceAsMap) { AddrEsVo addrEsVo = null; if (sourceAsMap != null && !sourceAsMap.isEmpty()) { String addressId = String.valueOf(sourceAsMap.get("addressId")); String address = String.valueOf(sourceAsMap.get("address")); Integer type = Integer.valueOf(String.valueOf(sourceAsMap.get("type"))); addrEsVo = new AddrEsVo(); addrEsVo.setAddress(address); addrEsVo.setAddressId(addressId); addrEsVo.setType(type); } return addrEsVo; } }
5、实现类
import com.alibaba.fastjson.JSON; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import sf.myboot.service.EsService; import java.util.Map; @Controller @RequestMapping(value="es") public class EsController { @Autowired EsService esService; @GetMapping(value="getAddress") @ResponseBody public String getAddress(String cityCode,String addressId){ Map<String, Object> map=esService.queryAddr(cityCode,addressId); return JSON.toJSONString(map); } }