# scriptField in TermsFacet to format a date value

**URL:** <https://discuss.elastic.co/t/scriptfield-in-termsfacet-to-format-a-date-value/13433>\
**Category:** Elasticsearch\
**Created:** [September 2, 2013, 1:54pm UTC](https://discuss.elastic.co/t/scriptfield-in-termsfacet-to-format-a-date-value/13433 "2013-09-02T13:54:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thomas\_Bolis](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thomas_bolis/32/700_2.png) [@Thomas\_Bolis](https://discuss.elastic.co/u/Thomas_Bolis)\
**Post date:** [September 2, 2013, 1:54pm UTC](https://discuss.elastic.co/t/scriptfield-in-termsfacet-to-format-a-date-value/13433/1 "2013-09-02T13:54:28Z")

</div>

Hi,

I'm trying to get results out of a TermsFacet with the use of scriptField.  
What I'm trying to do is to format the date value and truncate the  
milliseconds and then get the output from the facet. Can this be done via  
an mvel script?

E.g.

I have two docs with the following values

doc1 time: 2013-09-01 22:10:15.123  
doc2 time: 2013-09-01 22:10:15.456

and i want to have as an output

term: 2013-09-01 22:10:15.000  
count: 2

is it possible to have something like term.setScriptField (  
DateFormat.format( doc['time'].value, 'yyyy-MM-dd HH:mm:ss')) )??

Thank you

Thomas

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![javanna](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/javanna/32/4698_2.png) [@javanna](https://discuss.elastic.co/u/javanna)\
**Post date:** [September 2, 2013, 3:26pm UTC](https://discuss.elastic.co/t/scriptfield-in-termsfacet-to-format-a-date-value/13433/2 "2013-09-02T15:26:09Z")

</div>

Hi Thomas,  
probably not the fastest thing to do using scripting, but here is a little  
script that does what you need:

{  
"facets": {  
"test": {  
"terms": {  
"size": 10,  
"script\_field": "new java.text.SimpleDateFormat('yyyy-MM-dd  
HH:mm:ss').format(new Date(\_doc.time.value))"  
}  
}  
}  
}

At the end of the day I just used some Java code within the mvel script 😉

Cheers  
Luca

On Monday, September 2, 2013 3:54:28 PM UTC+2, Thomas wrote:

> Hi,
> 
> I'm trying to get results out of a TermsFacet with the use of scriptField.  
> What I'm trying to do is to format the date value and truncate the  
> milliseconds and then get the output from the facet. Can this be done via  
> an mvel script?
> 
> E.g.
> 
> I have two docs with the following values
> 
> doc1 time: 2013-09-01 22:10:15.123  
> doc2 time: 2013-09-01 22:10:15.456
> 
> and i want to have as an output
> 
> term: 2013-09-01 22:10:15.000  
> count: 2
> 
> is it possible to have something like term.setScriptField (  
> DateFormat.format( doc['time'].value, 'yyyy-MM-dd HH:mm:ss')) )??
> 
> Thank you
> 
> Thomas

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Thomas\_Bolis](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thomas_bolis/32/700_2.png) [@Thomas\_Bolis](https://discuss.elastic.co/u/Thomas_Bolis)\
**Post date:** [September 2, 2013, 3:34pm UTC](https://discuss.elastic.co/t/scriptfield-in-termsfacet-to-format-a-date-value/13433/3 "2013-09-02T15:34:51Z")

</div>

Hey thanks for your reply,

I also used this which might be slight better:

new  
org.joda.time.DateTime(doc['time'].value).withMillisOfSecond(0).getMillis()

but is there any way with native mvel which will probably be the fastest  
way?

Thanks again 🙂

On Monday, September 2, 2013 4:54:28 PM UTC+3, Thomas wrote:

> Hi,
> 
> I'm trying to get results out of a TermsFacet with the use of scriptField.  
> What I'm trying to do is to format the date value and truncate the  
> milliseconds and then get the output from the facet. Can this be done via  
> an mvel script?
> 
> E.g.
> 
> I have two docs with the following values
> 
> doc1 time: 2013-09-01 22:10:15.123  
> doc2 time: 2013-09-01 22:10:15.456
> 
> and i want to have as an output
> 
> term: 2013-09-01 22:10:15.000  
> count: 2
> 
> is it possible to have something like term.setScriptField (  
> DateFormat.format( doc['time'].value, 'yyyy-MM-dd HH:mm:ss')) )??
> 
> Thank you
> 
> Thomas

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:18am UTC](https://discuss.elastic.co/t/scriptfield-in-termsfacet-to-format-a-date-value/13433/4 "2017-07-06T02:18:28Z")

</div>

Closed on July 6, 2017, 2:18am UTC


