需求:把作家所有作品的总点击数加起来求和再进行排序的一个实现
    1.先通过SQL语句计算出作家排行

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

    image.png
    2.把SQL语句转换成ES语句
    语句转换地址:http://www.ischoolbar.com/EsParser/
    image.png
    3.格式化 ES语句
    格式化语句地址:http://www.bejson.com/
    image.png
    4.创建索引【novel_author_countsort 】
    拷贝之前创建的novel索引脚本,直接修改author字段的类型为【 keyword 】

    1. {
    2. "novel_author_countsort" : {
    3. "mappings" : {
    4. "doc" : {
    5. "properties" : {
    6. "@timestamp" : {
    7. "type" : "date"
    8. },
    9. "@version" : {
    10. "type" : "text",
    11. "fields" : {
    12. "keyword" : {
    13. "type" : "keyword",
    14. "ignore_above" : 256
    15. }
    16. }
    17. },
    18. "author" : {
    19. "type" : "keyword"
    20. },
    21. "category" : {
    22. "type" : "keyword"
    23. },
    24. "clickcount" : {
    25. "type" : "long"
    26. },
    27. "collect" : {
    28. "type" : "long"
    29. },
    30. "count" : {
    31. "type" : "long"
    32. },
    33. "countrecommend" : {
    34. "type" : "long"
    35. },
    36. "detail" : {
    37. "type" : "text",
    38. "fields" : {
    39. "keyword" : {
    40. "type" : "keyword",
    41. "ignore_above" : 256
    42. }
    43. }
    44. },
    45. "id" : {
    46. "type" : "long"
    47. },
    48. "lastchapter" : {
    49. "type" : "text",
    50. "fields" : {
    51. "keyword" : {
    52. "type" : "keyword",
    53. "ignore_above" : 256
    54. }
    55. }
    56. },
    57. "lastupdate" : {
    58. "type" : "date",
    59. "format" : "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
    60. },
    61. "monthclick" : {
    62. "type" : "long"
    63. },
    64. "monthrecommend" : {
    65. "type" : "long"
    66. },
    67. "name" : {
    68. "type" : "keyword"
    69. },
    70. "new" : {
    71. "type" : "text",
    72. "fields" : {
    73. "keyword" : {
    74. "type" : "keyword",
    75. "ignore_above" : 256
    76. }
    77. }
    78. },
    79. "novelinfo" : {
    80. "type" : "keyword"
    81. },
    82. "picurl" : {
    83. "type" : "text",
    84. "fields" : {
    85. "keyword" : {
    86. "type" : "keyword",
    87. "ignore_above" : 256
    88. }
    89. }
    90. },
    91. "status" : {
    92. "type" : "text",
    93. "fields" : {
    94. "keyword" : {
    95. "type" : "keyword",
    96. "ignore_above" : 256
    97. }
    98. }
    99. },
    100. "weekclick" : {
    101. "type" : "long"
    102. },
    103. "weekrecommend" : {
    104. "type" : "long"
    105. }
    106. }
    107. }
    108. }
    109. }
    110. }

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

    1. # 加载数据
    2. POST _reindex
    3. {
    4. "source": {
    5. "index": "novel"
    6. },
    7. "dest": {
    8. "index": "novel_author_countsort"
    9. }
    10. }

    6.测试
    基于之前SQL语句生成的ES脚本进行测试

    1. # 测试
    2. GET novel_author_countsort/_search
    3. {
    4. "size" : 0,
    5. "aggs": {
    6. "author": {
    7. "terms": {
    8. "field": "author",
    9. "size": 10,
    10. "order": {
    11. "countTotal": "DESC"
    12. }
    13. },
    14. "aggs": {
    15. "countTotal": {
    16. "sum": {
    17. "field": "clickcount"
    18. }
    19. },
    20. "top": {
    21. "top_hits": {
    22. "size": 1
    23. }
    24. }
    25. }
    26. }
    27. }
    28. }

    image.png
    7.测试数据分组页面效果image.png