2024年最全电商项目前台搜索服务es实现

}


### 2.定义商品feignClient



/**

  • projectName: b2c-cloud-store

  • description:商品客户端
    */
    @FeignClient(value = “product-service”)
    public interface ProductClient {

    /**

    • 商品全部数据调用
    • @return
      */
      @GetMapping(“/product/list”)
      List list();
      }

### 3.准备商品索引创建DSL语句!


将商品名称,标题以及详细描述添加到all字段,直接使用all字段搜索即可。



删除索引结构

DELETE /product

创建商品索引!

根据多列搜索性能较差,组成成一列搜索提高性能

PUT /product
{
“mappings”: {
“properties”: {
“productId”:{
“type”: “integer”
},
“productName”:{
“type”: “text”,
“analyzer”: “ik_smart”,
“copy_to”: “all”
},
“categoryId”:{
“type”: “integer”
},
“productTitle”:{
“type”: “text”,
“analyzer”: “ik_smart”,
“copy_to”: “all”
},
“productIntro”:{
“type”:“text”,
“analyzer”: “ik_smart”,
“copy_to”: “all”
},
“productPicture”:{
“type”: “keyword”,
“index”: false
},
“productPrice”:{
“type”: “double”,
“index”: true
},
“productSellingPrice”:{
“type”: “double”
},
“productNum”:{
“type”: “integer”
},
“productSales”:{
“type”: “integer”
},
“all”:{
“type”: “text”,
“analyzer”: “ik_max_word”
}
}
}
}

#查询索引
GET /product/_mapping

#全部查询

GET /product/_search
{
“query”: {
“match_all”: {

  }

}
}

#关键字查询

GET /product/_search
{
“query”: {
“match”: {
“all”: “最好”
}
}

}

关键字 和 添加分页

GET /product/_search
{
“query”: {
“match”: {
“all”: “最好”
}
},
“from”: 0,
“size”: 1

}

添加数据

POST /product/_doc/1
{
“productId”:1,
“productName”:“雷碧”,
“productTitle”:“最好的碳酸饮料”,
“categoryId”:1,
“productIntro”:“硫酸+煤炭制品最好的产品!”,
“productPicture”:“http://www.baidu.com”,
“productPrice”:10.5,
“productSellingPrice”:6.0,
“productNum”:10,
“productSales”:5
}

删除数据

DELETE /product/_doc/1


以上添加到es中,使用kibana添加索引即可。


###  3.准备商品doc模型


添加商品Doc实体类,定义all字段,重新构造方法,传入product参数,注意Product类中加@JsonIgnoreProperties(ignoreUnknown = true)。



@Data
@NoArgsConstructor
public class ProductDoc extends Product {

/**
 * 用于模糊查询字段,由商品名,标题和描述组成
 */
private String all;

public ProductDoc(Product product) {
    super(product.getProductId(),product.getProductName(),
            product.getCategoryId(),product.getProductTitle(),
            product.getProductIntro(),product.getProductPicture(),
            product.getProductPrice(),product.getProductSellingPrice(),
            product.getProductNum(),product.getProductSales());
    this.all = product.getProductName()+product.getProductTitle()+product.getProductIntro();
}

}


### 4.导入数据,添加配置类


mq序列化方式选择json,构建es客户端对象,并纳入容器管理



