27
27
import org .elasticsearch .common .Strings ;
28
28
import org .elasticsearch .common .collect .Tuple ;
29
29
import org .elasticsearch .common .component .AbstractComponent ;
30
- import org .elasticsearch .common .joda .FormatDateTimeFormatter ;
31
30
import org .elasticsearch .common .regex .Regex ;
32
31
import org .elasticsearch .common .settings .Settings ;
32
+ import org .elasticsearch .common .time .DateFormatter ;
33
+ import org .elasticsearch .common .time .DateFormatters ;
33
34
import org .elasticsearch .common .time .DateMathParser ;
34
- import org .elasticsearch .common .time .DateUtils ;
35
+ import org .elasticsearch .common .time .JavaDateMathParser ;
35
36
import org .elasticsearch .common .util .set .Sets ;
36
37
import org .elasticsearch .index .Index ;
37
38
import org .elasticsearch .index .IndexNotFoundException ;
38
39
import org .elasticsearch .indices .IndexClosedException ;
39
40
import org .elasticsearch .indices .InvalidIndexNameException ;
40
- import org .joda .time .DateTimeZone ;
41
- import org .joda .time .format .DateTimeFormat ;
42
- import org .joda .time .format .DateTimeFormatter ;
43
41
42
+ import java .time .Instant ;
43
+ import java .time .ZoneId ;
44
+ import java .time .ZoneOffset ;
44
45
import java .util .ArrayList ;
45
46
import java .util .Arrays ;
46
47
import java .util .Collections ;
47
48
import java .util .HashMap ;
48
49
import java .util .HashSet ;
49
50
import java .util .List ;
50
- import java .util .Locale ;
51
51
import java .util .Map ;
52
52
import java .util .Set ;
53
53
import java .util .SortedMap ;
@@ -62,7 +62,7 @@ public class IndexNameExpressionResolver extends AbstractComponent {
62
62
public IndexNameExpressionResolver (Settings settings ) {
63
63
super (settings );
64
64
expressionResolvers = Arrays .asList (
65
- dateMathExpressionResolver = new DateMathExpressionResolver (settings ),
65
+ dateMathExpressionResolver = new DateMathExpressionResolver (),
66
66
new WildcardExpressionResolver ()
67
67
);
68
68
}
@@ -815,24 +815,14 @@ private static List<String> resolveEmptyOrTrivialWildcard(IndicesOptions options
815
815
816
816
static final class DateMathExpressionResolver implements ExpressionResolver {
817
817
818
+ private static final DateFormatter DEFAULT_DATE_FORMATTER = DateFormatters .forPattern ("uuuu.MM.dd" );
818
819
private static final String EXPRESSION_LEFT_BOUND = "<" ;
819
820
private static final String EXPRESSION_RIGHT_BOUND = ">" ;
820
821
private static final char LEFT_BOUND = '{' ;
821
822
private static final char RIGHT_BOUND = '}' ;
822
823
private static final char ESCAPE_CHAR = '\\' ;
823
824
private static final char TIME_ZONE_BOUND = '|' ;
824
825
825
- private final DateTimeZone defaultTimeZone ;
826
- private final String defaultDateFormatterPattern ;
827
- private final DateTimeFormatter defaultDateFormatter ;
828
-
829
- DateMathExpressionResolver (Settings settings ) {
830
- String defaultTimeZoneId = settings .get ("date_math_expression_resolver.default_time_zone" , "UTC" );
831
- this .defaultTimeZone = DateTimeZone .forID (defaultTimeZoneId );
832
- defaultDateFormatterPattern = settings .get ("date_math_expression_resolver.default_date_format" , "YYYY.MM.dd" );
833
- this .defaultDateFormatter = DateTimeFormat .forPattern (defaultDateFormatterPattern );
834
- }
835
-
836
826
@ Override
837
827
public List <String > resolve (final Context context , List <String > expressions ) {
838
828
List <String > result = new ArrayList <>(expressions .size ());
@@ -896,13 +886,12 @@ String resolveExpression(String expression, final Context context) {
896
886
int dateTimeFormatLeftBoundIndex = inPlaceHolderString .indexOf (LEFT_BOUND );
897
887
String mathExpression ;
898
888
String dateFormatterPattern ;
899
- DateTimeFormatter dateFormatter ;
900
- final DateTimeZone timeZone ;
889
+ DateFormatter dateFormatter ;
890
+ final ZoneId timeZone ;
901
891
if (dateTimeFormatLeftBoundIndex < 0 ) {
902
892
mathExpression = inPlaceHolderString ;
903
- dateFormatterPattern = defaultDateFormatterPattern ;
904
- dateFormatter = defaultDateFormatter ;
905
- timeZone = defaultTimeZone ;
893
+ dateFormatter = DEFAULT_DATE_FORMATTER ;
894
+ timeZone = ZoneOffset .UTC ;
906
895
} else {
907
896
if (inPlaceHolderString .lastIndexOf (RIGHT_BOUND ) != inPlaceHolderString .length () - 1 ) {
908
897
throw new ElasticsearchParseException ("invalid dynamic name expression [{}]. missing closing `}` for date math format" , inPlaceHolderString );
@@ -915,20 +904,18 @@ String resolveExpression(String expression, final Context context) {
915
904
int formatPatternTimeZoneSeparatorIndex = dateFormatterPatternAndTimeZoneId .indexOf (TIME_ZONE_BOUND );
916
905
if (formatPatternTimeZoneSeparatorIndex != -1 ) {
917
906
dateFormatterPattern = dateFormatterPatternAndTimeZoneId .substring (0 , formatPatternTimeZoneSeparatorIndex );
918
- timeZone = DateTimeZone . forID (dateFormatterPatternAndTimeZoneId .substring (formatPatternTimeZoneSeparatorIndex + 1 ));
907
+ timeZone = ZoneId . of (dateFormatterPatternAndTimeZoneId .substring (formatPatternTimeZoneSeparatorIndex + 1 ));
919
908
} else {
920
909
dateFormatterPattern = dateFormatterPatternAndTimeZoneId ;
921
- timeZone = defaultTimeZone ;
910
+ timeZone = ZoneOffset . UTC ;
922
911
}
923
- dateFormatter = DateTimeFormat .forPattern (dateFormatterPattern );
912
+ dateFormatter = DateFormatters .forPattern (dateFormatterPattern );
924
913
}
925
- DateTimeFormatter parser = dateFormatter .withZone (timeZone );
926
- FormatDateTimeFormatter formatter = new FormatDateTimeFormatter (dateFormatterPattern , parser , Locale .ROOT );
927
- DateMathParser dateMathParser = formatter .toDateMathParser ();
928
- long millis = dateMathParser .parse (mathExpression , context ::getStartTime , false ,
929
- DateUtils .dateTimeZoneToZoneId (timeZone ));
914
+ DateFormatter formatter = dateFormatter .withZone (timeZone );
915
+ DateMathParser dateMathParser = new JavaDateMathParser (formatter );
916
+ long millis = dateMathParser .parse (mathExpression , context ::getStartTime , false , timeZone );
930
917
931
- String time = formatter .printer (). print (millis );
918
+ String time = formatter .format ( Instant . ofEpochMilli (millis ) );
932
919
beforePlaceHolderSb .append (time );
933
920
inPlaceHolderSb = new StringBuilder ();
934
921
inPlaceHolder = false ;
@@ -968,18 +955,4 @@ String resolveExpression(String expression, final Context context) {
968
955
return beforePlaceHolderSb .toString ();
969
956
}
970
957
}
971
-
972
- /**
973
- * Returns <code>true</code> iff the given expression resolves to the given index name otherwise <code>false</code>
974
- */
975
- public final boolean matchesIndex (String indexName , String expression , ClusterState state ) {
976
- final String [] concreteIndices = concreteIndexNames (state , IndicesOptions .lenientExpandOpen (), expression );
977
- for (String index : concreteIndices ) {
978
- if (Regex .simpleMatch (index , indexName )) {
979
- return true ;
980
- }
981
- }
982
- return indexName .equals (expression );
983
- }
984
-
985
958
}
0 commit comments