迁移搜索服务
This commit is contained in:
@@ -1 +1,4 @@
|
||||
/**
|
||||
* 占位,避免包折叠
|
||||
*/
|
||||
package cn.iocoder.mall.searchservice.enums;
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package cn.iocoder.mall.searchservice.enums.product;
|
||||
|
||||
/**
|
||||
* 搜索商品条件的字段枚举
|
||||
*/
|
||||
public enum SearchProductConditionFieldEnum {
|
||||
|
||||
CATEGORY("category");
|
||||
|
||||
/**
|
||||
* 字段
|
||||
*/
|
||||
private final String field;
|
||||
|
||||
SearchProductConditionFieldEnum(String field) {
|
||||
this.field = field;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1 +1,4 @@
|
||||
/**
|
||||
* 占位,避免包折叠
|
||||
*/
|
||||
package cn.iocoder.mall.searchservice.rpc;
|
||||
|
||||
@@ -57,6 +57,13 @@
|
||||
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Test 相关 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- 工具类相关 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
||||
@@ -11,6 +11,8 @@ import org.mapstruct.Mapping;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
import org.springframework.data.domain.Page;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface SearchProductConvert {
|
||||
|
||||
@@ -30,8 +32,11 @@ public interface SearchProductConvert {
|
||||
|
||||
ESProductDO convert(SearchProductSaveBO bean);
|
||||
|
||||
@Mapping(source = "content", target = "list")
|
||||
@Mapping(source = "getTotalElements", target = "total")
|
||||
PageResult<SearchProductBO> convert(Page<ESProductDO> searchPage);
|
||||
List<SearchProductBO> convertList(List<ESProductDO> list);
|
||||
|
||||
default PageResult<SearchProductBO> convertPage(Page<ESProductDO> searchPage) {
|
||||
return new PageResult<SearchProductBO>().setList(convertList(searchPage.getContent()))
|
||||
.setTotal(searchPage.getTotalElements());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -24,6 +24,8 @@ import java.util.List;
|
||||
@Slf4j
|
||||
public class SearchProductManager {
|
||||
|
||||
private static final Integer REBUILD_FETCH_PER_SIZE = 100;
|
||||
|
||||
@DubboReference(version = "${dubbo.consumer.ProductSpuRpc.version}")
|
||||
private ProductSpuRpc productSpuRpc;
|
||||
@DubboReference(version = "${dubbo.consumer.ProductSkuRpc.version}")
|
||||
@@ -37,6 +39,40 @@ public class SearchProductManager {
|
||||
@Autowired
|
||||
private SearchProductService searchProductService;
|
||||
|
||||
/**
|
||||
* 重建所有商品的 ES 索引
|
||||
*
|
||||
* @return 重建数量
|
||||
*/
|
||||
public Integer rebuild() {
|
||||
// TODO 芋艿,因为目前商品比较少,所以写的很粗暴。等未来重构
|
||||
Integer lastId = null;
|
||||
int rebuildCounts = 0;
|
||||
while (true) {
|
||||
// 从商品服务,增量获取商品列表编号
|
||||
CommonResult<List<Integer>> listProductSpuIdsResult = productSpuRpc.listProductSpuIds(lastId, REBUILD_FETCH_PER_SIZE);
|
||||
listProductSpuIdsResult.checkError();
|
||||
List<Integer> spuIds = listProductSpuIdsResult.getData();
|
||||
// 逐个重建索引到 ES 中
|
||||
spuIds.forEach(this::saveProduct);
|
||||
// 设置新的 lastId ,或者结束
|
||||
rebuildCounts += listProductSpuIdsResult.getData().size();
|
||||
if (spuIds.size() < REBUILD_FETCH_PER_SIZE) {
|
||||
break;
|
||||
} else {
|
||||
lastId = spuIds.get(spuIds.size() - 1);
|
||||
}
|
||||
}
|
||||
// 返回成功
|
||||
return rebuildCounts;
|
||||
}
|
||||
|
||||
/**
|
||||
* 重建指定商品的 ES 索引
|
||||
*
|
||||
* @param id 商品 SPU 编号
|
||||
* @return 是否重建成功
|
||||
*/
|
||||
public Boolean saveProduct(Integer id) {
|
||||
// 获得商品 SPU
|
||||
CommonResult<ProductSpuRespDTO> productSpuResult = productSpuRpc.getProductSpu(id);
|
||||
|
||||
@@ -8,12 +8,15 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* 商品更新 Topic 的消费者,重建对应的商品的 ES 索引
|
||||
*/
|
||||
@Service
|
||||
@RocketMQMessageListener(
|
||||
topic = ProductUpdateMessage.TOPIC,
|
||||
consumerGroup = "${spring.application.name}-consumer-group-" + ProductUpdateMessage.TOPIC
|
||||
)
|
||||
public class PayTransactionPaySuccessConsumer implements RocketMQListener<ProductUpdateMessage> {
|
||||
public class ProductUpdateConsumer implements RocketMQListener<ProductUpdateMessage> {
|
||||
|
||||
@Autowired
|
||||
private SearchProductManager productSearchManager;
|
||||
@@ -1,4 +1,4 @@
|
||||
/**
|
||||
* 占位
|
||||
* 占位,避免包折叠
|
||||
*/
|
||||
package cn.iocoder.mall.searchservice.service;
|
||||
|
||||
@@ -7,6 +7,7 @@ import cn.iocoder.common.framework.vo.SortingField;
|
||||
import cn.iocoder.mall.searchservice.convert.product.SearchProductConvert;
|
||||
import cn.iocoder.mall.searchservice.dal.es.dataobject.ESProductDO;
|
||||
import cn.iocoder.mall.searchservice.dal.es.repository.ESProductRepository;
|
||||
import cn.iocoder.mall.searchservice.enums.product.SearchProductConditionFieldEnum;
|
||||
import cn.iocoder.mall.searchservice.enums.product.SearchProductPageQuerySortFieldEnum;
|
||||
import cn.iocoder.mall.searchservice.service.product.bo.SearchProductBO;
|
||||
import cn.iocoder.mall.searchservice.service.product.bo.SearchProductConditionBO;
|
||||
@@ -47,7 +48,7 @@ public class SearchProductService {
|
||||
Page<ESProductDO> searchPage = productRepository.search(pageQueryBO.getCid(), pageQueryBO.getKeyword(),
|
||||
pageQueryBO.getPageNo(), pageQueryBO.getPageSize(), pageQueryBO.getSorts());
|
||||
// 转换结果
|
||||
return SearchProductConvert.INSTANCE.convert(searchPage);
|
||||
return SearchProductConvert.INSTANCE.convertPage(searchPage);
|
||||
}
|
||||
|
||||
private void checkSortFieldInvalid(List<SortingField> sorts) {
|
||||
@@ -74,11 +75,11 @@ public class SearchProductService {
|
||||
* 在我们搜索商品时,需要获得关键字可选择的分类、品牌等等搜索条件,方便用户进一步检索
|
||||
*
|
||||
* @param keyword 关键字
|
||||
* @param fields 需要返回的搜索条件。目前可传入的参数为
|
||||
* @param fields 需要返回的搜索条件{@link SearchProductConditionFieldEnum}。目前可传入的参数为
|
||||
* 1. category :商品分类,会返回商品分类编号
|
||||
* @return 搜索条件
|
||||
*/
|
||||
public SearchProductConditionBO getSearchCondition(String keyword, Collection<String> fields) {
|
||||
public SearchProductConditionBO getSearchProductCondition(String keyword, Collection<String> fields) {
|
||||
// 创建 ES 搜索条件
|
||||
NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
|
||||
// 筛选
|
||||
|
||||
@@ -16,3 +16,6 @@ dubbo:
|
||||
# Dubbo 服务提供者的配置
|
||||
provider:
|
||||
tag: ${DUBBO_TAG} # Dubbo 路由分组
|
||||
# Dubbo 服务消费者的配置
|
||||
consumer:
|
||||
tag: ${DUBBO_TAG} # Dubbo 路由分组
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* 占位
|
||||
*/
|
||||
package cn.iocoder.mall.searchservice.manager;
|
||||
@@ -0,0 +1,35 @@
|
||||
package cn.iocoder.mall.searchservice.manager.product;
|
||||
|
||||
import cn.iocoder.mall.searchservice.dal.es.repository.ESProductRepository;
|
||||
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.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* {@link SearchProductManager} 的测试类,目前是集成测试类
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
|
||||
public class SearchProductManagerTest {
|
||||
|
||||
static {
|
||||
System.setProperty("es.set.netty.runtime.available.processors", "false");
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private SearchProductManager searchProductManager;
|
||||
|
||||
@Autowired
|
||||
private ESProductRepository esProductRepository;
|
||||
|
||||
@Test
|
||||
public void testRebuild() {
|
||||
int counts = searchProductManager.rebuild();
|
||||
System.out.println("重建数量:" + counts);
|
||||
|
||||
System.out.println(esProductRepository.count());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* 占位
|
||||
*/
|
||||
package cn.iocoder.mall.searchservice.service;
|
||||
@@ -0,0 +1,32 @@
|
||||
package cn.iocoder.mall.searchservice.service.product;
|
||||
|
||||
import cn.iocoder.mall.searchservice.service.product.bo.SearchProductConditionBO;
|
||||
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.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* {@link SearchProductService} 的测试类,目前是集成测试类
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
|
||||
public class SearchProductServiceTest {
|
||||
|
||||
static {
|
||||
System.setProperty("es.set.netty.runtime.available.processors", "false");
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private SearchProductService searchProductService;
|
||||
|
||||
@Test
|
||||
public void testGetSearchCondition() {
|
||||
SearchProductConditionBO conditionBO = searchProductService.getSearchProductCondition("商品", Collections.singletonList("category"));
|
||||
System.out.println(conditionBO);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user