升级spring-data-elasticsearch到4.1.3
This commit is contained in:
@@ -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
-8
@@ -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;
|
|||||||
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.*;
|
|||||||
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 {
|
|||||||
//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 {
|
|||||||
@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 {
|
|||||||
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 {
|
|||||||
@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();
|
||||||
|
|||||||
+3
-5
@@ -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;
|
|||||||
*/
|
*/
|
||||||
@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 {
|
|||||||
)
|
)
|
||||||
).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
-4
@@ -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;
|
|||||||
*/
|
*/
|
||||||
@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 {
|
|||||||
*/
|
*/
|
||||||
@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())
|
||||||
|
|||||||
+6
-9
@@ -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 {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成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 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user