elastic search - 날짜 범위 별 쿼리

짱구석·2022년 10월 14일
0

elastice search

목록 보기
1/1
post-thumbnail
  • 하루 전, 일주일 전 row 개수 가져오기

QUERY

GET my_index/_search
{
  "aggs": {
    "byDate": {
      "date_range": { 
          "field": "createdAt",
          "ranges": [
              {"from": "now-1w", "to": "now", "key": "week" },
              {"from": "now-1d", "to": "now", "key": "today"}
          ],
          "keyed": true // 키 네이밍을 가능하게 해줌
      }
    }
  }
}

RESPONSE

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 14,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "byDate" : {
      "buckets" : {
        "week" : {
          "from" : 1.665159166158E12,
          "from_as_string" : "2022-10-07T16:12:46.158Z",
          "to" : 1.665763966158E12,
          "to_as_string" : "2022-10-14T16:12:46.158Z",
          "doc_count" : 14
        },
        "today" : {
          "from" : 1.665677566158E12,
          "from_as_string" : "2022-10-13T16:12:46.158Z",
          "to" : 1.665763966158E12,
          "to_as_string" : "2022-10-14T16:12:46.158Z",
          "doc_count" : 3
        }
      }
    }
  }
}

0개의 댓글