elasticsearch:统计search_type=count过时,用”size”:0替换

itlao6 DB 原创 开发&源码1 829字数 1388阅读4分37秒阅读模式

最近需要做个大数据统计看板,需要用到数据统计功能,于是用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

elasticsearch:统计search_type=count过时,用”size”:0替换
IT老五(it-lao5):关注公众号,一起源创,一起学习!

文末福利:示例代码: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

继续阅读
weinxin
我的微信公众号
微信扫一扫关注公众号,不定时更新
itlao6
  • 本文由 发表于 2020年 6月 28日 15:45:21
  • 转载请务必保留本文链接:https://itlao6.com/3200.html
评论  1  访客  1
    • 测试
      测试 6

      促进下收录,谢谢!

    匿名

    发表评论

    匿名网友

    :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

    确定