基于ES之业务数据分组求和TopN开发

简介: 需求:把作家所有作品的总点击数加起来求和再进行排序的一个实现

需求:把作家所有作品的总点击数加起来求和再进行排序的一个实现

1.先通过SQL语句计算出作家排行

select sum(a.clickcount) countTotal,a.author,group_concat(a.name) novel_names
from db_novel.novel_test a group by author order by countTotal desc ;


7.png

2.把SQL语句转换成ES语句

语句转换地址:http://www.ischoolbar.com/EsParser/

8.png

3.格式化 ES语句

格式化语句地址:http://www.bejson.com/

9.png

4.创建索引【novel_author_countsort 】

拷贝之前创建的novel索引,直接修改author字段的类型为【 keyword 】

{
  "novel_author_countsort" : {
    "mappings" : {
      "doc" : {
        "properties" : {
          "@timestamp" : {
            "type" : "date"
          },
          "@version" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "author" : {
            "type" : "keyword"
          },
          "category" : {
            "type" : "keyword"
          },
          "clickcount" : {
            "type" : "long"
          },
          "collect" : {
            "type" : "long"
          },
          "count" : {
            "type" : "long"
          },
          "countrecommend" : {
            "type" : "long"
          },
          "detail" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "id" : {
            "type" : "long"
          },
          "lastchapter" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "lastupdate" : {
            "type" : "date",
            "format" : "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
          },
          "monthclick" : {
            "type" : "long"
          },
          "monthrecommend" : {
            "type" : "long"
          },
          "name" : {
            "type" : "keyword"
          },
          "new" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "novelinfo" : {
            "type" : "keyword"
          },
          "picurl" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "status" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "weekclick" : {
            "type" : "long"
          },
          "weekrecommend" : {
            "type" : "long"
          }
        }
      }
    }
  }
}

5.给索引【novel_author_countsort】加载数据

# 加载数据
POST _reindex
{
  "source": {
    "index": "novel"
  },
  "dest": {
    "index": "novel_author_countsort"
  }
}

6.测试

基于之前SQL语句生成的ES进行测试

# 测试
GET novel_author_countsort/_search
{
  "size" : 0,
  "aggs": {
    "author": {
      "terms": {
        "field": "author",
        "size": 10,
        "order": {
          "countTotal": "DESC"
        }
      },
      "aggs": {
        "countTotal": {
          "sum": {
            "field": "clickcount"
          }
        },
        "top": {
          "top_hits": {
            "size": 1
          }
        }
      }
    }
  }
}

10.png

相关文章
使用mongo聚合分组查询获取每一组的时间最大的一条数据
使用mongo聚合分组查询获取每一组的时间最大的一条数据
956 0
|
12月前
|
数据挖掘
白话Elasticsearch49-深入聚合数据分析之 Percentile Ranks Aggregation-percentiles rank以及网站访问时延SLA统计
白话Elasticsearch49-深入聚合数据分析之 Percentile Ranks Aggregation-percentiles rank以及网站访问时延SLA统计
64 0
|
12月前
|
算法 数据挖掘
白话Elasticsearch46-深入聚合数据分析之Cardinality Aggs-cardinality去重算法以及每月销售品牌数量统计
白话Elasticsearch46-深入聚合数据分析之Cardinality Aggs-cardinality去重算法以及每月销售品牌数量统计
102 0
|
分布式计算 大数据 Spark
聚合操作_多维聚合_rollup 案例 | 学习笔记
快速学习聚合操作_多维聚合_rollup 案例
59 0
聚合操作_多维聚合_rollup 案例 | 学习笔记
|
大数据 开发者
聚合操作_多维聚合_rollup | 学习笔记
快速学习聚合操作_多维聚合_rollup
74 0
聚合操作_多维聚合_rollup | 学习笔记
|
SQL 分布式计算 Java
聚合操作_groupBy_聚合操作 | 学习笔记
快速学习聚合操作_groupBy_聚合操作
99 0
聚合操作_groupBy_聚合操作 | 学习笔记
|
SQL HIVE 开发者
Hive 高阶--分组窗口函数--聚合函数集成分组函数(SUM)|学习笔记
快速学习 Hive 高阶--分组窗口函数--聚合函数集成分组函数(SUM)
251 0
|
SQL 数据挖掘 关系型数据库
数据的分组与计算
对数据集进行分组并对各组应用一个函数(无论是聚合还是转换),通常是数据分析工作中的重要环节。在数据集准备好之后,通常就是计算分组统计或生成透视表。pandas 提供了一个灵活高效的 groupby 功能,使我们可以高效地对数据集进行操作。 关系型数据库和 SQL 能够如此流行的原因之一就是其能够方便地对数据进行连接、过滤、转换和聚合。但是,像 SQL 这样的查询语言所能执行的分组运算的种类很有限,而由于 pandas 强大的表达能力,我们可以执行复杂得多的分组运算。
98 0
|
存储 SQL
ES聚合查询详解(三):指标聚合
ES聚合查询详解(三):指标聚合
285 0
ES聚合查询详解(三):指标聚合
|
存储 SQL 关系型数据库
ES聚合查询详解(二):桶聚合
ES聚合查询详解(二):桶聚合
369 0
ES聚合查询详解(二):桶聚合