如何过滤Elasticsearch中的连续天数?

时间:2018-11-18 03:26:46

标签: elasticsearch

这是我的情况,我想找到连续登录我们网站等于或大于3天的用户。 例如:

{"login_time":"2018-01-01T18:19:07.982Z", "user_id":123}
{"login_time":"2018-01-01T08:30:07.982Z", "user_id":456}
{"login_time":"2018-01-02T09:39:07.982Z", "user_id":123}
{"login_time":"2018-01-03T08:20:07.982Z", "user_id":123}
{"login_time":"2018-01-03T08:20:07.982Z", "user_id":456}

因此,user_id:123已经连续登录了3天,而user_id:456已经连续登录了1天,我希望在Elasticsearch返回时可以删除user_id:456。

这是我的ES JSON:

GET event-tracking/_search
{
  "aggs": {
    "login_by_day": {
      "date_histogram": {
        "field": "login_time",
        "interval": "day"
      }, 
      "aggs": {
        "user_id": {
          "terms": {
            "field": "user_id",
            "size": 10
          }
        }
      }
    }
  }
}

然后回应:

"aggregations": {
    "login_by_day": {
      "buckets": [
        {
          "key_as_string": "2018-01-01T00:00:00.000Z",
          "key": 1514764800000,
          "doc_count": 2,
          "user_id": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": 123,
                "doc_count": 1
              },
              {
                "key": 456,
                "doc_count": 1
              }
            ]
          }
        },
        {
          "key_as_string": "2018-01-02T00:00:00.000Z",
          "key": 1514851200000,
          "doc_count": 1,
          "user_id": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": 123,
                "doc_count": 1
              }
            ]
          }
        },
        {
          "key_as_string": "2018-01-03T00:00:00.000Z",
          "key": 1514937600000,
          "doc_count": 2,
          "user_id": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": 123,
                "doc_count": 1
              },
              {
                "key": 456,
                "doc_count": 1
              }
            ]
          }
        }
      ]
    }
  }

然后,我必须编写一些代码来过滤结果。我的问题是如何使用ES JSON无需任何代码即可到达它。

谢谢。

0 个答案:

没有答案