• es 分组后排序 再分页


    1. //获取最近时间的一条数据
    2. TopHitsAggregationBuilder maxTime = AggregationBuilders.topHits(maxField).sort(maxField, SortOrder.DESC).size(1);
    3. //根据groupField分组
    4. TermsAggregationBuilder termsAggregationBuilder =
    5. //首先是分组 AggregationBuilders.terms(groupField).field(groupField).subAggregation(maxTime).
    6. size(10000);
    7. //获取最大的时间为了排序去做的聚合
    8. MaxAggregationBuilder field = AggregationBuilders.max("maxSort").field("msgSendTime");
    9. termsAggregationBuilder.subAggregation(field);
    10. //这个地方的排序必须要用 一个number类型的聚合去排序
    11. FieldSortBuilder fieldSortBuilder=new FieldSortBuilder("maxSort").order(SortOrder.DESC);
    12. termsAggregationBuilder.subAggregation(new BucketSortPipelineAggregationBuilder("pageAndSort",
    13. Lists.newArrayList(fieldSortBuilder)).from(msgListReqDTO.getStartIndex()).size(msgListReqDTO.getPageSize()));
    14. //设置 searchQuery
    15. searchSourceBuilder.query(boolQueryBuilder).aggregation(termsAggregationBuilder).
    16. sort(maxField, SortOrder.DESC).size(0);
    17. request.searchType(SearchType.DEFAULT).source(searchSourceBuilder);
    1. GET intelligent-core_message_es_index/_search
    2. {
    3. "size": 0,
    4. "query": {
    5. "bool": {
    6. "must": [{
    7. "match": {
    8. "sessionStatus": {
    9. "query": "END"
    10. }
    11. }
    12. }, {
    13. "match": {
    14. "userId": {
    15. "query": "20220822151440349572086684327936"
    16. }
    17. }
    18. }],
    19. "must_not": [{
    20. "match": {
    21. "sendRole": {
    22. "query": "SYSTEM_VISITOR"
    23. }
    24. }
    25. }, {
    26. "terms": {
    27. "sendRole": ["SYSTEM_VISITOR", "SYSTEM_USER"]
    28. }
    29. }],
    30. "adjust_pure_negative": true,
    31. "boost": 1.0
    32. }
    33. },
    34. "sort": [{
    35. "msgSendTime": {
    36. "order": "desc"
    37. }
    38. }],
    39. "aggregations": {
    40. "visitorId": {
    41. "terms": {
    42. "field": "visitorId",
    43. "size": 10000,
    44. "order": [{
    45. "_count": "desc"
    46. }, {
    47. "_key": "asc"
    48. }]
    49. },
    50. "aggregations": {
    51. "msgSendTime": {
    52. "top_hits": {
    53. "from": 0,
    54. "size": 1,
    55. "sort": [{
    56. "msgSendTime": {
    57. "order": "desc"
    58. }
    59. }]
    60. }
    61. },
    62. "maxSort": {
    63. "max": {
    64. "field": "msgSendTime"
    65. }
    66. },
    67. "pageAndSort": {
    68. "bucket_sort": {
    69. "sort": [{
    70. "maxSort": {
    71. "order": "desc"
    72. }
    73. }],
    74. "from": 15,
    75. "size": 5
    76. }
    77. }
    78. }
    79. }
    80. }
    81. }

  • 相关阅读:
    解决Qt msvc编译器 中文显示乱码问题
    虚析构+纯虚函数+抽象类+友元
    CORTEX-A系列处理器
    最新百度专用站群seo官网程序源码二级泛程序
    我摊牌了!真正的灰度队列实现方案!全网你都搜不到!
    SQLite 命令行客户端 + HTA 实现简易UI
    动态规划篇——线性DP
    【学习】软件压力测试对软件产品的作用
    var、let和const的区别和用法
    人体分割模型ACE2P与M2FP,解析人脸人体各部件属性,语义化分析
  • 原文地址:https://blog.csdn.net/gbh666666/article/details/126548187