ElasticSearch之JAVA API简单应用
发布日期:2021-05-08 09:46:40 浏览次数:0 分类:技术文章

Java高级REST客户端

添加maven坐标

<dependency>	<groupId>org.elasticsearch.client</groupId> 	<artifactId>elasticsearch-rest-high-level-client</artifactId></dependency>
package com.search.server;import java.io.IOException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.concurrent.TimeUnit;import org.apache.http.HttpHost;import org.elasticsearch.action.search.SearchRequest;import org.elasticsearch.action.search.SearchResponse;import org.elasticsearch.client.RequestOptions;import org.elasticsearch.client.RestClient;import org.elasticsearch.client.RestHighLevelClient;import org.elasticsearch.common.unit.TimeValue;import org.elasticsearch.index.query.QueryBuilders;import org.elasticsearch.search.SearchHit;import org.elasticsearch.search.SearchHits;import org.elasticsearch.search.builder.SearchSourceBuilder;import org.elasticsearch.search.sort.FieldSortBuilder;import org.elasticsearch.search.sort.SortOrder;import org.springframework.stereotype.Service;@Servicepublic class SearchServer {
   		public void getSmallBoxs(RestHighLevelClient client,String indexName,String type,Integer starNum, Integer endNum,Integer records) throws IOException {
   		SearchRequest searchRequest = new SearchRequest(indexName); 		searchRequest.types(type);		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); 		searchSourceBuilder.query(QueryBuilders.rangeQuery("width").from(starNum).to(endNum));		searchSourceBuilder.sort(new FieldSortBuilder("width").order(SortOrder.DESC));		searchSourceBuilder.size(records); //		String[] includeFields = new String[] {"id", "name", "width","height"};//		String[] excludeFields = new String[] {"_index","_type","_id","_score"};//		searchSourceBuilder.fetchSource(includeFields, excludeFields);		searchSourceBuilder.fetchSource(true);		searchSourceBuilder.timeout(new TimeValue(10, TimeUnit.SECONDS));		searchRequest.source(searchSourceBuilder);		SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);		SearchHits hits = searchResponse.getHits();		SearchHit[] searchHits = hits.getHits();		getBigBoxs(client, searchHits);		closeConnection(client);	}		private void getBigBoxs(RestHighLevelClient client, SearchHit[] searchHit) throws IOException {
   		List<Map> boxVos = new ArrayList<>();		for(SearchHit hit:searchHit) {
   			Map<String, Object> sourceAsMap = hit.getSourceAsMap();			SearchRequest searchRequest = new SearchRequest("bigbox"); 			searchRequest.types("bigbox");			SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); 			searchSourceBuilder.query(QueryBuilders.matchPhraseQuery("id", sourceAsMap.get("id")));			searchSourceBuilder.fetchSource(true);			searchSourceBuilder.timeout(new TimeValue(10, TimeUnit.SECONDS));			searchRequest.source(searchSourceBuilder);			SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);			SearchHits hits = searchResponse.getHits();			SearchHit[] searchHits = hits.getHits();			sourceAsMap.put("price", 0);			if(searchHits.length>0) {
   				sourceAsMap.put("price", searchHits[0].getSourceAsMap().get("price"));			}			boxVos.add(sourceAsMap);		}		for(Map map:boxVos) {
   			System.out.println(map);		}	}		public RestHighLevelClient getConnection(String ip,Integer port) {
   		try {
   			RestHighLevelClient client = new RestHighLevelClient(			        RestClient.builder(new HttpHost(ip, port, "http")));			return client;		}catch (Exception e) {
   			throw new RuntimeException("连接异常");		}	}	public void closeConnection(RestHighLevelClient client) {
   		try {
   			client.close();		}catch (Exception e) {
   			throw new RuntimeException("连接关闭异常");		}	}}
上一篇:安卓手机直接调用拍照
下一篇:SpingBoot跨域问题