提交 b0768b85 编写于 作者: zlt2000's avatar zlt2000

升级spring-data-elasticsearch到4.1.3

上级 dc4b6e21
...@@ -47,7 +47,7 @@ ...@@ -47,7 +47,7 @@
<spring-social-security.version>1.1.6.RELEASE</spring-social-security.version> <spring-social-security.version>1.1.6.RELEASE</spring-social-security.version>
<commons-io.version>2.6</commons-io.version> <commons-io.version>2.6</commons-io.version>
<servlet-api.version>4.0.1</servlet-api.version> <servlet-api.version>4.0.1</servlet-api.version>
<spring-data-elasticsearch.version>3.2.10.RELEASE</spring-data-elasticsearch.version> <spring-data-elasticsearch.version>4.1.3</spring-data-elasticsearch.version>
<elasticsearch.version>7.10.2</elasticsearch.version> <elasticsearch.version>7.10.2</elasticsearch.version>
<knife4j.version>2.0.5</knife4j.version> <knife4j.version>2.0.5</knife4j.version>
<hibernate-validator.version>6.2.0.Final</hibernate-validator.version> <hibernate-validator.version>6.2.0.Final</hibernate-validator.version>
......
...@@ -8,6 +8,7 @@ import com.central.common.utils.JsonUtil; ...@@ -8,6 +8,7 @@ import com.central.common.utils.JsonUtil;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest; import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse; import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest; import org.elasticsearch.client.indices.GetIndexRequest;
...@@ -21,7 +22,6 @@ import org.elasticsearch.cluster.metadata.AliasMetadata; ...@@ -21,7 +22,6 @@ import org.elasticsearch.cluster.metadata.AliasMetadata;
import org.elasticsearch.cluster.metadata.MappingMetadata; import org.elasticsearch.cluster.metadata.MappingMetadata;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.IOException; import java.io.IOException;
...@@ -40,10 +40,10 @@ import java.util.*; ...@@ -40,10 +40,10 @@ import java.util.*;
public class IndexServiceImpl implements IIndexService { public class IndexServiceImpl implements IIndexService {
private ObjectMapper mapper = new ObjectMapper(); private ObjectMapper mapper = new ObjectMapper();
private final ElasticsearchRestTemplate elasticsearchRestTemplate; private final RestHighLevelClient client;
public IndexServiceImpl(ElasticsearchRestTemplate elasticsearchRestTemplate) { public IndexServiceImpl(RestHighLevelClient client) {
this.elasticsearchRestTemplate = elasticsearchRestTemplate; this.client = client;
} }
@Override @Override
...@@ -57,7 +57,7 @@ public class IndexServiceImpl implements IIndexService { ...@@ -57,7 +57,7 @@ public class IndexServiceImpl implements IIndexService {
//mappings //mappings
request.mapping(indexDto.getMappingsSource(), XContentType.JSON); request.mapping(indexDto.getMappingsSource(), XContentType.JSON);
} }
CreateIndexResponse response = elasticsearchRestTemplate.getClient() CreateIndexResponse response = client
.indices() .indices()
.create(request, RequestOptions.DEFAULT); .create(request, RequestOptions.DEFAULT);
return response.isAcknowledged(); return response.isAcknowledged();
...@@ -66,7 +66,7 @@ public class IndexServiceImpl implements IIndexService { ...@@ -66,7 +66,7 @@ public class IndexServiceImpl implements IIndexService {
@Override @Override
public boolean delete(String indexName) throws IOException { public boolean delete(String indexName) throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest(indexName); DeleteIndexRequest request = new DeleteIndexRequest(indexName);
AcknowledgedResponse response = elasticsearchRestTemplate.getClient().indices().delete(request, RequestOptions.DEFAULT); AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);
return response.isAcknowledged(); return response.isAcknowledged();
} }
...@@ -75,7 +75,7 @@ public class IndexServiceImpl implements IIndexService { ...@@ -75,7 +75,7 @@ public class IndexServiceImpl implements IIndexService {
if (StrUtil.isNotEmpty(queryStr)) { if (StrUtil.isNotEmpty(queryStr)) {
indices = queryStr; indices = queryStr;
} }
Response response = elasticsearchRestTemplate.getClient().getLowLevelClient() Response response = client.getLowLevelClient()
.performRequest(new Request( .performRequest(new Request(
"GET", "GET",
"/_cat/indices?h=health,status,index,docsCount,docsDeleted,storeSize&s=cds:desc&format=json&index="+StrUtil.nullToEmpty(indices) "/_cat/indices?h=health,status,index,docsCount,docsDeleted,storeSize&s=cds:desc&format=json&index="+StrUtil.nullToEmpty(indices)
...@@ -93,7 +93,7 @@ public class IndexServiceImpl implements IIndexService { ...@@ -93,7 +93,7 @@ public class IndexServiceImpl implements IIndexService {
@Override @Override
public Map<String, Object> show(String indexName) throws IOException { public Map<String, Object> show(String indexName) throws IOException {
GetIndexRequest request = new GetIndexRequest(indexName); GetIndexRequest request = new GetIndexRequest(indexName);
GetIndexResponse getIndexResponse = elasticsearchRestTemplate.getClient() GetIndexResponse getIndexResponse = client
.indices().get(request, RequestOptions.DEFAULT); .indices().get(request, RequestOptions.DEFAULT);
MappingMetadata mappingMetadata = getIndexResponse.getMappings().get(indexName); MappingMetadata mappingMetadata = getIndexResponse.getMappings().get(indexName);
Map<String, Object> mappOpenMap = mappingMetadata.getSourceAsMap(); Map<String, Object> mappOpenMap = mappingMetadata.getSourceAsMap();
......
...@@ -16,7 +16,6 @@ import org.elasticsearch.search.aggregations.bucket.range.Range; ...@@ -16,7 +16,6 @@ import org.elasticsearch.search.aggregations.bucket.range.Range;
import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.metrics.ParsedCardinality; import org.elasticsearch.search.aggregations.metrics.ParsedCardinality;
import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.IOException; import java.io.IOException;
...@@ -41,10 +40,10 @@ import java.util.Map; ...@@ -41,10 +40,10 @@ import java.util.Map;
*/ */
@Service @Service
public class AggregationServiceImpl implements IAggregationService { public class AggregationServiceImpl implements IAggregationService {
private final ElasticsearchRestTemplate elasticsearchRestTemplate; private final RestHighLevelClient client;
public AggregationServiceImpl(ElasticsearchRestTemplate elasticsearchRestTemplate) { public AggregationServiceImpl(RestHighLevelClient client) {
this.elasticsearchRestTemplate = elasticsearchRestTemplate; this.client = client;
} }
/** /**
...@@ -219,7 +218,6 @@ public class AggregationServiceImpl implements IAggregationService { ...@@ -219,7 +218,6 @@ public class AggregationServiceImpl implements IAggregationService {
) )
).size(0); ).size(0);
RestHighLevelClient client = elasticsearchRestTemplate.getClient();
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
Aggregations aggregations = response.getAggregations(); Aggregations aggregations = response.getAggregations();
Map<String, Object> result = new HashMap<>(15); Map<String, Object> result = new HashMap<>(15);
......
...@@ -5,6 +5,7 @@ import com.central.es.utils.SearchBuilder; ...@@ -5,6 +5,7 @@ import com.central.es.utils.SearchBuilder;
import com.central.search.model.SearchDto; import com.central.search.model.SearchDto;
import com.central.search.service.ISearchService; import com.central.search.service.ISearchService;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.search.sort.SortOrder;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -19,10 +20,10 @@ import java.io.IOException; ...@@ -19,10 +20,10 @@ import java.io.IOException;
*/ */
@Service @Service
public class SearchServiceImpl implements ISearchService { public class SearchServiceImpl implements ISearchService {
private final ElasticsearchRestTemplate elasticsearchRestTemplate; private final RestHighLevelClient client;
public SearchServiceImpl(ElasticsearchRestTemplate elasticsearchRestTemplate) { public SearchServiceImpl(RestHighLevelClient client) {
this.elasticsearchRestTemplate = elasticsearchRestTemplate; this.client = client;
} }
/** /**
...@@ -33,7 +34,7 @@ public class SearchServiceImpl implements ISearchService { ...@@ -33,7 +34,7 @@ public class SearchServiceImpl implements ISearchService {
*/ */
@Override @Override
public PageResult<JsonNode> strQuery(String indexName, SearchDto searchDto) throws IOException { public PageResult<JsonNode> strQuery(String indexName, SearchDto searchDto) throws IOException {
return SearchBuilder.builder(elasticsearchRestTemplate, indexName) return SearchBuilder.builder(client, indexName)
.setStringQuery(searchDto.getQueryStr()) .setStringQuery(searchDto.getQueryStr())
.addSort(searchDto.getSortCol(), SortOrder.DESC) .addSort(searchDto.getSortCol(), SortOrder.DESC)
.setIsHighlight(searchDto.getIsHighlighter()) .setIsHighlight(searchDto.getIsHighlighter())
......
...@@ -22,8 +22,7 @@ import org.elasticsearch.search.builder.SearchSourceBuilder; ...@@ -22,8 +22,7 @@ import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField; import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.search.sort.SortOrder;
import org.springframework.data.elasticsearch.ElasticsearchException; import org.springframework.data.elasticsearch.UncategorizedElasticsearchException;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
...@@ -65,26 +64,24 @@ public class SearchBuilder { ...@@ -65,26 +64,24 @@ public class SearchBuilder {
/** /**
* 生成SearchBuilder实例 * 生成SearchBuilder实例
* @param elasticsearchTemplate * @param client
* @param indexName * @param indexName
*/ */
public static SearchBuilder builder(ElasticsearchRestTemplate elasticsearchTemplate, String indexName) { public static SearchBuilder builder(RestHighLevelClient client, String indexName) {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
SearchRequest searchRequest = new SearchRequest(indexName); SearchRequest searchRequest = new SearchRequest(indexName);
searchRequest.source(searchSourceBuilder); searchRequest.source(searchSourceBuilder);
RestHighLevelClient client = elasticsearchTemplate.getClient();
return new SearchBuilder(searchRequest, searchSourceBuilder, client); return new SearchBuilder(searchRequest, searchSourceBuilder, client);
} }
/** /**
* 生成SearchBuilder实例 * 生成SearchBuilder实例
* @param elasticsearchTemplate * @param client
*/ */
public static SearchBuilder builder(ElasticsearchRestTemplate elasticsearchTemplate) { public static SearchBuilder builder(RestHighLevelClient client) {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
SearchRequest searchRequest = new SearchRequest(); SearchRequest searchRequest = new SearchRequest();
searchRequest.source(searchSourceBuilder); searchRequest.source(searchSourceBuilder);
RestHighLevelClient client = elasticsearchTemplate.getClient();
return new SearchBuilder(searchRequest, searchSourceBuilder, client); return new SearchBuilder(searchRequest, searchSourceBuilder, client);
} }
...@@ -266,7 +263,7 @@ public class SearchBuilder { ...@@ -266,7 +263,7 @@ public class SearchBuilder {
} }
} }
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) { } catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
throw new ElasticsearchException("failed to set highlighted value for field: " + field.getName() throw new UncategorizedElasticsearchException("failed to set highlighted value for field: " + field.getName()
+ " with value: " + Arrays.toString(field.getFragments()), e); + " with value: " + Arrays.toString(field.getFragments()), e);
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册