聚合查詢


聚合查詢

  • sum

    # 聚合查詢
    GET /lib3/user/_search
    {
     "size": 0, #
     "aggs": {
       "price_of_sum": { # 取名
         "sum": {
           "field": "price"
        }
      }
    }
    }
  • min

    GET /lib3/user/_search
    {
     "size": 0,
     "aggs": {
       "price_of_min": {
         "min": {
           "field": "price"
        }
      }
    }
    }
  • max

    GET /lib3/user/_search
    {
     "size": 0,
     "aggs": {
       "price_of_max": {
         "max": {
           "field": "price"
        }
      }
    }
    }
  • avg

    GET /lib3/user/_search
    {
     "size": 0,
     "aggs": {
       "price_of_avg": {
         "avg": {
           "field": "price"
        }
      }
    }
    }
  • cardinality:求基數(去重后統計總數)

    GET /lib3/user/_search
    {
     "size": 0,
     "aggs": {
       "price_of_cardinality": {
         "cardinality": {
           "field": "price"
        }
      }
    }
    }
    # 執行結果
    {
     "took": 18,
     "timed_out": false,
     "_shards": {
       "total": 5,
       "successful": 5,
       "failed": 0
    },
     "hits": {
       "total": 5,
       "max_score": 0,
       "hits": []
    },
     "aggregations": {
       "price_of_cardinality": {
         "value": 4
      }
    }
    }
  • terms:分組

    GET /lib3/user/_search
    {
     "size": 0,
     "aggs": {
       "price_of_group": {
         "terms": {
           "field": "price"
        }
      }
    }
    }
    # 執行結果
    {
     "took": 3,
     "timed_out": false,
     "_shards": {
       "total": 5,
       "successful": 5,
       "failed": 0
    },
     "hits": {
       "total": 5,
       "max_score": 0,
       "hits": []
    },
     "aggregations": {
       "price_of_group": {
         "doc_count_error_upper_bound": 0,
         "sum_other_doc_count": 0,
         "buckets": [
          {
             "key": 25,
             "doc_count": 1
          },
          {
             "key": 30,
             "doc_count": 1
          },
          {
             "key": 40,
             "doc_count": 1
          },
          {
             "key": 50,
             "doc_count": 1
          }
        ]
      }
    }
    }
  • 綜合題: 對哪些有唱歌興趣的用戶按年齡分組

    GET /lib2/user/_search
    {
       "query":{
           "match":{
               "interests":"鍛煉"
          }
      },
       "aggs":{
           "age_of_group":{
               "terms":{
                   "field":"age"
              }
          }
      }
    }
  • 對哪些有唱歌興趣的用戶按年齡分組,然后求年齡平均值

    {
     "query":{
         "match":{
             "interests":"鍛煉"
        }
    },
     "aggs":{
         "age_of_group":{
             "terms":{
                 "field":"age"
            },
             "aggs": {
               "age_of_avg": {
                 "avg": {
                   "field": "age"
                }
              }
            }
        }
    }
    }
    # 執行結果
    {
     "took": 12,
     "timed_out": false,
     "_shards": {
       "total": 5,
       "successful": 5,
       "failed": 0
    },
     "hits": {
       "total": 3,
       "max_score": 0.7549128,
       "hits": [
        {
           "_index": "lib2",
           "_type": "user",
           "_id": "2",
           "_score": 0.7549128,
           "_source": {
             "name": "趙明",
             "adress": "北京海淀區清河",
             "age": 20,
             "birthday": "1998-10-12",
             "interests": "喜歡喝酒,鍛煉,唱歌"
          }
        },
        {
           "_index": "lib2",
           "_type": "user",
           "_id": "3",
           "_score": 0.2876821,
           "_source": {
             "name": "lisi",
             "adress": "北京海淀區清河",
             "age": 23,
             "birthday": "1998-10-12",
             "interests": "喜歡喝酒,鍛煉,唱歌"
          }
        },
        {
           "_index": "lib2",
           "_type": "user",
           "_id": "1",
           "_score": 0.2824934,
           "_source": {
             "name": "趙六",
             "adress": "黑龍江鐵嶺",
             "age": 50,
             "birthday": "1970-10-12",
             "interests": "喜歡喝酒,鍛煉,說相聲"
          }
        }
      ]
    },
     "aggregations": {
       "age_of_group": {
         "doc_count_error_upper_bound": 0,
         "sum_other_doc_count": 0,
         "buckets": [
          {
             "key": 20,
             "doc_count": 1,
             "age_of_avg": {
               "value": 20
            }
          },
          {
             "key": 23,
             "doc_count": 1,          "age_of_avg": {            "value": 23         }       },       {          "key": 50,          "doc_count": 1,          "age_of_avg": {            "value": 50         }       }     ]   } }}
  • 對哪些有唱歌興趣的用戶按年齡分組,再對年齡求平均值,並按平均年齡降序

    GET /lib2/user/_search
    {
     "size": 0,
     "query": {
       "match": {
         "interests": "鍛煉"
      }
    }
    , "aggs": {
       "age_of_group": {
         "terms": {
           "field": "age",
           "order": {
             "age_of_avg": "desc"
          }
        },
         "aggs": {
           "age_of_avg": {
             "avg": {
               "field": "age"
            }
          }
        }
      }
    }
    }
    # 查詢結果
    {
     "took": 5,
     "timed_out": false,
     "_shards": {
       "total": 5,
       "successful": 5,
       "failed": 0
    },
     "hits": {
       "total": 3,
       "max_score": 0,
       "hits": []
    },
     "aggregations": {
       "age_of_group": {
         "doc_count_error_upper_bound": 0,
         "sum_other_doc_count": 0,
         "buckets": [
          {
             "key": 50,
             "doc_count": 1,
             "age_of_avg": {
               "value": 50
            }
          },
          {
             "key": 23,
             "doc_count": 1,
             "age_of_avg": {
               "value": 23
            }
          },
          {
             "key": 20,
             "doc_count": 1,
             "age_of_avg": {
               "value": 20
            }
          }
        ]
      }
    }
    }


免責聲明!

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



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