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

  • 3,203 views
  • 阅读模式

最近需要做个大数据统计看板,需要用到数据统计功能,于是用es写了个统计脚本(我都是在Kibana写好再转为C#代码),如下:

GET /m_es_name/_search?search_type=count
{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "group_by_sex": {
        "terms": {
          "field": "Sex"
        }
    }
  }
}

简单的代码,但是,却报错了....错误如下

{
  "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即可,改为:

GET /m_es_name/_search
{
  "size": 0,
  "query": {
    "match_all": {}
  },
  "aggs": {
    "group_by_sex": {
        "terms": {
          "field": "Sex"
        }
    }
  }
}

个人博客: IT老五    简书:ThinkinLiu

源创不易,允许转载,来源勿删,多谢合作

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

文末福利:示例代码:aggs的terms及range统计方式,nested嵌套关联查询

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嵌套关联查询,这里老五不多说了,后面有时间再写

weinxin
扫码关注微信公众号--IT老五
微信扫一扫关注公众号,获取更多实用app,订阅地址不定时更新
IT老五
  • 本文由 发表于 2020-06-28 15:45:21
  • 转载请务必保留本文链接:https://itlao5.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:

    确定