Elasticsearch

[Elasticsearch] Data Math 개념과 계산하는 방법

알로호모라 2022. 10. 24. 11:35
반응형

Elasticsearch range queries에서 또는 datarange aggregations에서 사용되는 Date Math 알아보기

 

 

1) 기본 단위들 

기본 단위들은 아래와 같다.

supported units 
y Years
M Months
w Weeks
d Days
h Hours
H Hours
m Minutes
s Seconds

 

그리고 위의 단위들로 + - 기호를 사용해서 날짜를 계산할 수 있다. 

 

 

 

2) 예시 

ex) now == 2023-01-01 12:01:01 라고 할 때, 

계산식 풀이 결과
now+1h now + 1 시간 2023-01-01 13:01:01
now-1h now - 1 시간 2023-01-01 11:01:01
now/m rounded down to the start of the minute 2023-01-01 12:01:00
now/H rounded down to the start of the hour 2023-01-01 12:00:00
now/d rounded down to the start of the day  2023-01-01 00:00:00
now-1h/d now - 1 시간을 해주고 "d"를 기준으로 round down 해주기 (to the start of the month) 2023-01-01 00:00:00 
now/d+1h day 기준 round down 후, 1시간 플러스 2023-01-01 01:00:00
now/d-12h day 기준 round down 후, 12시간 마이너스 2022-12-31 12:00:00

 

 

example) 어제부터 오늘까지 검색 시 

              range: {
                date: {
                  gte: 'now-1d/d',
                  lt: 'now',
                },
              },

 

위와같이 적어줄 수 있다. 

반응형