Mall第五天

tech2025-08-30  5

一.整合Es

1spring-boot中star的es版本可能会不一样需要进行改成和下载的es一样的版本

<properties> <java.version>1.8</java.version> <elasticsearch.version>7.4.2</elasticsearch.version> </properties> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.4.2</version> </dependency>

2.项目检索服务

PUT product { "mappings": { "properties": { "skuId":{ "type": "long" }, "spuId":{ "type": "keyword" }, "skuTitle":{ "type": "text", "analyzer": "ik_smart" }, "skuPrice":{ "type": "keyword" }, "skuImg":{ "type": "keyword", "index": false, "doc_values": false }, "saleCount":{ "type": "long" }, "hasStock":{ "type": "boolean" }, "hotScore":{ "type": "long" }, "brandId":{ "type": "long" }, "catalogId":{ "type": "long" }, "brandName":{ "type": "keyword", "index": false, "doc_values": false }, "brandImg":{ "type": "keyword", "index": false, //可以查询,但不能用来做检索 冗余存储 "doc_values": false }, "catalogName":{ "type": "keyword", "index": false, "doc_values": false }, "attrs":{ //所有属性的规格 "type": "nested", //嵌入式检索:数组类型扁平化处理 "properties": { "attrId":{ "type":"long" }, "attrName":{ "type":"keyword", "index": false, "doc_values": false }, "attrValue":{ "type": "keyword" } } } } } } @Override public Boolean productStatusUp(List<SkuEsModel> skuEsModelList) throws IOException { //保存到es //1.给es建立索引 product 建立好映射关系 //2.给es保存数据 BulkRequest bulkRequest = new BulkRequest(); for (SkuEsModel model : skuEsModelList) { //1.构造保存请求 IndexRequest indexRequest = new IndexRequest(EsConstant.PRODUCT_INDEX); indexRequest.id(model.getSkuId().toString()); String s = JSON.toJSONString(model); indexRequest.source(s, XContentType.JSON); bulkRequest.add(indexRequest); } BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, GulimallElasticSearchConfig.COMMON_OPTIONS); //todo 如果批量错误 boolean b = bulk.hasFailures(); List<String> collect = Arrays.stream(bulk.getItems()).map(item -> { return item.getId(); }).collect(Collectors.toList()); log.error("商品上完成:{}", collect); return b; }

3.中文解析ik放到ningx中

/mydata/nginx/html/es/fengci.txt vi /mydata/elasticsearch/plugins/ik/config/IKAnalyzer.cfg.xml <!--用户可以在这里配置远程扩展字典 --> <entry key="remote_ext_dict">http://192.168.35.110/es/fenci.txt</entry>

 

最新回复(0)