# Accessing date field in a native (Java) script

**URL:** <https://discuss.elastic.co/t/accessing-date-field-in-a-native-java-script/16230>\
**Category:** Elasticsearch\
**Created:** [March 7, 2014, 3:02pm UTC](https://discuss.elastic.co/t/accessing-date-field-in-a-native-java-script/16230 "2014-03-07T15:02:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Niccolo\_Becchi](https://avatars.discourse-cdn.com/v4/letter/n/8edcca/32.png) [@Niccolo\_Becchi](https://discuss.elastic.co/u/Niccolo_Becchi)\
**Post date:** [March 7, 2014, 3:02pm UTC](https://discuss.elastic.co/t/accessing-date-field-in-a-native-java-script/16230/1 "2014-03-07T15:02:48Z")

</div>

Hi,  
I haven't been able to find any examples about how to access to a date  
field inside a java native script.  
I have a 'datePublished' field defined as following in the mapping:

mappings: {

- doc: {
  - properties: {
    - ...
    - datePublished: {
      - format: dateOptionalTime
      - type: date  
}  
}  
}

}

However, inside the class:

public class ScoreByAgeScript extends AbstractDoubleSearchScript {

```
public ScoreByAgeScript(@Nullable Map<String,Object> params) {
    ...
}

@Override
public double runAsDouble() {
        Object datePublishedStr = source().get(fieldDate); // => Get a 

```

string 2013-07-07T12:00:00  
Object datePublishedDoc = doc().get(fieldDate); // =\> Get a  
list of strings [00, 03, 07t12, 2013]  
}

}

I have been able only to the string text.  
I could eventually parse the text to a Date Java object but I would prefer  
to avoid it, avoiding any problem about string format or timezone, etc..  
Is there any way to get the java date Object?  
Thanks  
Niccolo

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/37cfcecc-400a-4344-95fb-77ab6509520a%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/37cfcecc-400a-4344-95fb-77ab6509520a%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Binh\_Ly\_2](https://avatars.discourse-cdn.com/v4/letter/b/d07c76/32.png) [@Binh\_Ly\_2](https://discuss.elastic.co/u/Binh_Ly_2)\
**Post date:** [March 10, 2014, 6:22pm UTC](https://discuss.elastic.co/t/accessing-date-field-in-a-native-java-script/16230/2 "2014-03-10T18:22:23Z")

</div>

If you do something like this, you should get the epoch value in  
milliseconds. Then you can use that value to initialize whatever object you  
want:

```
          ScriptDocValues v = (ScriptDocValues) doc().get(dateField);
          if (v != null && !v.isEmpty()) {
            long epoch_ms = ((Longs)v).getValue();
          }

```

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/f746d44b-f9dd-4c65-a4af-f653fb8e86c8%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/f746d44b-f9dd-4c65-a4af-f653fb8e86c8%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Niccolo\_Becchi](https://avatars.discourse-cdn.com/v4/letter/n/8edcca/32.png) [@Niccolo\_Becchi](https://discuss.elastic.co/u/Niccolo_Becchi)\
**Post date:** [March 31, 2014, 9:18am UTC](https://discuss.elastic.co/t/accessing-date-field-in-a-native-java-script/16230/3 "2014-03-31T09:18:06Z")

</div>

Sorry Bihn,  
just seen now your suggestion but It doesn't work.  
For a field\* "datePublished: {format: dateOptionalTime, type: date}"\* when  
I perform:

ScriptDocValues v = (ScriptDocValues) doc().get(dateField);

I got an object  
org.elasticsearch.index.fielddata.ScriptDocValues$Strings@5e834b36 that's  
containing the following strings:

[01, 12, 15z, 2014, 30t10]

For the following date 2014-01-30T10:12:15Z stored in ES. I don't know how  
to fix it.  
Thanks a lot  
Niccolo'

Il giorno lunedì 10 marzo 2014 18:22:23 UTC, Binh Ly ha scritto:

> If you do something like this, you should get the epoch value in  
> milliseconds. Then you can use that value to initialize whatever object you  
> want:
> 
> ```
> ScriptDocValues v = (ScriptDocValues) doc().get(dateField);
> if (v != null && !v.isEmpty()) {
> long epoch_ms = ((Longs)v).getValue();
> }
> 
> ```

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/deb72f15-f765-4287-b073-b9ddce1ef7fa%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/deb72f15-f765-4287-b073-b9ddce1ef7fa%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:** [July 6, 2017, 1:39am UTC](https://discuss.elastic.co/t/accessing-date-field-in-a-native-java-script/16230/4 "2017-07-06T01:39:26Z")

</div>


