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

最近需要做个大数据统计看板,需要用到数据统计功能,于是用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过时,用
关注公众号,与老五一起进步!

文末福利:示例代码: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嵌套关联查询,这里老五不多说了,后面有时间再写

你可能感兴趣的:(elasticsearch:统计search_type=count过时,用"size":0替换)