Skip to content

Commit

Permalink
升级spring-data-elasticsearch到4.1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
zlt2000 committed Feb 3, 2021
1 parent dc4b6e2 commit b0768b8
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 27 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<spring-social-security.version>1.1.6.RELEASE</spring-social-security.version>
<commons-io.version>2.6</commons-io.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>
<knife4j.version>2.0.5</knife4j.version>
<hibernate-validator.version>6.2.0.Final</hibernate-validator.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
Expand All @@ -21,7 +22,6 @@
import org.elasticsearch.cluster.metadata.MappingMetadata;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Service;

import java.io.IOException;
Expand All @@ -40,10 +40,10 @@
public class IndexServiceImpl implements IIndexService {
private ObjectMapper mapper = new ObjectMapper();

private final ElasticsearchRestTemplate elasticsearchRestTemplate;
private final RestHighLevelClient client;

public IndexServiceImpl(ElasticsearchRestTemplate elasticsearchRestTemplate) {
this.elasticsearchRestTemplate = elasticsearchRestTemplate;
public IndexServiceImpl(RestHighLevelClient client) {
this.client = client;
}

@Override
Expand All @@ -57,7 +57,7 @@ public boolean create(IndexDto indexDto) throws IOException {
//mappings
request.mapping(indexDto.getMappingsSource(), XContentType.JSON);
}
CreateIndexResponse response = elasticsearchRestTemplate.getClient()
CreateIndexResponse response = client
.indices()
.create(request, RequestOptions.DEFAULT);
return response.isAcknowledged();
Expand All @@ -66,7 +66,7 @@ public boolean create(IndexDto indexDto) throws IOException {
@Override
public boolean delete(String indexName) throws IOException {
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();
}

Expand All @@ -75,7 +75,7 @@ public PageResult<Map<String, String>> list(String queryStr, String indices) thr
if (StrUtil.isNotEmpty(queryStr)) {
indices = queryStr;
}
Response response = elasticsearchRestTemplate.getClient().getLowLevelClient()
Response response = client.getLowLevelClient()
.performRequest(new Request(
"GET",
"/_cat/indices?h=health,status,index,docsCount,docsDeleted,storeSize&s=cds:desc&format=json&index="+StrUtil.nullToEmpty(indices)
Expand All @@ -93,7 +93,7 @@ public PageResult<Map<String, String>> list(String queryStr, String indices) thr
@Override
public Map<String, Object> show(String indexName) throws IOException {
GetIndexRequest request = new GetIndexRequest(indexName);
GetIndexResponse getIndexResponse = elasticsearchRestTemplate.getClient()
GetIndexResponse getIndexResponse = client
.indices().get(request, RequestOptions.DEFAULT);
MappingMetadata mappingMetadata = getIndexResponse.getMappings().get(indexName);
Map<String, Object> mappOpenMap = mappingMetadata.getSourceAsMap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.metrics.ParsedCardinality;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Service;

import java.io.IOException;
Expand All @@ -41,10 +40,10 @@
*/
@Service
public class AggregationServiceImpl implements IAggregationService {
private final ElasticsearchRestTemplate elasticsearchRestTemplate;
private final RestHighLevelClient client;

public AggregationServiceImpl(ElasticsearchRestTemplate elasticsearchRestTemplate) {
this.elasticsearchRestTemplate = elasticsearchRestTemplate;
public AggregationServiceImpl(RestHighLevelClient client) {
this.client = client;
}

/**
Expand Down Expand Up @@ -219,7 +218,6 @@ public Map<String, Object> requestStatAgg(String indexName, String routing) thro
)
).size(0);

RestHighLevelClient client = elasticsearchRestTemplate.getClient();
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
Aggregations aggregations = response.getAggregations();
Map<String, Object> result = new HashMap<>(15);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.central.search.model.SearchDto;
import com.central.search.service.ISearchService;
import com.fasterxml.jackson.databind.JsonNode;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Service;
Expand All @@ -19,10 +20,10 @@
*/
@Service
public class SearchServiceImpl implements ISearchService {
private final ElasticsearchRestTemplate elasticsearchRestTemplate;
private final RestHighLevelClient client;

public SearchServiceImpl(ElasticsearchRestTemplate elasticsearchRestTemplate) {
this.elasticsearchRestTemplate = elasticsearchRestTemplate;
public SearchServiceImpl(RestHighLevelClient client) {
this.client = client;
}

/**
Expand All @@ -33,7 +34,7 @@ public SearchServiceImpl(ElasticsearchRestTemplate elasticsearchRestTemplate) {
*/
@Override
public PageResult<JsonNode> strQuery(String indexName, SearchDto searchDto) throws IOException {
return SearchBuilder.builder(elasticsearchRestTemplate, indexName)
return SearchBuilder.builder(client, indexName)
.setStringQuery(searchDto.getQueryStr())
.addSort(searchDto.getSortCol(), SortOrder.DESC)
.setIsHighlight(searchDto.getIsHighlighter())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.data.elasticsearch.ElasticsearchException;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.UncategorizedElasticsearchException;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -65,26 +64,24 @@ private SearchBuilder(SearchRequest searchRequest, SearchSourceBuilder searchBui

/**
* 生成SearchBuilder实例
* @param elasticsearchTemplate
* @param client
* @param indexName
*/
public static SearchBuilder builder(ElasticsearchRestTemplate elasticsearchTemplate, String indexName) {
public static SearchBuilder builder(RestHighLevelClient client, String indexName) {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
SearchRequest searchRequest = new SearchRequest(indexName);
searchRequest.source(searchSourceBuilder);
RestHighLevelClient client = elasticsearchTemplate.getClient();
return new SearchBuilder(searchRequest, searchSourceBuilder, client);
}

/**
* 生成SearchBuilder实例
* @param elasticsearchTemplate
* @param client
*/
public static SearchBuilder builder(ElasticsearchRestTemplate elasticsearchTemplate) {
public static SearchBuilder builder(RestHighLevelClient client) {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
SearchRequest searchRequest = new SearchRequest();
searchRequest.source(searchSourceBuilder);
RestHighLevelClient client = elasticsearchTemplate.getClient();
return new SearchBuilder(searchRequest, searchSourceBuilder, client);
}

Expand Down Expand Up @@ -266,7 +263,7 @@ private <T> void populateHighLightedFields(T result, Map<String, HighlightField>
}
}
} 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);
}
}
Expand Down

0 comments on commit b0768b8

Please sign in to comment.