问题场景:
今天用聚合函数想查询出catename不重复列表,类似于Mysql的group by函数。但返回的确是空数组[],再三确认是有数据的。输出response:
{"took":1,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":360,"relation":"eq"},"max_score":1.0,"hits":[{"_index":"goods","_type":"_doc","_id":"552","_score":1.0,"_source":{"cateName":"狗罐头","createTime":1673165093,"description":"","fullTextSearch":"PRINTPAW宠物狗零食鸡肉绕花生饼干成犬幼犬全犬种通用训犬奖励磨牙棒 【实惠装】鸡肉绕花生饼干360g狗罐头PRINTPAW官方旗舰店","id":552,"image":"//img10.360buyimg.com/n7/jfs/t1/228575/36/11044/79383/65976d82Fb5f1b102/48af91d69659fa57.jpg","price":36.9,"shopName":"PRINTPAW官方旗舰店","stock":1000,"title":"PRINTPAW宠物狗零食鸡肉绕花生饼干成犬幼犬全犬种通用训犬奖励磨牙棒 【实惠装】鸡肉绕花生饼干360g","updateTime":1680094862,"views":0}},{"_index":"goods","_type":"_doc","_id":"584","_score":1.0,"_source":{"cateName":"狗罐头","createTime":1673165093,"description":"","fullTextSearch":"麦富迪 狗狗零食 成犬幼犬全犬种通用训犬奖励 蛋黄缠肉100g 混合口味 全阶段 国产狗罐头麦富迪官方旗舰店","id":584,"image":"//img11.360buyimg.com/n7/jfs/t1/227452/12/4395/157612/655efca3F444b9b85/06932f5d20c97662.jpg","price":15.0,"shopName":"麦富迪官方旗舰店","stock":1000,"title":"麦富迪 狗狗零食 成犬幼犬全犬种通用训犬奖励 蛋黄缠肉100g 混合口味 全阶段 国产","updateTime":1680094862,"views":0}},{"_index":"goods","_type":"_doc","_id":"614","_score":1.0,"_source":{"cateName":"钓竿","createTime":1673165093,"description":"","fullTextSearch":"爱心东东 2022新款轻奢美人鱼鱼竿6h 美人鱼钓鱼竿轻硬19调6H碳素鲤鱼竿5.4 6.3 7.2米鱼台钓竿 超硬19调6H5.4米+礼包 新人首单满200多件多折!关注小店,更有粉丝价钓竿宽逸旗舰店","id":614,"image":"//img10.360buyimg.com/n7/jfs/t1/186750/35/19688/469778/61236599E7ca37659/d6efab66c7a0ad87.jpg","price":278.0,"shopName":"宽逸旗舰店","stock":1000,"title":"爱心东东 2022新款轻奢美人鱼鱼竿6h 美人鱼钓鱼竿轻硬19调6H碳素鲤鱼竿5.4 6.3 7.2米鱼台钓竿 超硬19调6H5.4米+礼包 新人首单满200多件多折!关注小店,更有粉丝价","updateTime":1680094862,"views":0}},{"_index":"goods","_type":"_doc","_id":"604","_score":1.0,"_source":{"cateName":"钓竿","createTime":1673165093,"description":"","fullTextSearch":"特狼王鱼竿手竿4.5米5.4米6.3米7.2米钓鱼竿超轻超硬碳素台钓竿 4.5米杆稍+钩漂线 本产品为预售定制款,下单前请先联系客服咨询哦!钓竿汇斯盾彩泥店","id":604,"image":"//img10.360buyimg.com/n7/jfs/t1/139661/9/21538/211718/619f872bEed3fafb8/c5b4c0906e308c44.jpg","price":87.0,"shopName":"汇斯盾彩泥店","stock":1000,"title":"特狼王鱼竿手竿4.5米5.4米6.3米7.2米钓鱼竿超轻超硬碳素台钓竿 4.5米杆稍+钩漂线 本产品为预售定制款,下单前请先联系客服咨询哦!","updateTime":1680094862,"views":0}},{"_index":"goods","_type":"_doc","_id":"340","_score":1.0,"_source":{"cateName":"汽车音箱","createTime":1673165093,"description":"","fullTextSearch":"6.5寸大柏林之声喇叭音箱空箱体汽车DIY水桶音响改装家用书架制作 如需定制钻孔/改孔请联系客服汽车音箱豪车拆车喇叭小店","id":340,"image":"//img11.360buyimg.com/n7/jfs/t1/229538/29/7287/214554/6575620aFe254e913/2af724e95d01ab53.jpg","price":198.0,"shopName":"豪车拆车喇叭小店","stock":1000,"title":"6.5寸大柏林之声喇叭音箱空箱体汽车DIY水桶音响改装家用书架制作 如需定制钻孔/改孔请联系客服","updateTime":1680094862,"views":0}},{"_index":"goods","_type":"_doc","_id":"360","_score":1.0,"_source":{"cateName":"手机","createTime":1673165093,"description":"","fullTextSearch":"三星(SAMSUNG) Galaxy S24 Al智享生活办公 超视觉影像 第三代骁龙8 8GB+256GB 水墨黑 5G AI手机手机三星京东自营官方旗舰店","id":360,"image":"//img11.360buyimg.com/n7/jfs/t1/105669/35/48470/88509/65eadd08F50ab3480/c96b46bc41de9d0b.jpg","price":4999.0,"shopName":"三星京东自营官方旗舰店","stock":1000,"title":"三星(SAMSUNG) Galaxy S24 Al智享生活办公 超视觉影像 第三代骁龙8 8GB+256GB 水墨黑 5G AI手机","updateTime":1680094862,"views":0}},{"_index":"goods","_type":"_doc","_id":"341","_score":1.0,"_source":{"cateName":"汽车音箱","createTime":1673165093,"description":"","fullTextSearch":"6.5寸大柏林之声喇叭音箱空箱体汽车DIY水桶音响改装家用书架制作 柏林分频器一只汽车音箱豪车拆车喇叭小店","id":341,"image":"//img14.360buyimg.com/n7/jfs/t1/229538/29/7287/214554/6575620aFe254e913/2af724e95d01ab53.jpg","price":70.0,"shopName":"豪车拆车喇叭小店","stock":1000,"title":"6.5寸大柏林之声喇叭音箱空箱体汽车DIY水桶音响改装家用书架制作 柏林分频器一只","updateTime":1680094862,"views":0}},{"_index":"goods","_type":"_doc","_id":"597","_score":1.0,"_source":{"cateName":"狗罐头","createTime":1673165093,"description":"","fullTextSearch":"麦富迪(Myfoodie) 狗零食成幼犬通用营养拌饭狗湿粮包 羊奶肉包混合装70g*10袋 混合口味 全阶段 国产狗罐头麦富迪京东自营旗舰店","id":597,"image":"//img14.360buyimg.com/n7/jfs/t1/237833/8/13993/123403/65f57aa0F4f863c55/534fa4382e52005e.jpg","price":26.0,"shopName":"麦富迪京东自营旗舰店","stock":1000,"title":"麦富迪(Myfoodie) 狗零食成幼犬通用营养拌饭狗湿粮包 羊奶肉包混合装70g*10袋 混合口味 全阶段 国产","updateTime":1680094862,"views":0}},{"_index":"goods","_type":"_doc","_id":"587","_score":1.0,"_source":{"cateName":"狗罐头","createTime":1673165093,"description":"","fullTextSearch":"麦富迪零食鸡胸肉干907g鸡肉干泰迪金毛幼犬磨牙补钙训练宠物狗狗零食 鸡肉味 全阶段 宠物狗罐头圣贝宠宠物生活专营店","id":587,"image":"//img10.360buyimg.com/n7/jfs/t1/175445/21/14350/367519/60c47a93E44b64e1e/3b02b0b509dc52f8.jpg","price":55.0,"shopName":"圣贝宠宠物生活专营店","stock":1000,"title":"麦富迪零食鸡胸肉干907g鸡肉干泰迪金毛幼犬磨牙补钙训练宠物狗狗零食 鸡肉味 全阶段 宠物","updateTime":1680094862,"views":0}},{"_index":"goods","_type":"_doc","_id":"598","_score":1.0,"_source":{"cateName":"狗罐头","createTime":1673165093,"description":"","fullTextSearch":"麦富迪 宠物狗狗零食 成犬幼犬训狗奖励鸡肉干400g 鸡肉味 全阶段 宠物狗罐头麦富迪京东自营旗舰店","id":598,"image":"//img11.360buyimg.com/n7/jfs/t1/181140/23/42682/127918/65f57aaeFac864a94/c2dbfdddc0a45d1a.jpg","price":35.0,"shopName":"麦富迪京东自营旗舰店","stock":1000,"title":"麦富迪 宠物狗狗零食 成犬幼犬训狗奖励鸡肉干400g 鸡肉味 全阶段 宠物","updateTime":1680094862,"views":0}}]},"aggregations":{"sterms#distinctCateName":{"doc_count_error_upper_bound":0,"sum_other_doc_count":0,"buckets":[]}}}
可以看到有数据,但是聚合函数为空。
附带上代码:
public List<HashMap<String, Object>> getCategoryList() {SearchRequest searchRequest = new SearchRequest(EsIndex.GOODS.getValue());// 创建一个搜索源构建器,用于构建搜索请求的主体部分SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();// ### 定义聚合条件,按cate_name字段分组// 聚合名称String termsName = "distinctCateName";TermsAggregationBuilder aggregation = AggregationBuilders.terms(termsName).field("cate_name.keyword");searchSourceBuilder.aggregation(aggregation);searchRequest.source(searchSourceBuilder);try {SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);ParsedStringTerms categories = response.getAggregations().get(termsName);ArrayList<HashMap<String, Object>> result = new ArrayList<>();System.out.println(categories.getBuckets());// // 遍历每个具体的分组及其包含的结果// for (Terms.Bucket bucket : categories.getBuckets()) {// HashMap<String, Object> cateMap = new HashMap<>();// cateMap.put("name", bucket.getKeyAsString());// cateMap.put("value", bucket.getKeyAsString());// result.add(cateMap);// }return result;} catch (CustomizeException exception) { // 自定义异常一般是对外暴露的异常错误throw new CustomizeException(exception.getCode(), exception.getMessage(), exception);} catch (Exception exception) { // 全局异常处理500不打算对外暴露的异常错误throw new CustomizeException(500, exception.getMessage(), exception);}}
解决:
原因是:字段问题,由于我使用的实体类bean写入ES,字段名都是大写,但是实际ES中是小写,但是实际写入也不会报错。
查看es字段列表:
GET goods
# 打印结果:
{"goods" : {"aliases" : { },"mappings" : {"properties" : {"cate_name" : {"type" : "text","fields" : {"keyword" : {"type" : "keyword"}},"analyzer" : "ik_max_word"},"cate_name" : {"type" : "text","fields" : {"keyword" : {"type" : "keyword","ignore_above" : 256}}},"create_time" : {"type" : "date"},"description" : {"type" : "keyword","index" : false},"full_text_search" : {"type" : "text","copy_to" : ["title","cate_name","shop_name"],"analyzer" : "ik_max_word"},"id" : {"type" : "keyword"},"image" : {"type" : "keyword","index" : false},"price" : {"type" : "scaled_float","scaling_factor" : 100.0},"shopName" : {"type" : "text","analyzer" : "ik_max_word"},"shop_name" : {"type" : "text","fields" : {"keyword" : {"type" : "keyword","ignore_above" : 256}}},"status" : {"type" : "short"},"stock" : {"type" : "integer"},"title" : {"type" : "text","analyzer" : "ik_max_word"},"update_time" : {"type" : "date"},"views" : {"type" : "integer"}}},"settings" : {"index" : {"routing" : {"allocation" : {"include" : {"_tier_preference" : "data_content"}}},"number_of_shards" : "1","provided_name" : "goods","creation_date" : "1712113365252","number_of_replicas" : "1","uuid" : "UyIaiESoTSyhWJO4CuhZnw","version" : {"created" : "7120199"}}}}
}
可以看到cate name是驼峰法,所以重建索引改为驼峰法即可。
# 删除索引
DELETE goodsPUT goods
{"properties": {"id": {"type": "keyword"},"cateName": {"type": "text","analyzer": "ik_max_word","fields": {"keyword": {"type": "keyword"}}},"title": {"type": "text","analyzer": "ik_max_word"},"image": {"type": "keyword","index": false},"description": {"type": "keyword","index": false},"price": {"type": "scaled_float","scaling_factor": 100},"stock": {"type": "integer"},"views": {"type": "integer"},"status": {"type": "short"},"createTime": {"type": "date"},"updateTime": {"type": "date"},"shopName": {"type": "text","analyzer": "ik_max_word"},"fullTextSearch": {"type": "text","analyzer": "ik_max_word","copy_to": ["title","cate_name","shop_name"]}}
}