升级Spring Cloud Hoxton & Alibaba,认证授权改用Oauth2。
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package com.macro.mall.search.config;
|
||||
|
||||
import com.macro.mall.common.config.BaseSwaggerConfig;
|
||||
import com.macro.mall.common.domain.SwaggerProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import springfox.documentation.builders.ApiInfoBuilder;
|
||||
@@ -16,23 +18,17 @@ import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
||||
*/
|
||||
@Configuration
|
||||
@EnableSwagger2
|
||||
public class Swagger2Config {
|
||||
@Bean
|
||||
public Docket createRestApi(){
|
||||
return new Docket(DocumentationType.SWAGGER_2)
|
||||
.apiInfo(apiInfo())
|
||||
.select()
|
||||
.apis(RequestHandlerSelectors.basePackage("com.macro.mall.search.controller"))
|
||||
.paths(PathSelectors.any())
|
||||
.build();
|
||||
}
|
||||
public class SwaggerConfig extends BaseSwaggerConfig {
|
||||
|
||||
private ApiInfo apiInfo() {
|
||||
return new ApiInfoBuilder()
|
||||
@Override
|
||||
public SwaggerProperties swaggerProperties() {
|
||||
return SwaggerProperties.builder()
|
||||
.apiBasePackage("com.macro.mall.search.controller")
|
||||
.title("mall搜索系统")
|
||||
.description("mall搜索模块")
|
||||
.contact("macro")
|
||||
.description("mall搜索相关接口文档")
|
||||
.contactName("macro")
|
||||
.version("1.0")
|
||||
.enableSecurity(false)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,6 @@ import com.macro.mall.search.domain.EsProduct;
|
||||
import com.macro.mall.search.domain.EsProductRelatedInfo;
|
||||
import com.macro.mall.search.repository.EsProductRepository;
|
||||
import com.macro.mall.search.service.EsProductService;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.common.lucene.search.function.FunctionScoreQuery;
|
||||
import org.elasticsearch.index.query.BoolQueryBuilder;
|
||||
import org.elasticsearch.index.query.QueryBuilders;
|
||||
@@ -14,10 +13,10 @@ import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
|
||||
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
|
||||
import org.elasticsearch.search.aggregations.Aggregation;
|
||||
import org.elasticsearch.search.aggregations.AggregationBuilders;
|
||||
import org.elasticsearch.search.aggregations.bucket.filter.InternalFilter;
|
||||
import org.elasticsearch.search.aggregations.bucket.nested.InternalNested;
|
||||
import org.elasticsearch.search.aggregations.bucket.terms.LongTerms;
|
||||
import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
|
||||
import org.elasticsearch.search.aggregations.bucket.filter.ParsedFilter;
|
||||
import org.elasticsearch.search.aggregations.bucket.nested.ParsedNested;
|
||||
import org.elasticsearch.search.aggregations.bucket.terms.ParsedLongTerms;
|
||||
import org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms;
|
||||
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
|
||||
import org.elasticsearch.search.sort.SortBuilders;
|
||||
import org.elasticsearch.search.sort.SortOrder;
|
||||
@@ -28,7 +27,10 @@ import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
|
||||
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
|
||||
import org.springframework.data.elasticsearch.core.SearchHit;
|
||||
import org.springframework.data.elasticsearch.core.SearchHits;
|
||||
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
|
||||
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
|
||||
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -39,6 +41,7 @@ import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
||||
/**
|
||||
@@ -53,7 +56,7 @@ public class EsProductServiceImpl implements EsProductService {
|
||||
@Autowired
|
||||
private EsProductRepository productRepository;
|
||||
@Autowired
|
||||
private ElasticsearchTemplate elasticsearchTemplate;
|
||||
private ElasticsearchRestTemplate elasticsearchRestTemplate;
|
||||
@Override
|
||||
public int importAll() {
|
||||
List<EsProduct> esProductList = productDao.getAllEsProductList(null);
|
||||
@@ -157,7 +160,12 @@ public class EsProductServiceImpl implements EsProductService {
|
||||
nativeSearchQueryBuilder.withSort(SortBuilders.scoreSort().order(SortOrder.DESC));
|
||||
NativeSearchQuery searchQuery = nativeSearchQueryBuilder.build();
|
||||
LOGGER.info("DSL:{}", searchQuery.getQuery().toString());
|
||||
return productRepository.search(searchQuery);
|
||||
SearchHits<EsProduct> searchHits = elasticsearchRestTemplate.search(searchQuery, EsProduct.class);
|
||||
if(searchHits.getTotalHits()<=0){
|
||||
return new PageImpl<>(null,pageable,0);
|
||||
}
|
||||
List<EsProduct> searchProductList = searchHits.stream().map(SearchHit::getContent).collect(Collectors.toList());
|
||||
return new PageImpl<>(searchProductList,pageable,searchHits.getTotalHits());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -178,20 +186,30 @@ public class EsProductServiceImpl implements EsProductService {
|
||||
filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery("keywords", keyword),
|
||||
ScoreFunctionBuilders.weightFactorFunction(2)));
|
||||
filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery("brandId", brandId),
|
||||
ScoreFunctionBuilders.weightFactorFunction(10)));
|
||||
ScoreFunctionBuilders.weightFactorFunction(5)));
|
||||
filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery("productCategoryId", productCategoryId),
|
||||
ScoreFunctionBuilders.weightFactorFunction(6)));
|
||||
ScoreFunctionBuilders.weightFactorFunction(3)));
|
||||
FunctionScoreQueryBuilder.FilterFunctionBuilder[] builders = new FunctionScoreQueryBuilder.FilterFunctionBuilder[filterFunctionBuilders.size()];
|
||||
filterFunctionBuilders.toArray(builders);
|
||||
FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(builders)
|
||||
.scoreMode(FunctionScoreQuery.ScoreMode.SUM)
|
||||
.setMinScore(2);
|
||||
//用于过滤掉相同的商品
|
||||
BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
|
||||
boolQueryBuilder.mustNot(QueryBuilders.termQuery("id",id));
|
||||
//构建查询条件
|
||||
NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
|
||||
builder.withQuery(functionScoreQueryBuilder);
|
||||
builder.withFilter(boolQueryBuilder);
|
||||
builder.withPageable(pageable);
|
||||
NativeSearchQuery searchQuery = builder.build();
|
||||
LOGGER.info("DSL:{}", searchQuery.getQuery().toString());
|
||||
return productRepository.search(searchQuery);
|
||||
SearchHits<EsProduct> searchHits = elasticsearchRestTemplate.search(searchQuery, EsProduct.class);
|
||||
if(searchHits.getTotalHits()<=0){
|
||||
return new PageImpl<>(null,pageable,0);
|
||||
}
|
||||
List<EsProduct> searchProductList = searchHits.stream().map(SearchHit::getContent).collect(Collectors.toList());
|
||||
return new PageImpl<>(searchProductList,pageable,searchHits.getTotalHits());
|
||||
}
|
||||
return new PageImpl<>(null);
|
||||
}
|
||||
@@ -220,16 +238,14 @@ public class EsProductServiceImpl implements EsProductService {
|
||||
.field("attrValueList.name"))));
|
||||
builder.addAggregation(aggregationBuilder);
|
||||
NativeSearchQuery searchQuery = builder.build();
|
||||
return elasticsearchTemplate.query(searchQuery, response -> {
|
||||
LOGGER.info("DSL:{}",searchQuery.getQuery().toString());
|
||||
return convertProductRelatedInfo(response);
|
||||
});
|
||||
SearchHits<EsProduct> searchHits = elasticsearchRestTemplate.search(searchQuery, EsProduct.class);
|
||||
return convertProductRelatedInfo(searchHits);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将返回结果转换为对象
|
||||
*/
|
||||
private EsProductRelatedInfo convertProductRelatedInfo(SearchResponse response) {
|
||||
private EsProductRelatedInfo convertProductRelatedInfo(SearchHits<EsProduct> response) {
|
||||
EsProductRelatedInfo productRelatedInfo = new EsProductRelatedInfo();
|
||||
Map<String, Aggregation> aggregationMap = response.getAggregations().getAsMap();
|
||||
//设置品牌
|
||||
@@ -248,14 +264,14 @@ public class EsProductServiceImpl implements EsProductService {
|
||||
productRelatedInfo.setProductCategoryNames(productCategoryNameList);
|
||||
//设置参数
|
||||
Aggregation productAttrs = aggregationMap.get("allAttrValues");
|
||||
List<LongTerms.Bucket> attrIds = ((LongTerms) ((InternalFilter) ((InternalNested) productAttrs).getProperty("productAttrs")).getProperty("attrIds")).getBuckets();
|
||||
List<? extends Terms.Bucket> attrIds = ((ParsedLongTerms) ((ParsedFilter) ((ParsedNested) productAttrs).getAggregations().get("productAttrs")).getAggregations().get("attrIds")).getBuckets();
|
||||
List<EsProductRelatedInfo.ProductAttr> attrList = new ArrayList<>();
|
||||
for (Terms.Bucket attrId : attrIds) {
|
||||
EsProductRelatedInfo.ProductAttr attr = new EsProductRelatedInfo.ProductAttr();
|
||||
attr.setAttrId((Long) attrId.getKey());
|
||||
List<String> attrValueList = new ArrayList<>();
|
||||
List<StringTerms.Bucket> attrValues = ((StringTerms) attrId.getAggregations().get("attrValues")).getBuckets();
|
||||
List<StringTerms.Bucket> attrNames = ((StringTerms) attrId.getAggregations().get("attrNames")).getBuckets();
|
||||
List<? extends Terms.Bucket> attrValues = ((ParsedStringTerms) attrId.getAggregations().get("attrValues")).getBuckets();
|
||||
List<? extends Terms.Bucket> attrNames = ((ParsedStringTerms) attrId.getAggregations().get("attrNames")).getBuckets();
|
||||
for (Terms.Bucket attrValue : attrValues) {
|
||||
attrValueList.add(attrValue.getKeyAsString());
|
||||
}
|
||||
|
||||
@@ -18,8 +18,9 @@ spring:
|
||||
elasticsearch:
|
||||
repositories:
|
||||
enabled: true
|
||||
cluster-nodes: 127.0.0.1:9300
|
||||
cluster-name: elasticsearch
|
||||
elasticsearch:
|
||||
rest:
|
||||
uris: 127.0.0.1:9200
|
||||
mybatis:
|
||||
mapper-locations:
|
||||
- classpath:dao/*.xml
|
||||
|
||||
@@ -1,13 +1,8 @@
|
||||
spring:
|
||||
cloud:
|
||||
config:
|
||||
profile: dev #启用环境名称
|
||||
label: master #分支名称
|
||||
name: search #配置文件名称
|
||||
nacos:
|
||||
discovery:
|
||||
enabled: true
|
||||
service-id: mall-config
|
||||
eureka:
|
||||
client:
|
||||
service-url:
|
||||
defaultZone: http://localhost:8001/eureka/
|
||||
server-addr: http://localhost:8848
|
||||
config:
|
||||
server-addr: http://localhost:8848
|
||||
file-extension: yaml
|
||||
@@ -1,15 +1,8 @@
|
||||
spring:
|
||||
cloud:
|
||||
config:
|
||||
profile: prod #启用环境名称
|
||||
label: master #分支名称
|
||||
name: search #配置文件名称
|
||||
nacos:
|
||||
discovery:
|
||||
enabled: true
|
||||
service-id: mall-config
|
||||
eureka:
|
||||
client:
|
||||
service-url:
|
||||
defaultZone: http://mall-registry:8001/eureka/
|
||||
instance:
|
||||
prefer-ip-address: true
|
||||
server-addr: http://nacos-registry:8848
|
||||
config:
|
||||
server-addr: http://nacos-registry:8848
|
||||
file-extension: yaml
|
||||
@@ -2,14 +2,13 @@ package com.macro.mall.search;
|
||||
|
||||
import com.macro.mall.search.dao.EsProductDao;
|
||||
import com.macro.mall.search.domain.EsProduct;
|
||||
import com.macro.mall.search.repository.EsProductRepository;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
|
||||
import org.springframework.data.elasticsearch.core.ResultsExtractor;
|
||||
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
|
||||
import org.springframework.data.elasticsearch.core.IndexOperations;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.List;
|
||||
@@ -21,7 +20,7 @@ public class MallSearchApplicationTests {
|
||||
@Autowired
|
||||
private EsProductDao productDao;
|
||||
@Autowired
|
||||
private ElasticsearchTemplate elasticsearchTemplate;
|
||||
private ElasticsearchRestTemplate elasticsearchTemplate;
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
@@ -32,8 +31,9 @@ public class MallSearchApplicationTests {
|
||||
}
|
||||
@Test
|
||||
public void testEsProductMapping(){
|
||||
elasticsearchTemplate.putMapping(EsProduct.class);
|
||||
Map mapping = elasticsearchTemplate.getMapping(EsProduct.class);
|
||||
IndexOperations indexOperations = elasticsearchTemplate.indexOps(EsProduct.class);
|
||||
indexOperations.putMapping(indexOperations.createMapping(EsProduct.class));
|
||||
Map mapping = indexOperations.getMapping();
|
||||
System.out.println(mapping);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user