ElasticSearch学习

文章40 |   阅读 18962 |   点赞0

来源:https://blog.csdn.net/ywl470812087/category_9621251.html

ElasticSearch 聚合查询

x33g5p2x  于2021-12-19 转载在 其他  
字(3.2k)|赞(0)|评价(0)|浏览(573)

价格总和:

1,使用aggs   2,自己起个名字price_of_sum  3,求和sum   4,filed要求和的字段

GET /lib5/items/_search
{
  "aggs": {
    "price_of_sum": {
      "sum": {
        "field": "price"
      }
    }
  }
}

聚合查询结果:

{
  "took" : 414,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 5,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "lib5",
        "_type" : "items",
        "_id" : "5",
        "_score" : 1.0,
        "_source" : {
          "price" : null,
          "itemID" : "ID100127"
        }
      },
      {
        "_index" : "lib5",
        "_type" : "items",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "price" : 50,
          "itemID" : "ID100124"
        }
      },
      {
        "_index" : "lib5",
        "_type" : "items",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "price" : 30,
          "itemID" : "ID100125"
        }
      },
      {
        "_index" : "lib5",
        "_type" : "items",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "price" : 40,
          "itemID" : "ID100123"
        }
      },
      {
        "_index" : "lib5",
        "_type" : "items",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "price" : 25,
          "itemID" : "ID100124"
        }
      }
    ]
  },
  "aggregations" : {
    "price_of_sum" : {
      "value" : 145.0
    }
  }
}

发现好多信息,但是我们并不关系我只想看聚合结果,只需要加一个 "size": 0 

#练习 对有唱歌兴趣的用户按年龄分组
GET /lib4/user/_search
{
  "query": {
    "match": {
      "interests": "唱歌"
    }
  },
  "aggs": {
    "age_of_group": {
      "terms": {
        "field": "age"
      }
    }
  }
  
}
#2.9聚合查询
#(1)sum
GET /lib5/items/_search 
{ 
  "size":0, 
  "aggs":{ 
    "price_of_sum":{ 
      "sum":{ "field": "price" }
    }
  }
}

#(2)min
GET /lib5/items/_search 
{
  "size":0, 
  "aggs":{
    "price_of_min":{
      "min":{ "field": "price" }
    }
  }
}

#(3)max
GET /lib5/items/_search
{ 
  "size":0, 
  "aggs":{
    "price_of_max":{ 
      "max":{ "field": "price"}
    }
  }
}

#(4)avg
GET /lib5/items/_search 
{ 
  "size" :0,
  "aggs":{ 
    "price_of_avg":{
      "avg":{"field": "price" }
    }
  }
}

#(5)cardinality:求基数
GET /lib5/items/_search 
{ 
  "size" :0, 
  "aggs":{
    "price_of_cardi": {
      "cardinality":{ "field": "price" }
    }
  }
}

#(6)terms:分组
GET /lib5/items/_search
{ 
  "size":0,
  "aggs":{
    "price_group_by":{
      "terms":{ "field": "price" }
    }
  }
}

#对那些有唱歌兴趣的用户按年龄分组
GET /lib3/user/_search 
{ 
  "query":{
    "match":{"interests": "changge" }
  }, 
  "size":0, 
  "aggs":{ 
    "age_group_by":{
      "terms":{
        "field": "age", 
        "order":{"avg_of_age": "desc" }
      }, 
      "aggs":{
        "avg_of_age":{
          "avg":{ "field": "age"}
        }
      }
    }
  }
}

相关文章