Elasticsearch 聚合函数返回空数组|查询返回空内容 rs里有数据

问题场景:

        今天用聚合函数想查询出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"]}}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/791066.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

WebKit简单介绍

WebKit是一个开源的浏览器网页排版引擎&#xff0c;起源于苹果公司&#xff0c;最初是为了开发Safari浏览器而创建的。WebKit主要包含两个核心组件&#xff1a;WebCore和JavaScriptCore。 WebCore&#xff1a;主要负责解析HTML文档、样式表&#xff08;CSS&#xff09;以及处理…

【考研数学】0基础网课汇总+资源分享

选老师千万别跟风&#xff01; 考研界里的大咖其实真的不少啊&#xff01;像是汤家凤、张宇、李永乐、武忠祥、王世安、杨超这些老师&#xff0c;都是大神级别的存在&#xff01;他们每个人都有自己独特的教学风格&#xff0c;只要跟着其中任何一个&#xff0c;认真听讲、做好…

[RK3128-LINUX] 关于 OpenGL ES2 实现画图相关问题

问题描述 在SDK中并没有找到有关OpenGL ES2 实现画图或者刷图的Demo程序&#xff0c;那么该功能如何实现呢&#xff1f; 解决方案&#xff1a; 标准api说明可以参考khronos定义&#xff1a;https://registry.khronos.org/ 相关书籍&#xff1a;《OpenGL超级宝典》、《openGL…

增强函数(Augmentation Function)

增强函数&#xff08;Augmentation Function&#xff09;通常用于扩充数据集&#xff0c;通过应用一系列的随机变换来生成新的数据样本。这在机器学习和深度学习中特别有用&#xff0c;因为增加数据的多样性可以帮助模型更好地泛化。下面是一个简单的增强函数的例子&#xff0c…

FFmpeg获取视频详情

话不多说&#xff0c;直接上代码&#xff1a; pom依赖&#xff1a; <!--视频多媒体工具包 包含 FFmpeg、OpenCV--><dependency><groupId>org.bytedeco</groupId><artifactId>javacv-platform</artifactId><version>1.5.3</versi…

linux清理缓存垃圾命令和方法介绍

在Linux系统中&#xff0c;清理缓存和垃圾文件可以通过多种方法完成&#xff0c;这些方法旨在释放磁盘空间、提高系统性能。以下是一些常用的方法&#xff0c;结合了搜索结果中的信息&#xff1a; 1. 使用sync和echo命令清除RAM缓存和交换空间1 清除页面缓存&#xff08;Page …

【c++基础】数池塘(四方向)

说明 农夫约翰的农场可以表示成N*M&#xff08;1≤N、M≤100&#xff09;个方格组成的矩形。由于近日的降雨&#xff0c;在约翰农场上的不同地方形成了池塘。每一个方格或者有积水&#xff08;W&#xff09;或者没有积水&#xff08;.&#xff09;。农夫约翰打算数出他的农场上…

深入理解 Linux 内核链表:C 链表的实用性和优势

深入理解 Linux 内核链表&#xff1a;C 链表的实用性和优势 在 Linux 内核的设计和实现中&#xff0c;链表是一种非常关键的数据结构&#xff0c;尤其是因为它在处理动态数据集合时的高效性和灵活性。本文将深入探讨链表在 Linux 内核中的作用以及内核开发者如何利用 C 语言中…

css 属性值initial、unset、revert

1.initial CSS 关键字 initial 将属性的初始&#xff08;或默认&#xff09;值应用于元素。不应将初始值与浏览器样式表指定的值混淆。它可以应用于任何 CSS 属性。这包括 CSS 简写 all&#xff0c;initial 可用于将所有 CSS 属性恢复到其初始状态。 2.unset CSS 关键字 uns…

【css】使用display:inline-block后,元素间存在4px的间隔

