# Timefieldが一つ前のドキュメントとの差分を取得したい

**URL:** <https://discuss.elastic.co/t/timefield/214542>\
**Category:** 日本語による質問・議論はこちら\
**Created:** [January 10, 2020, 6:15am UTC](https://discuss.elastic.co/t/timefield/214542 "2020-01-10T06:15:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sshota](https://avatars.discourse-cdn.com/v4/letter/s/f9ae1b/32.png) [@sshota](https://discuss.elastic.co/u/sshota)\
**Post date:** [January 10, 2020, 6:15am UTC](https://discuss.elastic.co/t/timefield/214542/1 "2020-01-10T06:15:01Z")

</div>

お世話になります。

あるフィールドにおいて、同一index内のtimefieldが一つ前のドキュメントとの差分を計算して取得したいのですが、そのようなことは可能でしょうか。

scripted field を使用して計算することなどを模索してみたのですが、同一ドキュメント内で異なるフィールドの差分などは取れそうでしたが、別ドキュメントの同じフィールドの差分は取れそうにありませんでした。

## やりたいことのイメージ

たとえば下記のようなデータ群で、valueフィールドに対してtimefiledが一つ前のドキュメントとの差分を取りたい場合の期待値は以下のとおりとなります。

| \_id | 期待値 (ひとつ前のドキュメントとの差分) |
| --- | --- |
| 1 | なし |
| 2 | 10 ( =20-10) |
| 3 | 80 ( =100-20) |

```auto
      {
        "_index": "sample-index",
        "_type": "sample-type",
        "_id": "1",
        "_score": 1,
        "_source": {
          "title": "testdata-1",
          "timefiled": "2020-01-01T00:00:00+09:00",
          "value": 10
        }
      },
            {
        "_index": "sample-index",
        "_type": "sample-type",
        "_id": "2",
        "_score": 1,
        "_source": {
          "title": "testdata-2",
          "timefiled": "2020-01-02T00:00:00+09:00",
          "value": 20
        }
      },
      {
        "_index": "sample-index",
        "_type": "sample-type",
        "_id": "3",
        "_score": 1,
        "_source": {
          "title": "testdata-3",
          "timefiled": "2020-01-03T00:00:00+09:00",
          "value": 100
        }
      }

```

## 動作環境

- Elasticsearch 7.3.0
- Kibana 7.3.0

ご教示いただけますと幸いです。  
どうぞ宜しくお願い致します。

---

<div class="post-metadata">

**Author:** ![Koji\_Kawamura](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/koji_kawamura/32/93938_2.png) [@Koji\_Kawamura](https://discuss.elastic.co/u/Koji_Kawamura)\
**Post date:** [February 6, 2020, 8:29am UTC](https://discuss.elastic.co/t/timefield/214542/2 "2020-02-06T08:29:45Z")

</div>

@sshota さん、

`Serial Differencing Aggregation` などはもう試されたでしょうか？  
ぱっと見、用途にマッチするように思えます。  
[https://www.elastic.co/guide/en/elasticsearch/reference/7.5/search-aggregations-pipeline-serialdiff-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/7.5/search-aggregations-pipeline-serialdiff-aggregation.html)

---

<div class="post-metadata">

**Author:** ![sshota](https://avatars.discourse-cdn.com/v4/letter/s/f9ae1b/32.png) [@sshota](https://discuss.elastic.co/u/sshota)\
**Post date:** [February 18, 2020, 4:42am UTC](https://discuss.elastic.co/t/timefield/214542/3 "2020-02-18T04:42:22Z")

</div>

@Koji_Kawamura さん

お礼が遅くなり申し訳ないです。  
ご教示いただきありがとうございました。

教えていただいた内容で、やりたいことがクリアできそうです。  
`Serial Differencing Aggregation`は知らなかったので大変参考になりました。

まだ試しに触ってみた程度なので、いろいろと不明点は出てきそうですが、  
実現したかったことはクリアになりましたので、本トピックはクローズします。

ありがとうございました。

---

<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 17, 2020, 4:42am UTC](https://discuss.elastic.co/t/timefield/214542/4 "2020-03-17T04:42:22Z")

</div>

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