# Create time series with fields date as long mapping

**URL:** <https://discuss.elastic.co/t/create-time-series-with-fields-date-as-long-mapping/262833>\
**Category:** Kibana\
**Created:** [February 1, 2021, 12:38pm UTC](https://discuss.elastic.co/t/create-time-series-with-fields-date-as-long-mapping/262833 "2021-02-01T12:38:49Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Giovanni\_Di\_Lembo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/giovanni_di_lembo/32/45714_2.png) [@Giovanni\_Di\_Lembo](https://discuss.elastic.co/u/Giovanni_Di_Lembo)\
**Post date:** [February 1, 2021, 12:38pm UTC](https://discuss.elastic.co/t/create-time-series-with-fields-date-as-long-mapping/262833/1 "2021-02-01T12:38:49Z")

</div>

Hi all, sorry for my poor english  
I have a index with a eventTime field mapped as long,  
So when I create a time series no date field is found by kibana.  
Is there a way (with a pre script???) to say to kibana "consider that long as date field"? I know that I should reindex the index and do a mapping for that purpose.  
But I wonder if exists a way more easy.  
Regards

---

<div class="post-metadata">

**Author:** ![afharo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/afharo/32/75202_2.png) [@afharo](https://discuss.elastic.co/u/afharo)\
**Post date:** [February 1, 2021, 3:53pm UTC](https://discuss.elastic.co/t/create-time-series-with-fields-date-as-long-mapping/262833/2 "2021-02-01T15:53:33Z")

</div>

Hi @Giovanni_Di_Lembo,

Unfortunately, you can't automatically convert a `long` to a `date` type. However, you can make use of ES's multi-field mappings and set it to `date`:

This request below updates the mappings to add a _sub-field_ `eventTime.date` of type `date`:

```auto
PUT <YOUR_INDEX>/_mappings
{
  "properties": {
    "eventTime": {
      "type": "long",
      "fields": {
        "date": {"type": "date", "format": "epoch_millis"}
      }
    }
  }
}

```

Now you'll be able to create a time-series index-pattern in Kibana using the `eventTime.date` field.

For this change to apply to all your docs, you'll need to force an update so all the documents are reindexed with the new mappings. Running the following request does the job:

```auto
POST <YOUR_INDEX>/_update_by_query
{
  "query": {
    "match_all": {}
  }
}

```

Alternatively, you can define a new index with the correct mapping and reindex all the content of your index to the new index.

---

<div class="post-metadata">

**Author:** ![Giovanni\_Di\_Lembo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/giovanni_di_lembo/32/45714_2.png) [@Giovanni\_Di\_Lembo](https://discuss.elastic.co/u/Giovanni_Di_Lembo)\
**Post date:** [February 11, 2021, 9:05am UTC](https://discuss.elastic.co/t/create-time-series-with-fields-date-as-long-mapping/262833/3 "2021-02-11T09:05:06Z")

</div>

> [@afharo](#):
>
> ```auto
> POST <YOUR_INDEX>/_update_by_query
> {
> "query": {
> "match_all": {}
> }
> }
> 
> ```

The problem is I cannot reindex my docs, I'd like to access field currentDate in this mapping

```
"mappings" : {
  "properties" : {
    "_class" : {
      "type" : "text",
      "fields" : {
        "keyword" : {
          "type" : "keyword",
          "ignore_above" : 256
        }
      }
    },
    "activities" : {
      "type" : "nested",
      "properties" : {
        "uuid" : {
          "type" : "keyword"
        },
        "creationDate" : {
          "type" : "keyword"
        },
        "errorCode" : {
          "type" : "keyword"
        },
        "errorMessage" : {
          "type" : "keyword"
        },
        "jobId" : {
          "type" : "keyword"
        },
        "lastUpdate" : {
          "type" : "keyword"
        },
        "message" : {
          "type" : "keyword"
        },
        "modificationDate" : {
          "type" : "keyword"
        },
        "operationType" : {
          "type" : "keyword"
        }
      }
    }
    
  }
},

```

But actually kibana cannot access it  
I created script as date script

```
doc['activities.creationDate'].value

```

But kibana says

Script is invalid. View script preview for details

---

<div class="post-metadata">

**Author:** ![afharo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/afharo/32/75202_2.png) [@afharo](https://discuss.elastic.co/u/afharo)\
**Post date:** [February 15, 2021, 10:30am UTC](https://discuss.elastic.co/t/create-time-series-with-fields-date-as-long-mapping/262833/4 "2021-02-15T10:30:37Z")

</div>

Oh! I see `activities` is `type: "nested"`, so I assume it's an array of objects.

In that case, you'll have to iterate through all the elements in the array. Also, because it's `nested`, you'll need to access the values from `params._source` instead of `doc`. This is a bit more costly regarding performance.

```auto
List dates = [];

for (int i = 0; i < params._source['activities'].length; ++i){
    def parsedDate = ZonedDateTime.parse(params._source['activities'].get(i)['creationDate']);
    dates.add(parsedDate);
}

return dates;

```

Mind in the script I'm converting my keyword (that looks like `"2020-02-15T10:00:00.000Z"`) to a date. Check out these docs in case your documents have a different format: [Using Datetime in Painless | Painless Scripting Language [7.11] | Elastic](https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-datetime.html)

I should warn you though: since the scripted fields are added after creating the index pattern, Kibana won't treat this index as a timeseries index, and the date picker will likely be disabled in many apps. I hope that's OK for your use case 🙂

---

<div class="post-metadata">

**Author:** ![Giovanni\_Di\_Lembo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/giovanni_di_lembo/32/45714_2.png) [@Giovanni\_Di\_Lembo](https://discuss.elastic.co/u/Giovanni_Di_Lembo)\
**Post date:** [February 16, 2021, 1:53pm UTC](https://discuss.elastic.co/t/create-time-series-with-fields-date-as-long-mapping/262833/5 "2021-02-16T13:53:34Z")

</div>

I've done something similar

```
 List dates = [];
 DateTimeFormatter dtf = DateTimeFormatter.ofPattern("YYYY-MM-dd 'T' HH:mm:ss.SSSZ");
for (int i = 0; i < params._source['activities'].length; ++i){
    def dateTime=params._source['activities'].get(i)['creationDate'];
    def parsedDate = ZonedDateTime.parse(dateTime,dtf);
    dates.add(parsedDate);
}

return dates;

```

And i got

```
"reason": "Text '2021-01-18 T 18:54:10.037+0000' could not be parsed: 
Unable to obtain ZonedDateTime from TemporalAccessor: 
{OffsetSeconds=0, DayOfMonth=18, 
WeekBasedYear[WeekFields[SUNDAY,1]]=2021, MonthOfYear=1},ISO 
resolved to 18:54:10.037 of type java.time.format.Parsed",
```

---

<div class="post-metadata">

**Author:** ![stu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stu/32/75063_2.png) [@stu](https://discuss.elastic.co/u/stu)\
**Post date:** [February 17, 2021, 4:07pm UTC](https://discuss.elastic.co/t/create-time-series-with-fields-date-as-long-mapping/262833/6 "2021-02-17T16:07:48Z")

</div>

> [@Giovanni\_Di\_Lembo](#):
>
> `YYYY-MM-dd 'T' HH:mm:ss.SSSZ`

`YYYY` is [week based year](https://en.wikipedia.org/wiki/ISO_8601#Week_dates), which is not what you want here. Using `yyyy` instead, for [year-of-era](https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html) works.

---

<div class="post-metadata">

**Author:** ![Giovanni\_Di\_Lembo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/giovanni_di_lembo/32/45714_2.png) [@Giovanni\_Di\_Lembo](https://discuss.elastic.co/u/Giovanni_Di_Lembo)\
**Post date:** [February 17, 2021, 4:53pm UTC](https://discuss.elastic.co/t/create-time-series-with-fields-date-as-long-mapping/262833/7 "2021-02-17T16:53:08Z")

</div>

You are the best!! Great 😀

---

<div class="post-metadata">

**Author:** ![Giovanni\_Di\_Lembo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/giovanni_di_lembo/32/45714_2.png) [@Giovanni\_Di\_Lembo](https://discuss.elastic.co/u/Giovanni_Di_Lembo)\
**Post date:** [February 17, 2021, 5:09pm UTC](https://discuss.elastic.co/t/create-time-series-with-fields-date-as-long-mapping/262833/8 "2021-02-17T17:09:30Z")

</div>

> [@Giovanni\_Di\_Lembo](#):
>
> ```auto
> List dates = [];
> DateTimeFormatter dtf = DateTimeFormatter.ofPattern("YYYY-MM-dd 'T' HH:mm:ss.SSSZ");
> for (int i = 0; i < params._source['activities'].length; ++i){
> def dateTime=params._source['activities'].get(i)['creationDate'];
> def parsedDate = ZonedDateTime.parse(dateTime,dtf);
> dates.add(parsedDate);
> }
> 
> return dates;
> 
> ```

I solved syntax of my script with help stu help, but actually it hangs when I try to save

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/1/4/1407e3033282ae37ca1aef1efb8fbe6908e867d1.png)

---

<div class="post-metadata">

**Author:** ![stu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stu/32/75063_2.png) [@stu](https://discuss.elastic.co/u/stu)\
**Post date:** [February 17, 2021, 5:55pm UTC](https://discuss.elastic.co/t/create-time-series-with-fields-date-as-long-mapping/262833/9 "2021-02-17T17:55:57Z")

</div>

Can you check out the network inspector to see what's going on?

---

<div class="post-metadata">

**Author:** ![Giovanni\_Di\_Lembo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/giovanni_di_lembo/32/45714_2.png) [@Giovanni\_Di\_Lembo](https://discuss.elastic.co/u/Giovanni_Di_Lembo)\
**Post date:** [February 17, 2021, 7:08pm UTC](https://discuss.elastic.co/t/create-time-series-with-fields-date-as-long-mapping/262833/10 "2021-02-17T19:08:36Z")

</div>

Got this

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/8/5/85e01b56ada89af4d97a6195d99b86e6e14d8436.png)

---

<div class="post-metadata">

**Author:** ![afharo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/afharo/32/75202_2.png) [@afharo](https://discuss.elastic.co/u/afharo)\
**Post date:** [February 18, 2021, 12:59pm UTC](https://discuss.elastic.co/t/create-time-series-with-fields-date-as-long-mapping/262833/11 "2021-02-18T12:59:11Z")

</div>

Can you try selecting the **Type** to `date`?

---

<div class="post-metadata">

**Author:** ![Giovanni\_Di\_Lembo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/giovanni_di_lembo/32/45714_2.png) [@Giovanni\_Di\_Lembo](https://discuss.elastic.co/u/Giovanni_Di_Lembo)\
**Post date:** [February 18, 2021, 1:05pm UTC](https://discuss.elastic.co/t/create-time-series-with-fields-date-as-long-mapping/262833/12 "2021-02-18T13:05:31Z")

</div>

It is selected

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/d/4/d47074466f2aee470476e775e6002bfa077d9e9f.png)

---

<div class="post-metadata">

**Author:** ![afharo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/afharo/32/75202_2.png) [@afharo](https://discuss.elastic.co/u/afharo)\
**Post date:** [February 18, 2021, 3:26pm UTC](https://discuss.elastic.co/t/create-time-series-with-fields-date-as-long-mapping/262833/13 "2021-02-18T15:26:47Z")

</div>

oh! wait! what are those errors for HTTP requests? `ERR_NAME_NOT_RESOLVED`? Any connectivity issues?

---

<div class="post-metadata">

**Author:** ![Giovanni\_Di\_Lembo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/giovanni_di_lembo/32/45714_2.png) [@Giovanni\_Di\_Lembo](https://discuss.elastic.co/u/Giovanni_Di_Lembo)\
**Post date:** [February 18, 2021, 3:55pm UTC](https://discuss.elastic.co/t/create-time-series-with-fields-date-as-long-mapping/262833/14 "2021-02-18T15:55:30Z")

</div>

> [@Giovanni\_Di\_Lembo](#):
>
> ```auto
> List dates = [];
> DateTimeFormatter dtf = DateTimeFormatter.ofPattern("YYYY-MM-dd 'T' HH:mm:ss.SSSZ");
> for (int i = 0; i < params._source['activities'].length; ++i){
> def dateTime=params._source['activities'].get(i)['creationDate'];
> def parsedDate = ZonedDateTime.parse(dateTime,dtf);
> dates.add(parsedDate);
> }
> 
> return dates;
> 
> ```

No, sorry was a old session picture.  
See this, ti hangs the button

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/b/7/b73ba463966a71a35b2494b74fa443e1a279a16f.png)

---

<div class="post-metadata">

**Author:** ![afharo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/afharo/32/75202_2.png) [@afharo](https://discuss.elastic.co/u/afharo)\
**Post date:** [March 3, 2021, 11:11am UTC](https://discuss.elastic.co/t/create-time-series-with-fields-date-as-long-mapping/262833/15 "2021-03-03T11:11:21Z")

</div>

Looking at the code, it looks like the page fails to remove the field from the previous group type to assign it to the new one `date`. Do you mind trying to create a new field instead?

---

<div class="post-metadata">

**Author:** ![Giovanni\_Di\_Lembo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/giovanni_di_lembo/32/45714_2.png) [@Giovanni\_Di\_Lembo](https://discuss.elastic.co/u/Giovanni_Di_Lembo)\
**Post date:** [March 3, 2021, 11:41am UTC](https://discuss.elastic.co/t/create-time-series-with-fields-date-as-long-mapping/262833/16 "2021-03-03T11:41:35Z")

</div>

I hoped in your script to avoid to create new field (it is mandatory to reindex my data I think)

---

<div class="post-metadata">

**Author:** ![afharo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/afharo/32/75202_2.png) [@afharo](https://discuss.elastic.co/u/afharo)\
**Post date:** [March 3, 2021, 11:45am UTC](https://discuss.elastic.co/t/create-time-series-with-fields-date-as-long-mapping/262833/17 "2021-03-03T11:45:57Z")

</div>

Scripts are not indexed. AFAIK, creating a new scripted field does not require reindexing. Maybe I'm misunderstanding the situation. Can you provide more context and why you think you'll need to reindex if you create a new scripted field?

---

<div class="post-metadata">

**Author:** ![Giovanni\_Di\_Lembo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/giovanni_di_lembo/32/45714_2.png) [@Giovanni\_Di\_Lembo](https://discuss.elastic.co/u/Giovanni_Di_Lembo)\
**Post date:** [March 3, 2021, 1:05pm UTC](https://discuss.elastic.co/t/create-time-series-with-fields-date-as-long-mapping/262833/18 "2021-03-03T13:05:46Z")

</div>

I just need a script field that transform a string mapped field in date field.  
The problem is that string field (in format YYYY-MM-DD HH:MM:SS.SSSS ) is placed in a nested array of object and kibana do no see it when I 'm creating an histogram by date. For 2 reason it is nested and it is not a date  
With your script kibana hangs with previous described error.  
Thanks for your attention

---

<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 31, 2021, 1:05pm UTC](https://discuss.elastic.co/t/create-time-series-with-fields-date-as-long-mapping/262833/19 "2021-03-31T13:05:51Z")

</div>

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