elasticsearch聚合查询

sum求和

GET lib4/items/_search
{
  "size": 0,   //只查看price_of_sum的值
  "aggs": {
    "price_of_sum": {
      "sum": {
        "field": "price"
      }
    }
  }
}

min求最小值

GET /lib4/items/_search
{
  "size": 0,
  "aggs": {
    "price_of_min": {
      "min": {
        "field": "price"
      }
    }
  }
}

max求最大值

GET /lib4/items/_search
{
  "size": 0,
  "aggs": {
    "price_of_min": {
      "max": {
        "field": "price"
      }
    }
  }
}

avg求平均值

GET /lib4/items/_search
{
  "size": 0,
  "aggs": {
    "price_of_min": {
      "avg": {
        "field": "price"
      }
    }

cardinality求基数(互不相同的)

GET /lib4/items/_search
{
  "size": 0,
  "aggs": {
    "price_of_cardid": {
      "cardinality": {
        "field": "price"
      }
    }
  }
}
  }
}

terms分组

GET /lib4/items/_search
{
  "size": 0,
  "aggs": {
    "price_of_group": {
      "terms": {
        "field": "price"
        
      }
    }

 

查询喜欢唱歌的并按年龄分组

GET /lib3/user/_search
{
  "size": 0,
  "query": {
    "match": {
      "interests": "changge"
    }
  },
  "aggs": {
    "age_of_group": {
      "terms": {
        "field": "age"
       
      }
    }
  }
}
  }
}

查询喜欢唱歌的并按年龄分组并求每一组的平均年龄

GET /lib3/user/_search
{
  "size": 0,
  "query": {
    "match": {
      "interests": "changge"
    }
  },
  "aggs": {
    "age_of_group": {
      "terms": {
        "field": "age"
       
      },
      "aggs": {
        "age_of_avg": {
          "avg": {
            "field": "age"
          }
        }
      }
    }
  }
}

查询喜欢唱歌的并按年龄分组并求每一组的平均年龄并按平均年龄排序

GET /lib3/user/_search
{
  "size": 0,
  "query": {
    "match": {
      "interests": "changge"
    }
  },
  "aggs": {
    "age_of_group": {
      "terms": {
        "field": "age"
         , "order": {
           "age_of_avg": "asc"
         }
      },
      "aggs": {
        "age_of_avg": {
          "avg": {
            "field": "age"
          }
        }
      }
    }
  }
}

你可能感兴趣的:(elasticsearch)