es聚合查詢之指標聚合


1.ES聚合分析是什么

  聚合分析是數據庫中重要的功能特性,完成對⼀個查詢的數據集中數據的聚合計算,如:找 出某字段(或計算表達式的結果)的最⼤值、最⼩值,計算和、平均值等。ES作為搜索引擎 兼數據庫,同樣提供了強⼤的聚合分析能⼒。

  對⼀個數據集求最⼤、最⼩、和、平均值等指標的聚合,在ES中稱為指標聚合

  ⽽關系型數據庫中除了有聚合函數外,還可以對查詢出的數據進⾏分組group by,再在組上 進⾏指標聚合。在ES中稱為桶聚合

 

2.max min sum avg

  求出⽕箭隊球員的平均年齡

POST /nba/_search
{
  "query": {
    "match": {
      "teamNameEn": "Rockets"
    }
  },
  "aggs": {
    "avgAge": {
      "avg": {
        "field": "playYear"
      }
    }
  }
}

  效果:

  

 

 

3.value_count 統計⾮空字段的⽂檔數

   求出⽕箭隊中球員打球時間不為空的數量

 

POST /nba/_search
{
  "query": {
    "match": {
      "teamNameEn": "Rockets"
    }
  },
  "aggs": {
    "countPlayreYear": {
      "value_count": {
        "field": "playYear"
      }
    }
  },
  "size": 1
}

  加上size,效果:

{
  "took" : 7,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 21,
      "relation" : "eq"
    },
    "max_score" : 3.2723064,
    "hits" : [
      {
        "_index" : "nba",
        "_type" : "_doc",
        "_id" : "86",
        "_score" : 3.2723064,
        "_source" : {
          "countryEn" : "Switzerland",
          "teamName" : "火箭",
          "birthDay" : 769233600000,
          "country" : "瑞士",
          "teamCityEn" : "Houston",
          "code" : "clint_capela",
          "displayAffiliation" : "Switzerland/Switzerland",
          "displayName" : "克林特 卡佩拉",
          "schoolType" : "",
          "teamConference" : "西部",
          "teamConferenceEn" : "Western",
          "weight" : "108.9 公斤",
          "teamCity" : "休斯頓",
          "playYear" : 5,
          "jerseyNo" : "15",
          "teamNameEn" : "Rockets",
          "draft" : 2014,
          "displayNameEn" : "Clint Capela",
          "heightValue" : 2.08,
          "birthDayStr" : "1994-05-18",
          "position" : "中鋒",
          "age" : 25,
          "playerId" : "203991"
        }
      }
    ]
  },
  "aggregations" : {
    "countPlayreYear" : {
      "value" : 21
    }
  }
}

  

4.Cardinality 值去重

  計數 查出⽕箭隊中年齡不同的數量

POST /nba/_search
{
    "query":{
        "term":{
            "teamNameEn":{
                "value":"Rockets"
            }
        }
    },
    "aggs":{
        "counAget":{
            "cardinality":{
                "field":"age"
            }
        }
    },
    "size":0
}

  

5.stats 統計count max min avg sum 5個值

POST /nba/_search
{
  "query": {
    "match": {
      "teamNameEn": "Rockets"
    }
  },
  "aggs": {
    "statsField": {
      "stats": {
        "field": "playYear"
      }
    }
  },
  "size": 1
}

  效果:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 21,
      "relation" : "eq"
    },
    "max_score" : 3.2723064,
    "hits" : [
      {
        "_index" : "nba",
        "_type" : "_doc",
        "_id" : "86",
        "_score" : 3.2723064,
        "_source" : {
          "countryEn" : "Switzerland",
          "teamName" : "火箭",
          "birthDay" : 769233600000,
          "country" : "瑞士",
          "teamCityEn" : "Houston",
          "code" : "clint_capela",
          "displayAffiliation" : "Switzerland/Switzerland",
          "displayName" : "克林特 卡佩拉",
          "schoolType" : "",
          "teamConference" : "西部",
          "teamConferenceEn" : "Western",
          "weight" : "108.9 公斤",
          "teamCity" : "休斯頓",
          "playYear" : 5,
          "jerseyNo" : "15",
          "teamNameEn" : "Rockets",
          "draft" : 2014,
          "displayNameEn" : "Clint Capela",
          "heightValue" : 2.08,
          "birthDayStr" : "1994-05-18",
          "position" : "中鋒",
          "age" : 25,
          "playerId" : "203991"
        }
      }
    ]
  },
  "aggregations" : {
    "statsField" : {
      "count" : 21,
      "min" : 0.0,
      "max" : 17.0,
      "avg" : 5.333333333333333,
      "sum" : 112.0
    }
  }
}

  

6.Extended stats ⽐stats多4個統計結果: 平⽅和、⽅差、標准差、平均值加/減兩個標准差的區間

POST /nba/_search
{
  "query": {
    "match": {
      "teamNameEn": "Rockets"
    }
  },
  "aggs": {
    "statsField": {
      "extended_stats": {
        "field": "playYear"
      }
    }
  },
  "size": 1
}

  效果:

{
  "took" : 8,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 21,
      "relation" : "eq"
    },
    "max_score" : 3.2723064,
    "hits" : [
      {
        "_index" : "nba",
        "_type" : "_doc",
        "_id" : "86",
        "_score" : 3.2723064,
        "_source" : {
          "countryEn" : "Switzerland",
          "teamName" : "火箭",
          "birthDay" : 769233600000,
          "country" : "瑞士",
          "teamCityEn" : "Houston",
          "code" : "clint_capela",
          "displayAffiliation" : "Switzerland/Switzerland",
          "displayName" : "克林特 卡佩拉",
          "schoolType" : "",
          "teamConference" : "西部",
          "teamConferenceEn" : "Western",
          "weight" : "108.9 公斤",
          "teamCity" : "休斯頓",
          "playYear" : 5,
          "jerseyNo" : "15",
          "teamNameEn" : "Rockets",
          "draft" : 2014,
          "displayNameEn" : "Clint Capela",
          "heightValue" : 2.08,
          "birthDayStr" : "1994-05-18",
          "position" : "中鋒",
          "age" : 25,
          "playerId" : "203991"
        }
      }
    ]
  },
  "aggregations" : {
    "statsField" : {
      "count" : 21,
      "min" : 0.0,
      "max" : 17.0,
      "avg" : 5.333333333333333,
      "sum" : 112.0,
      "sum_of_squares" : 1140.0,
      "variance" : 25.84126984126984,
      "std_deviation" : 5.083430912412387,
      "std_deviation_bounds" : {
        "upper" : 15.500195158158107,
        "lower" : -4.833528491491442
      }
    }
  }
}

  

7.Percentiles 占⽐百分位對應的值統計,默認返回[ 1, 5, 25, 50, 75, 95, 99 ]分位上的值

   查出⽕箭的球員的年齡占⽐

POST /nba/_search
{
    "query": {
        "term": {
            "teamNameEn": {
                "value": "Rockets"
            }
        }
    },
    "aggs": {
        "percentAge": {
            "percentiles": {
                "field": "age",
                "percents": [
                    20,
                    50,
                    75
                ]
            }
        }
    },
    "size": 0
}

  效果:

{
  "took" : 37,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 21,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "percentAge" : {
      "values" : {
        "20.0" : 21.7,
        "50.0" : 25.0,
        "75.0" : 30.25
      }
    }
  }
}

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM