Skip to content

OmriBromberg/elasticsearch-datemath

Repository files navigation

elasticsearch-datemath

This is a datemath library for Elasticsearch written in Java
It supports the datemath expression standard of Elasticsearch 5.5

Specification

https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/date-math-expressions.html

Example

DateMathParser dateMathParser = new DateMathBuilder()
                .pattern("yyyy-MM-dd HH-ss")
                .zone(ZoneId.of("America/New_York"))
                .build();
dateMathParser.resolveExpression("1998-09-18 16-43||-4d");

API

Parser

DateMathParser

ZonedDateTime resolveExpression(String Expression)

// gets the current datetime and subtracts 5 days
dateMathParser.resolveExpression("now-5d");
    
// gets the current datetime and substracts 6 months, adds 6 years and rounds the month
dateMathParser.resolveExpression("now-6M+6Y/M");
    
// gets the datetime of "2017-07-18" and adds 5 minutes
dateMathParser.resolveExpression("2017-07-18||+5m");

DateMathBuilder

DateMathBuilder pattern(String pattern)

// creates a new instance from the current with the new pattern
dateMathBuilder.pattern("yyyy-MM-dd");

DateMathBuilder zone(DateTimeZone zone)

// creates a new instance from the current with the new zone
dateMathBuilder.zone(ZoneId.of("America/New_York"));

DateMathBuilder now(Supplier nowSupplier)

// creates a new instance from the current with the new nowSupplier
dateMathBuilder.now(() -> ZonedDateTime.now(ZoneId.of("America/New_York")));

DateMathBuilder nowPattern(String nowPattern)

// creates a new instance from the current with the new nowPattern
dateMathBuilder.nowPattern("now");

DateMathParser build()

// creates a new DateMathParser instance and returns it
dateMathBuilder.build();

Formatter

DateMathFormatter

static Collection<String> getAllPatternsBetween(ZonedDateTime start, ZonedDateTime end, String pattern)

// gets all the patterns of datetimes between start and end
DateMathFormatter.getAllPatternsBetween(ZonedDateTime.now().minus(7, ChronoUnit.YEARS), ZonedDateTime.now(), "yyyy-MM-dd HH");

Collection<String> getAllPatternsBetween(ZonedDateTime start, ZonedDateTime end)

// gets all the patterns of datetimes between start and end
dateMathFormatter.getAllPatternsBetween(ZonedDateTime.now().minus(7, ChronoUnit.YEARS), ZonedDateTime.now(), "yyyy-MM-dd HH");