date_nanos 필드 타입

date_nanos 필드 타입

도입 1.0

date_nanos 필드 타입은 date 필드 타입과 마찬가지로 날짜를 저장해요. 다만 date는 밀리초 단위로 날짜를 저장하는 반면, date_nanos는 나노초 단위로 날짜를 저장해요. 날짜는 epoch 이후의 나노초에 해당하는 long 값으로 저장되며, 따라서 지원되는 날짜 범위는 대략 1970~2262년이에요.

date_nanos 필드에 대한 쿼리는 필드 값의 long 표현에 대한 범위 쿼리로 변환되어요. 그런 다음 저장된 필드와 집계 결과는 필드에 설정된 format을 사용해 문자열로 변환돼요.

date_nanos 필드는 date가 지원하는 모든 형식과 파라미터를 지원해요. ||로 구분하여 여러 형식을 사용할 수 있어요.

date_nanos 필드의 경우 strict_date_optional_time_nanos 형식을 사용해 나노초 단위의 정밀도를 유지할 수 있어요. 필드를 date_nanos로 매핑할 때 형식을 지정하지 않으면 기본 형식은 strict_date_optional_time||epoch_millis로, strict_date_optional_time 또는 epoch_millis 형식 중 하나로 값을 전달할 수 있어요. strict_date_optional_time 형식은 나노초 단위의 날짜를 지원하지만 epoch_millis 형식은 밀리초 단위의 날짜만 지원해요.

출처: 문서

본문

예제

strict_date_optional_time_nanos 형식을 갖는 타입이 date_nanos인 date 필드를 사용해 매핑을 만들어 볼게요.

PUT testindex/_mapping
{
  "properties": {
      "date": {
        "type": "date_nanos",
        "format" : "strict_date_optional_time_nanos"
      }
    }
}

인덱스에 두 문서를 색인해요.

PUT testindex/_doc/1
{ "date": "2022-06-15T10:12:52.382719622Z" }

PUT testindex/_doc/2
{ "date": "2022-06-15T10:12:52.382719624Z" }

범위 쿼리를 사용해 날짜 범위를 검색할 수 있어요.

GET testindex/_search
{
  "query": {
    "range": {
      "date": {
        "gte": "2022-06-15T10:12:52.382719621Z",
        "lte": "2022-06-15T10:12:52.382719623Z"
      }
    }
  }
}

응답에는 지정된 범위 안에 있는 날짜의 문서가 포함돼요.

{
  "took": 43,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "testindex",
        "_id": "1",
        "_score": 1,
        "_source": {
          "date": "2022-06-15T10:12:52.382719622Z"
        }
      }
    ]
  }
}

date_nanos 필드가 있는 문서를 쿼리할 때는 fields 또는 docvalue_fields를 사용할 수 있어요.

GET testindex/_search
{
  "fields": ["date"]
}

GET testindex/_search
{
  "docvalue_fields" : [
    {
      "field" : "date"
    }
  ]
}

앞선 두 쿼리 중 어느 쪽에 대한 응답에도 두 문서가 모두 포함돼요.

{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 2,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "testindex",
        "_id": "1",
        "_score": 1,
        "_source": {
          "date": "2022-06-15T10:12:52.382719622Z"
        },
        "fields": {
          "date": [
            "2022-06-15T10:12:52.382719622Z"
          ]
        }
      },
      {
        "_index": "testindex",
        "_id": "2",
        "_score": 1,
        "_source": {
          "date": "2022-06-15T10:12:52.382719624Z"
        },
        "fields": {
          "date": [
            "2022-06-15T10:12:52.382719624Z"
          ]
        }
      }
    ]
  }
}

다음과 같이 date_nanos 필드를 기준으로 정렬할 수 있어요.

GET testindex/_search
{
  "sort": { 
    "date": "asc"
  } 
}

응답에는 정렬된 문서가 포함돼요.

{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 2,
      "relation": "eq"
    },
    "max_score": null,
    "hits": [
      {
        "_index": "testindex",
        "_id": "1",
        "_score": null,
        "_source": {
          "date": "2022-06-15T10:12:52.382719622Z"
        },
        "sort": [
          1655287972382719700
        ]
      },
      {
        "_index": "testindex",
        "_id": "2",
        "_score": null,
        "_source": {
          "date": "2022-06-15T10:12:52.382719624Z"
        },
        "sort": [
          1655287972382719700
        ]
      }
    ]
  }
}

Painless 스크립트를 사용해 필드의 나노초 부분에 접근할 수도 있어요.

GET testindex/_search
{
  "script_fields" : {
    "my_field" : {
      "script" : {
        "lang" : "painless",
        "source" : "doc['date'].value.nano" 
      }
    }
  }
}

응답에는 필드의 나노초 부분만 포함돼요.

{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 2,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "testindex",
        "_id": "1",
        "_score": 1,
        "fields": {
          "my_field": [
            382719622
          ]
        }
      },
      {
        "_index": "testindex",
        "_id": "2",
        "_score": 1,
        "fields": {
          "my_field": [
            382719624
          ]
        }
      }
    ]
  }
}

파생 소스 (Derived source)

인덱스가 파생 소스를 사용하면 OpenSearch는 소스를 재구성하는 동안 다중 값 date 필드의 값을 정렬할 수 있어요. format 매핑 파라미터 아래에서 ||로 구분된 여러 date 형식을 구성하는 경우, 파생 소스는 첫 번째 제공된 형식의 결과를 반환해요.

파생 소스를 활성화하고 여러 형식으로 date_nanos 필드를 구성하는 인덱스를 만들어 볼게요.

PUT sample-index1
{
  "settings": {
    "index": {
      "derived_source": {
        "enabled": true
      }
    }
  },
  "mappings": {
    "properties": {
      "date_nanos": {
        "type": "date_nanos",
        "format": "strict_date_optional_time_nanos||strict_date_optional_time||epoch_millis"
      }
    }
  }
}

혼합된 날짜 형식을 가진 문서를 인덱스에 색인해요.

PUT sample-index1/_doc/1
{
  "date_nanos": [1758504860, "2025-09-22T00:34", "2025-09-22T01:34:20Z"]
}

OpenSearch가 _source를 재구성한 후 파생 _source는 다음과 같아요.

{
  "date_nanos": ["2025-09-22T00:34:00.000000000Z", "2025-09-22T01:34:00.000000000Z", "2025-09-22T01:34:00.000000000Z"]
}

출처: 문서

더 알아보기 (Learn more)