参考链接:

  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
    }
  }
}
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