19
19
package org .elasticsearch .ingest .common ;
20
20
21
21
import org .elasticsearch .ingest .IngestDocument ;
22
+ import org .elasticsearch .ingest .TestTemplateService ;
22
23
import org .elasticsearch .test .ESTestCase ;
23
24
import org .joda .time .DateTime ;
24
25
import org .joda .time .DateTimeZone ;
26
+ import org .joda .time .format .DateTimeFormat ;
25
27
26
28
import java .util .Collections ;
29
+ import java .util .List ;
27
30
import java .util .Locale ;
28
31
import java .util .function .Function ;
29
32
@@ -33,11 +36,8 @@ public class DateIndexNameProcessorTests extends ESTestCase {
33
36
34
37
public void testJodaPattern () throws Exception {
35
38
Function <String , DateTime > function = DateFormat .Joda .getFunction ("yyyy-MM-dd'T'HH:mm:ss.SSSZ" , DateTimeZone .UTC , Locale .ROOT );
36
- DateIndexNameProcessor processor = new DateIndexNameProcessor (
37
- "_tag" , "_field" , Collections .singletonList (function ), DateTimeZone .UTC ,
38
- "events-" , "y" , "yyyyMMdd"
39
- );
40
-
39
+ DateIndexNameProcessor processor = createProcessor ("_field" , Collections .singletonList (function ),
40
+ DateTimeZone .UTC , "events-" , "y" , "yyyyMMdd" );
41
41
IngestDocument document = new IngestDocument ("_index" , "_type" , "_id" , null , null , null ,
42
42
Collections .singletonMap ("_field" , "2016-04-25T12:24:20.101Z" ));
43
43
processor .execute (document );
@@ -46,7 +46,7 @@ public void testJodaPattern() throws Exception {
46
46
47
47
public void testTAI64N ()throws Exception {
48
48
Function <String , DateTime > function = DateFormat .Tai64n .getFunction (null , DateTimeZone .UTC , null );
49
- DateIndexNameProcessor dateProcessor = new DateIndexNameProcessor ( "_tag" , "_field" , Collections .singletonList (function ),
49
+ DateIndexNameProcessor dateProcessor = createProcessor ( "_field" , Collections .singletonList (function ),
50
50
DateTimeZone .UTC , "events-" , "m" , "yyyyMMdd" );
51
51
IngestDocument document = new IngestDocument ("_index" , "_type" , "_id" , null , null , null ,
52
52
Collections .singletonMap ("_field" , (randomBoolean () ? "@" : "" ) + "4000000050d506482dbdf024" ));
@@ -56,7 +56,7 @@ public void testTAI64N()throws Exception {
56
56
57
57
public void testUnixMs ()throws Exception {
58
58
Function <String , DateTime > function = DateFormat .UnixMs .getFunction (null , DateTimeZone .UTC , null );
59
- DateIndexNameProcessor dateProcessor = new DateIndexNameProcessor ( "_tag" , "_field" , Collections .singletonList (function ),
59
+ DateIndexNameProcessor dateProcessor = createProcessor ( "_field" , Collections .singletonList (function ),
60
60
DateTimeZone .UTC , "events-" , "m" , "yyyyMMdd" );
61
61
IngestDocument document = new IngestDocument ("_index" , "_type" , "_id" , null , null , null ,
62
62
Collections .singletonMap ("_field" , "1000500" ));
@@ -71,12 +71,41 @@ public void testUnixMs()throws Exception {
71
71
72
72
public void testUnix ()throws Exception {
73
73
Function <String , DateTime > function = DateFormat .Unix .getFunction (null , DateTimeZone .UTC , null );
74
- DateIndexNameProcessor dateProcessor = new DateIndexNameProcessor ( "_tag" , "_field" , Collections .singletonList (function ),
74
+ DateIndexNameProcessor dateProcessor = createProcessor ( "_field" , Collections .singletonList (function ),
75
75
DateTimeZone .UTC , "events-" , "m" , "yyyyMMdd" );
76
76
IngestDocument document = new IngestDocument ("_index" , "_type" , "_id" , null , null , null ,
77
77
Collections .singletonMap ("_field" , "1000.5" ));
78
78
dateProcessor .execute (document );
79
79
assertThat (document .getSourceAndMetadata ().get ("_index" ), equalTo ("<events-{19700101||/m{yyyyMMdd|UTC}}>" ));
80
80
}
81
81
82
+ public void testTemplatedFields () throws Exception {
83
+ String indexNamePrefix = randomAlphaOfLength (10 );
84
+ String dateRounding = randomFrom ("y" , "M" , "w" , "d" , "h" , "m" , "s" );
85
+ String indexNameFormat = randomFrom ("yyyy-MM-dd'T'HH:mm:ss.SSSZ" , "yyyyMMdd" , "MM/dd/yyyy" );
86
+ String date = Integer .toString (randomInt ());
87
+ Function <String , DateTime > dateTimeFunction = DateFormat .Unix .getFunction (null , DateTimeZone .UTC , null );
88
+
89
+ DateIndexNameProcessor dateProcessor = createProcessor ("_field" ,
90
+ Collections .singletonList (dateTimeFunction ), DateTimeZone .UTC , indexNamePrefix ,
91
+ dateRounding , indexNameFormat );
92
+
93
+ IngestDocument document = new IngestDocument ("_index" , "_type" , "_id" , null , null , null ,
94
+ Collections .singletonMap ("_field" , date ));
95
+ dateProcessor .execute (document );
96
+
97
+ assertThat (document .getSourceAndMetadata ().get ("_index" ),
98
+ equalTo ("<" +indexNamePrefix +"{" +DateTimeFormat .forPattern (indexNameFormat )
99
+ .print (dateTimeFunction .apply (date ))+"||/" +dateRounding +"{" +indexNameFormat +"|UTC}}>" ));
100
+ }
101
+
102
+ private DateIndexNameProcessor createProcessor (String field , List <Function <String , DateTime >> dateFormats ,
103
+ DateTimeZone timezone , String indexNamePrefix , String dateRounding ,
104
+ String indexNameFormat ) {
105
+ return new DateIndexNameProcessor (randomAlphaOfLength (10 ), field , dateFormats , timezone ,
106
+ new TestTemplateService .MockTemplateScript .Factory (indexNamePrefix ),
107
+ new TestTemplateService .MockTemplateScript .Factory (dateRounding ),
108
+ new TestTemplateService .MockTemplateScript .Factory (indexNameFormat )
109
+ );
110
+ }
82
111
}
0 commit comments