Elasticsearch第12节 聚合查询

一、聚合查询

//
#聚合查询 : sum
#  "size": 0  去掉明细列表,只保留求和结果
GET /lib4/_search
{
  "size": 0,  
  "aggs": {
    "self_define_name_for_result": {
      "sum": {"field":"price"}
    }
  }
}

#最小值:min
GET /lib4/_search
{
  "size": 0, 
  "aggs": {
    "price_min": {
      "min": {"field":"price"}
    }
  }
}
#最大值:max
GET /lib4/_search
{
  "size": 0, 
  "aggs": {
    "price_max": {
      "max": {"field":"price"}
    }
  }
}

#平均值:avg
GET /lib4/_search
{
  "size": 0, 
  "aggs": {
    "price_avg": {
      "avg": {"field":"price"}
    }
  }
}

#基数:cardinality (互不相同的值的个数)
#select count(distinct column) from talbe_name
GET /lib4/_search
{
  "size": 0, 
  "aggs": {
    "price_cardinality": {
      "cardinality": {"field":"price"}
    }
  }
}



#分组:terms 相当于group by 
# 数字类型OK,text类型的不行
GET /lib4/_search
{
  "size": 0, 
  "aggs": {
    "price_of_group": {
      "terms": {"field":"price"}
    }
  }
}
#结果(部分数据):
  "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
        }
         ......      ]
    }
  }


#兴趣中包含xi huan字样的用户按年龄分组求年龄的平均值
# 并对分组年龄的平均值按降序排列
GET /myindex/_search
{
  "size": 0, 
  "query": {
    "match": {
      "intrest": "xi huan"
    }
  },
  "aggs": {
    "post_date_group": {
      "terms": {
        "field": "age",
        "order": {
          "age_of_avg": "desc"
        }
      },
      "aggs": {
        "age_of_avg": {
          "avg": {
            "field": "age"
          }
        }
      }
    }
  }
}

二、复合查询

GET /myindex/_search
{
  "query":{
    "bool":{
      "must": [
        {"match":{"intrest": "xi huan"}}
      ],
      "must_not": [
        {"match": {"intrest": "football"}}
      ],
      "should": [
        {"match": {"content": "t"}},
        {"range": {"age": {"gte": 10,"lte": 30}}}
      ]
    }
  }
}



GET /myindex/_search
{
  "query":{
    "bool":{
      "must": [
        {"match":{"intrest": "xi huan"}}
      ],
      "must_not": [
        {"match": {"intrest": "football"}}
      ],
      "should": [
        {"match": {"content": "t"}}
      ],
      "filter": {
        "range": {"age": {"gte": 10,"lte": 30}}
      }
    }
  }
}
三、constant_score
//
# constant_score 
# 它将一个不变的常量评分应用于所有匹配的文档,它被经常用于
# 你只需要一个filter(例如评分查询)而没有其它查询的情况
# term被放置到了constant_score中,转成不评分的filter,
# 这种方式可以用来取代只有filter语句的bool查询
GET /myindex/_search
{
  "query": {
    "constant_score": {
      "filter": {"term": {
        "id": "120"
      }},
      "boost": 1.2
    }
  }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • title:乐优商城学习笔记十一-Elasticsearch学习(三)date: 2019-04-18 09:49...
    smallmartial阅读 1,333评论 2 0
  • 说明:以下五十个语句都按照测试数据进行过测试,最好每次只单独运行一个语句。 问题及描述: --1.学生表 Stud...
    lijun_m阅读 1,408评论 0 1
  • 翻译自官方文档英文版,有删减。 这里使用的Java客户端版本是5.1.2,Elasticsearch的版本号也要是...
    liycode阅读 3,260评论 0 4
  • 秋天一个收获的季节, 即使秋情泛滥成灾, 也不要影响心情, 抱着温暖的瞄会暖一些, 人的心情也可舒畅一些。 人近不...
    朝壹阅读 190评论 0 0
  • 论成长,在社会和集体中锻炼才是最真实有效的 这一点在极限挑战中就得到了很有力的诠释 曾经在第一集就因为被抢东西而哭...
    盛企阅读 888评论 0 0

友情链接更多精彩内容