问题&#xff1a;在本地项目中使用【display: inline-block】&#xff0c;元素间存在4px间隔。打包后发布到外网又不存在这个问题了。 归根结底这是一个西文排版的问题&#xff0c;英文有空格作为词分界&#xff0c;而中文则没有。 此时的元素具有文本属性&#xff0c;只要标签…

基于深度学习的肿瘤图像检测系统(网页版+YOLOv8/v7/v6/v5代码+训练数据集)

摘要&#xff1a;在本博客中&#xff0c;我们深入探讨了基于YOLOv8/v7/v6/v5的肿瘤图像检测系统。核心上&#xff0c;我们采用了最新的YOLOv8技术&#xff0c;并将其与YOLOv7、YOLOv6、YOLOv5算法进行了综合整合和性能指标对比分析。我们详细阐述了当前国内外在此领域的研究现状…

python实现UDP服务器

import socket # 创建UDP socket udp_server_socket socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # 绑定地址和端口 server_address (localhost, 12000) udp_server_socket.bind(server_address) # 服务器循环 while True: # 接收客户端消息 message, c…

Python机器学习实验 Python 数据分析

1.实验目的 掌握常见数据预处理方法&#xff0c;熟练运用数据分析方法&#xff0c;并掌握 Python 中的 Numpy、 Pandas 模块提供的数据分析方法。 2.实验内容 1. Pandas 基本数据处理 使用 Pandas 模块&#xff0c;完成以下操作。 &#xff08;1&#xff09;创建一个由 0 到 50…

Ai音乐大师演示(支持H5、小程序)独立部署源码

Ai音乐大师演示&#xff08;支持H5、小程序&#xff09;独立部署源码

使用IntelliJ IDEA配置版本管理(SVN和Git)

一&#xff1a;SVN版本管理 步骤一&#xff1a;安装SVN插件 打开IntelliJ IDEA&#xff0c;点击顶部菜单栏的 "File" -> "Settings"。在弹出的窗口中&#xff0c;选择 "Plugins"。在搜索框中输入 "SVN"&#xff0c;找到 "Su…

如何在Python中处理JSON数据?

如何在Python中处理JSON数据&#xff1f; 在Python中处理JSON数据是一个常见的任务&#xff0c;因为JSON&#xff08;JavaScript Object Notation&#xff09;是一种轻量级的数据交换格式&#xff0c;它易于人阅读和编写&#xff0c;同时也易于机器解析和生成。Python的内置库…

政安晨:【Keras机器学习实践要点】(十五)—— KerasTuner 简述

目录 导言 调整模型结构 定义搜索空间 开始搜索 查询结果 重新训练模型 调整模型训练 调整数据预处理 重新训练模型 指定调整目标 以内置指标为目标 以自定义指标为目标 调整端到端工作流程 将 Keras 代码分开 政安晨的个人主页&#xff1a;政安晨 欢迎 &#x1…

Web框架开发-Django-model进阶

一、QuerySet 可切片 使用python的切片语法来限制查询集记录的数目,它等同于SQL的limit和offset子句。 1 2 In [2]: Book.objects.all()[:5] # (LIMIT 5) In [2]: Book.objects.all()[5:10]     # (OFFSET 5 LIMIT 5) 不支持负的索引(例…

漫谈结构体

注意&#xff1a; 1.结构体是自定义数据类型&#xff0c;定义之后使用就跟使用库自带的int这些数据类型一样的。 2.定义结构体类型不会分配内存空间&#xff0c;定义变量才会。 1.匿名结构体&#xff08;声明时创建变量&#xff0c;不然没啥用&#xff09; 匿名结构体是没有定…

React 入门

一、官网地址 英文官网: https://reactjs.org/中文官网: https://react.docschina.org/ 二、React 特点 声明式编码组件化编码React Native 编写原生应用高效&#xff08;优秀的 Diffing 算法&#xff09;高效的原因&#xff1a;1.使用虚拟DOM&#xff0c;不总是直接操作页面…