ES--top_hits的简单使用获取最早进 最晚出的数据

  • 需求:
    业务 需要知道工人打卡记录最早进 与 最早出 一天记录原始信息,现通过 top_hits来实现
  • 如下:
GET /hw-attendance-2022-3month/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "record_time": {
              "gte": "2022-03-07T00:00:00",
              "lte": "2022-03-08T00:00:00"
            }
          }
        },
        {
          "match":{
            "idcardno.keyword":"53212*******1613"
          }
        }
      ]
      
    }
  }
  ,"size":0,
  "aggs": {
    "groupBy_idcardno": {
      "terms": { --对人员身份证进行分桶
        "field": "idcardno.keyword",
        "size": 50
      },
      "aggs": {
        "groupBy_recordtime":{
          "date_histogram": { --对人员打卡日期,以天为单位进行分桶
            "field": "record_time",
            "calendar_interval": "day",
            "format": "yyyy-MM-dd" ,
            "min_doc_count" : 0
          },
            "aggs": {
              "idcard_recordTime_top_last": {--通过top_hits排序获取最晚出的记录
                "top_hits": {
                  "sort": [
                    { 
                      "record_time": {
                        "order": "desc"
                      }
                    }
                  ], 
                "size": 1
                
              }
            },
            "idcard_recordTime_earliest": {--通过top_hits排序获取最早进的记录
                "top_hits": {
                  "sort": [
                    { 
                      "record_time": {
                        "order": "asc"
                      }
                    }
                  ], 
                "size": 1
                
              }
            }
          }
        }
      }
      
    }
  }

}
  • 结果
{
  "took" : 69,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 281,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "record_date_partiton" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "532122*******1613",
          "doc_count" : 281,
          "groupBy_recordtime" : {
            "buckets" : [
              {
                "key_as_string" : "2022-03-07",
                "key" : 1646611200000,
                "doc_count" : 281,
                "idcard_recordTime_earliest" : {
                  "hits" : {
                    "total" : {
                      "value" : 281,
                      "relation" : "eq"
                    },
                    "max_score" : null,
                    "hits" : [
                      {
                        "_index" : "hw-attendance-2022-3month",
                        "_type" : "_doc",
                        "_id" : "PFywaH8BGUfFSK8ILMmm",
                        "_score" : null,
                        "_source" : {
                          "app_key" : "8134",
                          "sn" : "5437405391420012470I",
                          "equp_name" : "Other",
                          "pid" : 0,
                          "record_time" : "2022-03-07T06:04:17",
                          "full_name" : "赵***",
                          "photo_path" : "",
                          "idcardno" : "532*********1613",
                          "mobile" : "",
                          "tid" : "0",
                          "direct" : 1,
                          "insert_time" : "2022-03-08T16:40:50"
                        },
                        "sort" : [
                          1646633057000
                        ]
                      }
                    ]
                  }
                },
                "idcard_recordTime_top_last" : {
                  "hits" : {
                    "total" : {
                      "value" : 281,
                      "relation" : "eq"
                    },
                    "max_score" : null,
                    "hits" : [
                      {
                        "_index" : "hw-attendance-2022-3month",
                        "_type" : "_doc",
                        "_id" : "qIOSZ38BPDXhbVpyFFgY",
                        "_score" : null,
                        "_source" : {
                          "app_key" : "8134",
                          "sn" : "5437405391420012470I",
                          "equp_name" : "Other",
                          "pid" : 0,
                          "record_time" : "2022-03-07T17:35:49",
                          "full_name" : "赵**",
                          "photo_path" : "",
                          "idcardno" : "5321**********613",
                          "mobile" : "",
                          "tid" : "0",
                          "direct" : 1,
                          "insert_time" : "2022-03-08T11:28:20"
                        },
                        "sort" : [
                          1646674549000
                        ]
                      }
                    ]
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值