Search code examples
elasticsearchelasticsearch-painless

Strip day from elasticsearch date using painless scripting


I have date with this format November 17th 2016, 12:54:29.000" and I need to get that 17 only using painless script. Also if anyone can provide me link to painless script tutorial regarding date formats.


Solution

  • If the field is mapped as date you can just

    doc['<field_name>'].date.dayOfMonth.

    Otherwise, you'll need to use a regular expression, something like

    / (\d{2})/.matcher(doc['<field_name>'].value).group(1).

    The date-based solution uses org.joda.time.MutableDateTime. The regex one uses java.util.regex.Matcher and you have to enable regular expressions in your Elasticsearch configuration file as script.painless.regex.enabled=on.