elasticsearch es sum求和统计用法

参考链接:

  1. 聚合用法
  2. 用 extendedstats
  3. 用 stats
  4. 用 sum

有多种方法都可以实现:

  1. 用 stats ,其中 “query” 是我自己需要想要查询出的记录,query 部分是我的查询标准,大家在用的时候需要根据业务修改。 my_stats 就是个名字,随便起都行。stats、field 不能修改。大家需要修改的是 height8.height 字段,替换为自己需要的即可。
GET xxxx/_doc/_search
{
  "query" : {
    "terms": {
      "exerciseId": [50716279]
    }
  },
  "aggs": {
    "my_stats: {
      "stats": {
        "field": "height8.height"
      }
    }
  }
  
}

返回结果:

{
  "took": 54,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "xxx",
        "_type": "_doc",
        "_id": "50716279",
        "_score": 1,
        "_source": {
        "exerciseId": 50716279,
        "height8": {
          "analysis": 0,
          "answer": 0,
          "context": 1149,
          "height": 1149
        }
        
      }
    }
    ]
  },
  "aggregations": {
    "my_stats": {
      "count": 1,
      "min": 1149,
      "max": 1149,
      "avg": 1149,
      "sum": 1149
    }
  }
}
  1. 用 extended_stats ,其中 “query” 是我自己需要想要查询出的记录,query 部分是我的查询标准,大家在用的时候需要根据业务修改。 my_stats 就是个名字,随便起都行。extended_stats、field 不能修改。大家需要修改的是 height8.height 字段,替换为自己需要的即可。
GET xxxx/_doc/_search
{
  "query" : {
    "terms": {
      "exerciseId": [50716279]
    }
  },
  "aggs": {
    "my_stats: {
      "extended_stats": {
        "field": "height8.height"
      }
    }
  }
  
}

返回结果:

{
  "took": 54,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "xxx",
        "_type": "_doc",
        "_id": "50716279",
        "_score": 1,
        "_source": {
        "exerciseId": 50716279,
        "height8": {
          "analysis": 0,
          "answer": 0,
          "context": 1149,
          "height": 1149
        }
        
      }
    }
    ]
  },
  "aggregations": {
    "my_stats": {
      "count": 1,
      "min": 1149,
      "max": 1149,
      "avg": 1149,
      "sum": 1149,
      "sum_of_squares": 1320201,
      "variance": 0,
      "std_deviation": 0,
      "std_deviation_bounds": {
        "upper": 1149,
        "lower": 1149
      }
    }
  }
}
  1. 用 sum ,其中 “query” 是我自己需要想要查询出的记录,query 部分是我的查询标准,大家在用的时候需要根据业务修改。 my_stats 就是个名字,随便起都行。sum、field 不能修改。大家需要修改的是 height8.height 字段,替换为自己需要的即可。
GET xxxx/_doc/_search
{
  "query" : {
    "terms": {
      "exerciseId": [50716279]
    }
  },
  "aggs": {
    "my_stats: {
      "sum": {
        "field": "height8.height"
      }
    }
  }
  
}

返回结果:

{
  "took": 54,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "xxx",
        "_type": "_doc",
        "_id": "50716279",
        "_score": 1,
        "_source": {
        "exerciseId": 50716279,
        "height8": {
          "analysis": 0,
          "answer": 0,
          "context": 1149,
          "height": 1149
        }
        
      }
    }
    ]
  },
  "aggregations": {
    "my_stats": {
      "value": 1149
    }
  }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值