elasticsearch实现商品搜索

本文介绍了如何使用Elasticsearch实现商品搜索功能,包括数据初始化和数据同步。在数据初始化阶段,文章讲解了如何在SpringBoot应用启动时通过restHighLevelClient创建并设置索引mapping。而在数据同步部分,提到了三种方式,推荐使用通过RocketMQ进行数据变更的消息传递,并详细描述了该方案的实现步骤和优缺点。
摘要由CSDN通过智能技术生成

elasticsearch实现商品搜索

安装es
安装rocketmq

数据初始化

在项目启动时对未初始化的索引进行数据的初始化。
使用restHighLevelClient操作es,注意使用高版本es,restHighLevelClient对低版本es支持不好。
在项目启动时使用springboot的application做数据初始化
实例化客户端

 //创建低级客户端 ip,端口,请求方式http或https
        RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost(hostName, Integer.parseInt(port), scheme));
        //创建高级客户端
        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder);

判断索引是否存在

		 //判断商品索引是否存在
        boolean zeastProductExists;
        try {
   
            GetIndexRequest zeastProductExistsRequest = new GetIndexRequest();
            zeastProductExistsRequest.indices(zeastProductIndex);
            zeastProductExists = restHighLevelClient.indices().exists(zeastProductExistsRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
   
            logger.error("ES索引判断异常!",e);
            throw new RuntimeException("ES索引判断异常!");
        }

索引不存在时进行索引初始化
创建索引

 CreateIndexRequest createIndexRequest = new CreateIndexRequest(zeastProductIndex);

            //创建索引
            CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);

            boolean createIndexResult = createIndexResponse.isAcknowledged();
            if(!createIndexResult){
   
                logger.error("ES商品索引初始化失败!");
                return CommonResult.failed("ES商品索引初始化失败!");
            }
            boolean shardsCreateIndexResult = createIndexResponse.isShardsAcknowledged();
            if(!shardsCreateIndexResult){
   
                logger.error("ES商品索引初始化失败!");
                return CommonResult.failed("ES商品索引初始化失败!");
            }

索引创建完成后设置索引mapping
mapping设置

			 PutMappingRequest putMappingRequest = new PutMappingRequest(zeastProductIndex);
            //设置type
            putMappingRequest.type(zeastProductType);
            
            Map<String, Object> jsonMap = new HashMap<>();
            Map<String, Object> properties = new HashMap<>();
            
            Map<String, Object> productId = new HashMap<>();
            productId.put("type", "keyword");
            properties.put("productId", productId);

            Map<String, Object> productName = new HashMap<>();
            productName.put("type", "keyword");
            properties.put("productName", productName);
            //keyword不分词 text分词
            Map<String, Object> keyword = new HashMap<>();
            keyword.put("type", "text");
            properties.put("keyword", keyword);

            Map<String, Object> mapping = new HashMap<>();
            mapping.put("properties", properties);

            jsonMap.put(zeastProductType, mapping);

            putMappingRequest.source(jsonMap);

            putMappingRequest.timeout(TimeValue.timeValueMinutes(2));
            putMappingRequest.masterNodeTimeout(TimeValue.timeValueMinutes(1));

            AcknowledgedResponse putMappingResponse = restHighLevelClient.indices().putMapping(putMappingRequest, RequestOptions.DEFAULT);
            boolean mappingResult = putMappingResponse.isAcknowledged();
            if(!mappingResult){
   
                logger.error("ES商品映射初始化失败!");
                return CommonResult.failed("ES商品映射初始化失败!");
            }

mapping设置完成后开始全量数据初始化
数据初始化
查询数据部分省略,初始化代码大概如下,数据组装多开了几个线程同时跑。

 BulkRequest request = new BulkRequest();
            //在任务执行完成后解析返回值,
            for(Future<List<SearchProductDo>> future :futures){
   
                try {
   
                    List
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值