/**

  • projectName: b2c-cloud-store

  • description: 消息队列配置
    */
    @Configuration
    public class SearchConfiguration {

    /**

    • mq序列化方式,选择json!

    • @return
      */
      @Bean
      public MessageConverter messageConverter(){

      return new Jackson2JsonMessageConverter();
      }

    /**

    • es客户端添加到ioc容器

    • @return
      */
      @Bean
      public RestHighLevelClient restHighLevelClient(){
      RestHighLevelClient client =
      new RestHighLevelClient(
      RestClient.builder(HttpHost.create(“http://自己的es服务地址:9200”)));

      return client;
      }
      }


### 5.修改启动类



@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
@EnableFeignClients(clients = ProductClient.class)
public class SearchApplication {

public static void main(String[] args) {
    SpringApplication.run(SearchApplication.class,args);
}

}


### 6.程序启动监控


监控程序启动,初始化es数据。


初始化逻辑:在启动时就执行商品查询,然后判断是否存在es索引,用getindexrequest获取,对象传入索引名称。如果不存在索引就创建索引,然后删除全部数据,然后重新批量插入数据。


es入门常见的用法可以参照这篇博客:[http://t.csdnimg.cn/QsBAm]( )



@Component
@Slf4j
public class SpringBootListener implements ApplicationRunner {

/**
 * 在此方法完成es数据的同步
 * 1.判断es中的product索引是否存在
 * 2.如果不存在,java代码创建一个
 * 3.如果存在,删除原来的数据
 * 4.查询商品全部数据
 * 5.进行es库的更新工作(插入)
 * @param args
 * @throws Exception
 */
@Autowired
private RestHighLevelClient restHighLevelClient;
@Autowired
private ProductClient productClient;

private String indexStr="{\\n\" +\n" +
        "            \"  \\\"mappings\\\": {\\n\" +\n" +
        "            \"    \\\"properties\\\": {\\n\" +\n" +
        "            \"      \\\"productId\\\":{\\n\" +\n" +
        "            \"        \\\"type\\\": \\\"integer\\\"\\n\" +\n" +
        "            \"      },\\n\" +\n" +
        "            \"      \\\"productName\\\":{\\n\" +\n" +
        "            \"        \\\"type\\\": \\\"text\\\",\\n\" +\n" +
        "            \"        \\\"analyzer\\\": \\\"ik_smart\\\",\\n\" +\n" +
        "            \"        \\\"copy_to\\\": \\\"all\\\"\\n\" +\n" +
        "            \"      },\\n\" +\n" +
        "            \"      \\\"categoryId\\\":{\\n\" +\n" +
        "            \"        \\\"type\\\": \\\"integer\\\"\\n\" +\n" +
        "            \"      },\\n\" +\n" +
        "            \"      \\\"productTitle\\\":{\\n\" +\n" +
        "            \"        \\\"type\\\": \\\"text\\\",\\n\" +\n" +
        "            \"        \\\"analyzer\\\": \\\"ik_smart\\\",\\n\" +\n" +
        "            \"        \\\"copy_to\\\": \\\"all\\\"\\n\" +\n" +
        "            \"      },\\n\" +\n" +
        "            \"      \\\"productIntro\\\":{\\n\" +\n" +
        "            \"        \\\"type\\\":\\\"text\\\",\\n\" +\n" +
        "            \"        \\\"analyzer\\\": \\\"ik_smart\\\",\\n\" +\n" +
        "            \"        \\\"copy_to\\\": \\\"all\\\"\\n\" +\n" +
        "            \"      },\\n\" +\n" +
        "            \"      \\\"productPicture\\\":{\\n\" +\n" +
        "            \"        \\\"type\\\": \\\"keyword\\\",\\n\" +\n" +
        "            \"        \\\"index\\\": false\\n\" +\n" +
        "            \"      },\\n\" +\n" +
        "            \"      \\\"productPrice\\\":{\\n\" +\n" +
        "            \"        \\\"type\\\": \\\"double\\\",\\n\" +\n" +
        "            \"        \\\"index\\\": true\\n\" +\n" +
        "            \"      },\\n\" +\n" +
        "            \"      \\\"productSellingPrice\\\":{\\n\" +\n" +
        "            \"        \\\"type\\\": \\\"double\\\"\\n\" +\n" +
        "            \"      },\\n\" +\n" +
        "            \"      \\\"productNum\\\":{\\n\" +\n" +
        "            \"        \\\"type\\\": \\\"integer\\\"\\n\" +\n" +
        "            \"      },\\n\" +\n" +
        "            \"      \\\"productSales\\\":{\\n\" +\n" +
        "            \"        \\\"type\\\": \\\"integer\\\"\\n\" +\n" +
        "            \"      },\\n\" +\n" +
        "            \"      \\\"all\\\":{\\n\" +\n" +
        "            \"        \\\"type\\\": \\\"text\\\",\\n\" +\n" +
        "            \"        \\\"analyzer\\\": \\\"ik_max_word\\\"\\n\" +\n" +
        "            \"      }\\n\" +\n" +
        "            \"    }\\n\" +\n" +
        "            \"  }\\n\" +\n" +
        "            \"}";
@Override
public void run(ApplicationArguments args) throws Exception {
    GetIndexRequest getIndexRequest=new GetIndexRequest("product");
    //判断是否存在
    boolean exists = restHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
    //存在查询删除
    if(exists){
        DeleteByQueryRequest deleteByQueryRequest=new DeleteByQueryRequest("product");
        deleteByQueryRequest.setQuery(QueryBuilders.matchAllQuery());
        restHighLevelClient.deleteByQuery(deleteByQueryRequest,RequestOptions.DEFAULT);
    }else{
        //不存在创建索引
        CreateIndexRequest createIndexRequest=new CreateIndexRequest("product");
        createIndexRequest.source(indexStr, XContentType.JSON);
        restHighLevelClient.indices().create(createIndexRequest,RequestOptions.DEFAULT);
    }
    //查询全部数据
    List<Product> productList=productClient.allList();

    //遍历批量数据插入
    BulkRequest bulkRequest=new BulkRequest();
    ObjectMapper objectMapper=new ObjectMapper();
    for (Product product : productList) {
        ProductDoc productDoc=new ProductDoc(product);
    //插入数据的作用
        IndexRequest indexRequest=new IndexRequest("product").id(product.getProductId().toString());
//productDoc转成JSON放入
        String json = objectMapper.writeValueAsString(productDoc);
        indexRequest.source(json,XContentType.JSON);
        bulkRequest.add(indexRequest);
    }

    restHighLevelClient.bulk(bulkRequest,RequestOptions.DEFAULT);
}

}


以上完成es的数据同步操作。


## 三、商品搜索服务



![img](https://img-blog.csdnimg.cn/img_convert/2258f9557a3d33a7f92bf3e26e8fcc9f.png)
![img](https://img-blog.csdnimg.cn/img_convert/c76a9726b0ea5589e298157fc326c83f.png)
![img](https://img-blog.csdnimg.cn/img_convert/e7d0578c06391986b890f90c9386687f.png)

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**

完成es的数据同步操作。


## 三、商品搜索服务



[外链图片转存中...(img-USesDF4A-1714678959357)]
[外链图片转存中...(img-6DVMhWbx-1714678959357)]
[外链图片转存中...(img-aSEqwXcb-1714678959357)]

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**

  • 11
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值