# Ingest pipeline analyze time difference

**URL:** <https://discuss.elastic.co/t/ingest-pipeline-analyze-time-difference/207140>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [November 8, 2019, 4:07pm UTC](https://discuss.elastic.co/t/ingest-pipeline-analyze-time-difference/207140 "2019-11-08T16:07:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrii](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrii/32/78646_2.png) [@Andrii](https://discuss.elastic.co/u/Andrii)\
**Post date:** [November 8, 2019, 4:08pm UTC](https://discuss.elastic.co/t/ingest-pipeline-analyze-time-difference/207140/1 "2019-11-08T16:08:00Z")

</div>

Hello,

I need to route messages which have @timestamp field that differs more than 8 hours from now to a separate topic.  
I tried to use ingest pipeline with some processors for that. But the only one which comes to my mind is a script processor.  
Have anyone used a script processor for such modification/routing?

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [November 8, 2019, 7:27pm UTC](https://discuss.elastic.co/t/ingest-pipeline-analyze-time-difference/207140/2 "2019-11-08T19:27:44Z")

</div>

I assume that by "topic" you mean "index"?

I would use a combination of two `set` processors. One that sets the value of a field to the current time, and one that conditionally sets the index if the time difference between the two timestamps is more than 8 hours:

```auto
POST /_ingest/pipeline/_simulate
{
  "pipeline": {
    "processors": [
      {
        "set": {
          "field": "ingest_time",
          "value": "{{_ingest.timestamp}}"
        }
      },
      {
        "set": {
          "field": "_index",
          "value": "some_other_index",
          "if": "ChronoUnit.HOURS.between(ZonedDateTime.parse(ctx['@timestamp']), ZonedDateTime.parse(ctx['ingest_time'])) > 8"
        }
      }
    ]
  },
  "docs": [
    {
      "_source": {
        "@timestamp": "2019-11-07T20:39:00.000Z"
      }
    }
  ]
}

```

---

<div class="post-metadata">

**Author:** ![Andrii](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrii/32/78646_2.png) [@Andrii](https://discuss.elastic.co/u/Andrii)\
**Post date:** [November 9, 2019, 9:35am UTC](https://discuss.elastic.co/t/ingest-pipeline-analyze-time-difference/207140/3 "2019-11-09T09:35:47Z")

</div>

yeah, I meant index.  
Thanks a lot @abdon that is exactly what I looked for.

---

<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:** [December 7, 2019, 9:35am UTC](https://discuss.elastic.co/t/ingest-pipeline-analyze-time-difference/207140/4 "2019-12-07T09:35:48Z")

</div>

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