# 7.2.3 날짜 - date

&#x20; Elasticsearch 에서 날짜 타입은 [ISO8601](https://www.iso.org/iso-8601-date-and-time-format.html) 형식을 따라 입력을 합니다. 일반적으로 다음과 같은 형태로 입력된 경우 자동으로 날짜 타입으로 인식이 됩니다.

* "2019-06-12"
* "2019-06-12T17:13:40"
* "2019-06-12T17:13:40+09:00"
* "2019-06-12T17:13:40.428Z"

&#x20; 위와 같은 **ISO8601** 형식이 아니라 **"2019/06/12 12:10:30"** 와 같이 입력하면 보통은 **text**, **keyword** 로 저장됩니다. 이 외에도 **1550282065513** 와 같이 **long** 타입의 정수인 **epoch\_millis** 형태의 입력도 가능합니다. **epoch\_millis** 는 **1970-01-01 00:00:00** 부터의 시간을 밀리초 단위로 카운트 한 값입니다. 필드가 **date** 형으로 정의 된 이후에는 long 타입의 정수를 입력하면 날짜 형태로 저장이 가능합니다. "2019/06/10 12:10:30" 같은 형식으로 날짜를 저장하려면 **format** 옵션을 사용해서 형태를 지정해야 합니다.

&#x20; 다음은 날짜 타입에서 사용 가능한 옵션들입니다.

* `"doc_values"`, `"index"`, `"null_value"`, `"ignore_malformed"` 옵션들은 문자열, 숫자 필드와 기능이 동일합니다.
* `"format" : "<문자열 || 문자열 ...>"` 입력 가능한 날짜 형식을 || 로 구분해서 입력합니다.

&#x20; 다음은 **my\_date** 인덱스에서 **"2019/06/10"**, **"2019-06-10 12:10:30"** 그리고 **epoch\_millis** 형태로 입력받도록 **date\_val** 날짜 필드를 지정하는 예제입니다.

{% code title="my\_date 인덱스 선언" %}

```javascript
PUT my_date
{
  "mappings": {
    "properties": {
      "date_val": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss||yyyy/MM/dd||epoch_millis"
      }
    }
  }
}
```

{% endcode %}

&#x20; long 타입의 정수형은 **epoch\_millis** 외에도 **epoch\_second** 의 사용이 가능합니다. 그 외에도 **basic\_date**, **strict\_date\_time** 등과 같이 미리 정의 된 포맷을 이용하거나, [joda.time.format](https://www.joda.org/joda-time/apidocs/org/joda/time/format/DateTimeFormat.html) 심볼을 사용하여 지정할 수 있습니다.&#x20;

정의된 포맷들은 Elastic 홈페이지의 공식 도큐먼트 [https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html#built-in-date-formats)\
페이지에서 볼 수 있으며, joda 심볼 기호들은 다음과 같습니다.

| 심볼   | 의미                 | 예) 2019-09-12T17:13:07.428+09:00 |
| ---- | ------------------ | -------------------------------- |
| yyyy | 년도                 | 2019                             |
| MM   | 월 - 숫자             | 09                               |
| MMM  | 월 - 문자 (3자리)       | Sep                              |
| MMMM | 월 - 문자 (전체)        | September                        |
| dd   | 일                  | 12                               |
| a    | 오전 / 오후            | PM                               |
| HH   | 시각 (0\~23)         | 17                               |
| kk   | 시각 (01\~24)        | 17                               |
| hh   | 시각 (01\~12)        | 05                               |
| h    | 시각 (1\~12)         | 5                                |
| mm   | 분 (00\~59)         | 13                               |
| m    | 분 (0\~59)          | 13                               |
| ss   | 초 (00\~59)         | 07                               |
| s    | 초 (0\~59)          | 7                                |
| SSS  | 밀리초                | 428                              |
| Z    | 타임존                | +0900 / +09:00                   |
| e    | 요일 (숫자 1:월 \~ 7:일) | 4                                |
| E    | 요일 (텍스트)           | Thu                              |

&#x20; 날짜 필드는 입력된 값들을 실제로 내부에서는 모두 **long** 형태의 **epoch\_millis** 로 저장합니다. 또한 매핑의 **format** 형식만 지정 해 놓으면 지정된 어떤 형식으로도 색인 및 쿼리가 가능합니다. 다시 말해 \_source 의 날짜 는 **"2019-09-12"** 형식으로 입력 되었어도 **"2019/09/12"** 형식으로 range 쿼리를 해도 정상적으로 동작합니다. [range 쿼리](/05-search/5.6-range.md)는 5장 검색에서 확인 하시기 바랍니다.

&#x20; 다음은 앞에서 선언한 **my\_date** 인덱스에 `"date_val": "2019-09-12 15:01:23"` 인 도큐먼트를 입력하고 "2019/09/10" 보다 크고 "2019-09-13 12:00:00" 의 epoch\_millis 값인 1568332800000 보다 작은 값을 검색하는 쿼리입니다.

{% code title=""yyyy-MM-dd HH:mm:ss" 형식으로 날짜값 입력" %}

```javascript
PUT my_date/_doc/1
{
  "date_val": "2019-09-12 15:01:23"
}
```

{% endcode %}

{% tabs %}
{% tab title="request" %}
{% code title=""yyyy/MM/dd", epoch\_millis 형식으로 날짜 검색" %}

```javascript
GET my_date/_search
{
  "query": {
    "range": {
      "date_val": {
        "gt": "2019/09/10",
        "lt": 1568332800000
      }
    }
  }
}
```

{% endcode %}
{% endtab %}

{% tab title="response" %}
{% code title=""yyyy/MM/dd", epoch\_millis 형식으로 날짜 검색 결과" %}

```javascript
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "my_date",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "date_val" : "2019-09-12 15:01:23"
        }
      }
    ]
  }
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

&#x20; 입력된 포맷과 검색 쿼리 포맷이 달라도 정상적으로 검색되는 것을 확인할 수 있습니다.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://esbook.kimjmin.net/07-settings-and-mappings/7.2-mappings/7.2.3-date.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
