微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

全文检索工具:第一章:Spring-data-elasticSearch搜索

引入依赖:

=====

org.springframework.boot

spring-boot-starter-data-elasticsearch

控制层:

====

@Autowired

private EsProductService esProductService;

@ApiOperation(value = “简单搜索:根据关键字,品牌名称或者产品名称,产品编号,副标题搜索(字符串:Text类型最大拆分)”)

@RequestMapping(value = “/search/keyword”, method = RequestMethod.GET)

@ResponseBody

public CommonResult<CommonPage> searchKeyword(@RequestParam(required = false) String keyword,

@RequestParam(required = false, defaultValue = “0”) Integer pageNum,

@RequestParam(required = false, defaultValue = “5”) Integer pageSize) {

Page esProductPage = esProductService.searchKeyword(keyword, pageNum, pageSize);

return CommonResult.success(CommonPage.restPage(esProductPage));

}

@ApiOperation(value = “删除索引库”)

@ApiImplicitParam(name = “indexName”, value = “索引库名称”,

defaultValue = “product”, paramType = “query”, dataType = “String”)

@RequestMapping(value = “/deleteall”, method = RequestMethod.GET)

@ResponseBody

public CommonResult deleteall(String indexName) {

int i = esProductService.deleteall(indexName);

return CommonResult.success(i);

}

@ApiOperation(value = “导入所有产品信息数据库中商品到ES”)

@RequestMapping(value = “/importAll”, method = RequestMethod.GET)

@ResponseBody

public CommonResult importAllList() {

int count = esProductService.importAll();

return CommonResult.success(count);

}

service接口:

==========

public interface EsProductService {

/**

*/

int importAll();

/**

  • 根据关键字,品牌名称或者产品名称搜索(字符串:Text类型最大拆分)

  • @param keyword

  • @param pageNum

  • @param pageSize

  • @return

*/

Page searchKeyword(String keyword, Integer pageNum, Integer pageSize);

/**

*/

int deleteall(String indexName);

}

业务实现类:

======

@Service

public class EsProductServiceImpl implements EsProductService {

private static final Logger LOGGER = LoggerFactory.getLogger(EsProductServiceImpl.class);

@Autowired

private EsProductDao productDao;

@Autowired

private EsProductRepository productRepository;

@Autowired

private ElasticsearchTemplate elasticsearchTemplate;

@Override

public int importAll() {

List esProductList = productDao.getAllEsProductList(null);

Iterable esProductIterable = productRepository.saveAll(esProductList);

Iterator iterator = esProductIterable.iterator();

int result = 0;

while (iterator.hasNext()) {

result++;

iterator.next();

}

return result;

}

/**

  • 根据关键字,品牌名称或者产品名称,产品编号搜索(字符串:Text类型最大拆分)

  • @param keyword

  • @param pageNum

  • @param pageSize

  • @return

*/

@Override

public Page searchKeyword(String keyword, Integer pageNum, Integer pageSize) {

Pageable pageable = PageRequest.of(pageNum, pageSize);

return productRepository.findByKeywordsOrProductNameOrBrandNameOrProductSnorSubTitle(keyword,keyword,keyword,keyword,keyword,pageable);

}

/**

*/

@Override

public int deleteall(String indexName) {

boolean product = elasticsearchTemplate.deleteIndex(indexName);

if(product){

return 1;

}

return 0;

}

}

EsProductDao接口

==============

public interface EsProductDao {

List getAllEsProductList(@Param(“id”) Long id);

}

EsProductDao.xml

================

<?xml version="1.0" encoding="UTF-8"?>

SELECT

p.id productId,

p.product_sn productSn,

p.brand_id brandId,

p.brand_name brandName,

p.product_category_id productCategoryId,

p.name productName,

p.sale sale,

p.sub_title subTitle,

p.price price,

p.keywords keywords,

pav.id pavProductAttributeValueId,

pav.value pavProductAttributeValue,

pa.id paProductAttributeId,

pa.name paProductAttributeName,

pc.id pcProductCategoryId,

pc.name pcProductCategoryName

FROM pms_product p

LEFT JOIN pms_product_attribute_value pav ON p.id = pav.product_id

LEFT JOIN pms_product_attribute pa ON pav.product_attribute_id= pa.id

LEFT JOIN pms_product_category pc ON p.product_category_id = pc.id

WHERE delete_status = 0 AND publish_sta

《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享

tus = 1

and p.id=#{id}

EsProductRepository接口

=====================

public interface EsProductRepository extends ElasticsearchRepository<EsProduct, Long> {

/**

*/

Page findByKeywordsOrProductNameOrBrandNameOrProductSnorSubTitle(String keywords,String productName,String brandName,String productSn,String subTitle,Pageable page);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