问题
在完成黑马的leyou商城中,在13天的视频中对规格参数进行聚合查询并封装数据时,后台出现这个错误信息:
java.lang.ClassCastException: org.elasticsearch.search.aggregations.bucket.terms.UnmappedTerms cannot be cast to org.elasticsearch.search.aggregations.bucket.terms.StringTerms
看到这个cast to就知道格式不能转换成StringTerms,并且错误代码在getParamAggResult方法中,如图:
问题原因
至于问题原因出在哪,我是参考的这篇文章:
问题解决
然后根据博主提供的信息,最后还是解决了,所以把我的操作步骤写一下。
步骤1:删除elasticsearch中的goods引索
- 开启kibana Server在网页端一般都是http://localhost:5601/,然后执行
DELETE /goods - 或者使用PostMan:
切记一定要先删除索引,再进行下一步重新渲染数据到ElasticSearch中!
查看该索引是否删除成功:
步骤2:修改SearchService中代码
找到buildGoods方法,进行如下修改:
步骤3:在elasticsearch中添加goods引索,和导入数据
@Test
public void ReLoadData(){
elasticsearchTemplate.createIndex(Goods.class);
elasticsearchTemplate.putMapping(Goods.class);
Integer page = 1;
Integer rows = 100;
do {
PageResult<SpuBo> result = goodsClient.querySpuByPage("", null, page, rows);
List<SpuBo> items = result.getItems();
List<Goods> goods = items.stream().map(spuBo -> {
try {
return searchService.buildGoods(spuBo);
} catch (JsonProcessingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}).collect(Collectors.toList());
goodsRepository.saveAll(goods);
rows = items.size();
page++;
}while (rows == 100);
}
最后,重启ES微服务和Zull网关微服务,再去重新访问前端项目,如果有缓存,可以重启前端项目!
注意:
buildGoods方法中,不要忘了下面这行代码: