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

该博客展示了如何使用 Elasticsearch 的 top_hits 聚合来获取指定日期范围内特定员工的最早和最晚考勤打卡记录。查询示例中,通过对 idcardno 和 record_time 进行过滤和分桶,成功地检索到了一天内员工的最早进与最晚出记录。
摘要由CSDN通过智能技术生成
  • 需求:
    业务 需要知道考勤打卡记录最早进 与 最早出 一天记录原始信息,现通过 top_hits来实现
  • 如下:
GET /att_index/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "record_time": {
              "gte": "2022-03-07T00:00:00",
              "lte": "2022-03-08T00:00:00"
            }
          }
        },
        {
          "match":{
            "idcardno.keyword":"5*******"
          }
        }
      ]
      
    }
  }
  ,"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" : "atte",
                        "_type" : "_doc",
                        "_id" : "PFywaH8BGUfFSK8ILMmm",
                        "_score" : null,
                        "_source" : {
                          
                         .....
                          "record_time" : "2022-03-07T06:04:17",
                         
                        ......
                          "idcardno" : "5*********",

                          "direct" : 1,
                        
                        },
                        "sort" : [
                          1646633057000
                        ]
                      }
                    ]
                  }
                },
                "idcard_recordTime_top_last" : {
                  "hits" : {
                    "total" : {
                      "value" : 281,
                      "relation" : "eq"
                    },
                    "max_score" : null,
                    "hits" : [
                      {
                        "_index" : "att",
                        "_type" : "_doc",
                        "_id" : "qIOSZ38BPDXhbVpyFFgY",
                        "_score" : null,
                        "_source" : {
                       .......
                          "record_time" : "2022-03-07T17:35:49",
                       ........
                          "idcardno" : "5**********",
                      ......
                          "direct" : 1,
                        
                        },
                        "sort" : [
                          1646674549000
                        ]
                      }
                    ]
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值