# Date型をepochミリ秒で取得することはできますか

**URL:** <https://discuss.elastic.co/t/date-epoch/167569>\
**Category:** 日本語による質問・議論はこちら\
**Created:** [February 8, 2019, 5:03am UTC](https://discuss.elastic.co/t/date-epoch/167569 "2019-02-08T05:03:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tsukahara](https://avatars.discourse-cdn.com/v4/letter/t/cab0a1/32.png) [@tsukahara](https://discuss.elastic.co/u/tsukahara)\
**Post date:** [February 8, 2019, 5:03am UTC](https://discuss.elastic.co/t/date-epoch/167569/1 "2019-02-08T05:03:41Z")

</div>

現在 Elasticsearch6.2.2を使用しております。  
データの登録時にdate型を"yyyy/mm/dd"などで登録したデータに対して、  
検索結果のdate型の値を epochミリ秒で取得する方法ありますでしょうか。

1文書のみであればaggsを使うとepochミリ秒が取れることがわかったのですが、  
検索結果（\_sourceの箇所）として取る方法があれば教えていただきたく  
質問させていただきました。

データ登録例

```
POST aaa/type/1?refresh=true
{
  "cdate": "2019/02/08"
}

```

検索例

```
GET aaa/_search
{
  "aggs": {
    "cdate": {
      "max": {
        "field": "cdate"
      }
    }
  }
}

```

検索結果

```
        "_source": {
          "cdate": "2019/02/08"
        }
      }
    ]
  },
  "aggregations": {
    "cdate": {
      "value": 1549584000000,
      "value_as_string": "2019/02/08 00:00:00"
    }
  }
```

---

<div class="post-metadata">

**Author:** ![tsgkdt](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tsgkdt/32/39151_2.png) [@tsgkdt](https://discuss.elastic.co/u/tsgkdt)\
**Post date:** [February 8, 2019, 6:38am UTC](https://discuss.elastic.co/t/date-epoch/167569/2 "2019-02-08T06:38:25Z")

</div>

script fieldを利用するのはどうでしょう？

データ登録

```auto
PUT forum0208/_doc/1
{
  "title": "epochでほしい",
  "date": "2019-02-08T14:37:15+0900"
}

```

クエリ

```auto
GET forum0208/_search
{
  "query": {
    "match_all": {}
  },
  "script_fields": {
    "converted_date1": {
      "script": {
        "lang": "painless",
        "source": "doc['date'].value.toInstant().toEpochMilli()"
      }
    }
  }
}

```

結果

```auto
{
  "took" : 7,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "forum0208",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "fields" : {
          "converted_date1" : [
            1549604235000
          ]
        }
      }
    ]
  }
}

```

---

<div class="post-metadata">

**Author:** ![tsukahara](https://avatars.discourse-cdn.com/v4/letter/t/cab0a1/32.png) [@tsukahara](https://discuss.elastic.co/u/tsukahara)\
**Post date:** [February 8, 2019, 8:42am UTC](https://discuss.elastic.co/t/date-epoch/167569/3 "2019-02-08T08:42:19Z")

</div>

早速の回答ありがとうございます。  
いただいた例で試してみたのですが、こちらの環境ですと以下のエラーとなってしまいました。

```
    "reason": {
      "type": "script_exception",
      "reason": "runtime error",
      "script_stack": [
        "doc['date'].value.toInstant().toEpochMilli()",
        " ^---- HERE"
      ],
      "script": "doc['date'].value.toInstant().toEpochMilli()",
      "lang": "painless",
      "caused_by": {
        "type": "illegal_argument_exception",
        "reason": "Unable to find dynamic method [toInstant] with [0] arguments for class [org.joda.time.MutableDateTime]."
      }
    }

```

以下とすることでepochミリ秒が取得できましたので  
scriptを利用したいと思います。

```
    "source": "doc['date'].value.toInstant().toEpochMilli()"
    ↓
    "source": "doc['cdate'].value.getMillis()"

```

ありがとうございます。

---

<div class="post-metadata">

**Author:** ![tsgkdt](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tsgkdt/32/39151_2.png) [@tsgkdt](https://discuss.elastic.co/u/tsgkdt)\
**Post date:** [February 8, 2019, 12:07pm UTC](https://discuss.elastic.co/t/date-epoch/167569/4 "2019-02-08T12:07:34Z")

</div>

バージョンが6.2.2であるところを、うっかり見落としておりました。

最新の6.6.0ですと、getMillisはdeprecatedとなっておりますので、今後のバージョンアップの際はご留意いただくと良いと思いますー

Query

```auto
GET forum0208/_search
{
  "query": {
    "match_all": {}
  },
  "script_fields": {
    "converted_date1": {
      "script": {
        "lang": "painless",
        "source": "doc['date'].value.getMillis()"
      }
    }
  }
}

```

とすると、deprecatedログが出る。

```auto
#! Deprecation: Use of the joda time method [getMillis()] is deprecated. Use [toInstant().toEpochMilli()] instead.

```

ご参考まで。

---

<div class="post-metadata">

**Author:** ![tsukahara](https://avatars.discourse-cdn.com/v4/letter/t/cab0a1/32.png) [@tsukahara](https://discuss.elastic.co/u/tsukahara)\
**Post date:** [February 8, 2019, 4:44pm UTC](https://discuss.elastic.co/t/date-epoch/167569/5 "2019-02-08T16:44:05Z")

</div>

情報ありがとうございます。  
新しいバージョンでは違う呼び出し方になっているのですね。  
機会を見てバージョンアップ（7以降かな）したいと思います。

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 8, 2019, 4:44pm UTC](https://discuss.elastic.co/t/date-epoch/167569/6 "2019-03-08T16:44:09Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
