最近需要做个大数据统计看板,需要用到数据统计功能,于是用es写了个统计脚本(我都是在Kibana写好再转为C#代码),如下:
GET /m_es_name/_search?search_type=count { "query": { "match_all": {} }, "aggs": { "group_by_sex": { "terms": { "field": "Sex" } } } }
简单的代码,但是,却报错了....错误如下文章源自IT老刘-https://itlao6.com/3200.html
{ "error": { "root_cause": [ { "type": "illegal_argument_exception", "reason": "No search type for [count]" } ], "type": "illegal_argument_exception", "reason": "No search type for [count]" }, "status": 400 }
原来,我所用的es版本中search_type=count已经被移除,不能使用,替代方案直接用"size": 0即可,改为:文章源自IT老刘-https://itlao6.com/3200.html
GET /m_es_name/_search { "size": 0, "query": { "match_all": {} }, "aggs": { "group_by_sex": { "terms": { "field": "Sex" } } } }
个人博客: IT老五 简书:ThinkinLiu文章源自IT老刘-https://itlao6.com/3200.html
源创不易,允许转载,来源勿删,多谢合作文章源自IT老刘-https://itlao6.com/3200.html
文末福利:示例代码:aggs的terms及range统计方式,nested嵌套关联查询文章源自IT老刘-https://itlao6.com/3200.html
GET /m_itlao5_com/_search { "_source": false, "size": 0, "query": { "bool": { "must": [ {"term": {"State": "2"}}, { "nested": { "path": "Taskinfo", "query": { "bool": { "must": [ { "term": { "Taskinfo.Stage": "100" } }, { "term": { "Taskinfo.Result": "2" } } ] } } } } ] } }, "aggs": { "group_by_sex": { "terms": { "field": "Sex" } } } }
GET /m_itlao5_com/_search { "_source": false, "size": 0, "query": { "bool": { "must": [ {"term": {"State": "2"}}, { "nested": { "path": "Taskinfo", "query": { "bool": { "must": [ { "term": { "Taskinfo.Stage": "100" } }, { "term": { "Taskinfo.Result": "2" } } ] } } } } ] } }, "aggs": { "group_by_age": { "range": { "field": "Age", "ranges": [ { "from": 26, "to": 35 }, { "from": 36, "to": 45 }, { "from": 45, "to": 60 }, { "from": 60 } ] } } } }
上面老五给出两条统计语句,用于根据性别及年龄段统计,分别用到了aggs的terms及range两种统计方式,还用到了nested嵌套关联查询,这里老五不多说了,后面有时间再写文章源自IT老刘-https://itlao6.com/3200.html 文章源自IT老刘-https://itlao6.com/3200.html
继续阅读
我的微信公众号
微信扫一扫关注公众号,不定时更新
北京市 1F
促进下收录,谢谢!