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

2022-12-30 19:47:38 浏览数 (1)

快速上手:导入删除查询 引入依赖:

代码语言:javascript复制
  <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
                </dependency>
1234

控制层:

代码语言:javascript复制
 @Autowired
        private EsProductService esProductService;
     
        @ApiOperation(value = "简单搜索:根据关键字,品牌名称或者产品名称,产品编号,副标题搜索(字符串:Text类型最大拆分)")
        @RequestMapping(value = "/search/keyword", method = RequestMethod.GET)
        @ResponseBody
        public CommonResult<CommonPage<EsProduct>> searchKeyword(@RequestParam(required = false) String keyword,
                                                                @RequestParam(required = false, defaultValue = "0") Integer pageNum,
                                                                @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
            Page<EsProduct> 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<Object> deleteAll(String indexName) {
            int i = esProductService.deleteAll(indexName);
            return CommonResult.success(i);
        }
     
        @ApiOperation(value = "导入所有产品信息数据库中商品到ES")
        @RequestMapping(value = "/importAll", method = RequestMethod.GET)
        @ResponseBody
        public CommonResult<Integer> importAllList() {
            int count = esProductService.importAll();
            return CommonResult.success(count);
        }
123456789101112131415161718192021222324252627282930

service接口:

代码语言:javascript复制
    public interface EsProductService {
        /**
         * 从数据库中导入所有商品到ES
         */
        int importAll();
        /**
         * 根据关键字,品牌名称或者产品名称搜索(字符串:Text类型最大拆分)
         * @param keyword
         * @param pageNum
         * @param pageSize
         * @return
         */
        Page<EsProduct> searchKeyword(String keyword, Integer pageNum, Integer pageSize);
     
     
        /**
         * 删除索引库
         * @return
         */
        int deleteAll(String indexName);
    }
12345678910111213141516171819202122

业务实现类:

代码语言:javascript复制
  @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<EsProduct> esProductList = productDao.getAllEsProductList(null);
            Iterable<EsProduct> esProductIterable = productRepository.saveAll(esProductList);
            Iterator<EsProduct> iterator = esProductIterable.iterator();
            int result = 0;
            while (iterator.hasNext()) {
                result  ;
                iterator.next();
            }
            return result;
        }
     
        /**
         * 根据关键字,品牌名称或者产品名称,产品编号搜索(字符串:Text类型最大拆分)
1234567891011121314151617181920212223242526

更多内容请见原文,原文转载自:https://blog.csdn.net/weixin_44519496/article/details/120482353

0 人点赞