Skip to content

Commit f9a6ec1

Browse files
committed
ingest: date_index_name processor template resolution
Review changes: * Change to TemplateScript.Factory instead of ValueSource.wrap * Add support for templated index_name_format * Remove unecessary changes
1 parent 86d6b1b commit f9a6ec1

File tree

4 files changed

+67
-72
lines changed

4 files changed

+67
-72
lines changed

modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateIndexNameProcessor.java

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
import org.elasticsearch.ingest.ConfigurationUtils;
3333
import org.elasticsearch.ingest.IngestDocument;
3434
import org.elasticsearch.ingest.Processor;
35-
import org.elasticsearch.ingest.ValueSource;
3635
import org.elasticsearch.script.ScriptService;
36+
import org.elasticsearch.script.TemplateScript;
3737
import org.joda.time.DateTime;
3838
import org.joda.time.DateTimeZone;
3939
import org.joda.time.format.DateTimeFormat;
@@ -44,23 +44,22 @@ public final class DateIndexNameProcessor extends AbstractProcessor {
4444
public static final String TYPE = "date_index_name";
4545

4646
private final String field;
47-
private final String indexNamePrefix;
48-
private final String dateRounding;
49-
private final String indexNameFormat;
47+
private final TemplateScript.Factory indexNamePrefixTemplate;
48+
private final TemplateScript.Factory dateRoundingTemplate;
49+
private final TemplateScript.Factory indexNameFormatTemplate;
5050
private final DateTimeZone timezone;
5151
private final List<Function<String, DateTime>> dateFormats;
52-
private final ScriptService scriptService;
5352

5453
DateIndexNameProcessor(String tag, String field, List<Function<String, DateTime>> dateFormats, DateTimeZone timezone,
55-
String indexNamePrefix, String dateRounding, String indexNameFormat, ScriptService scriptService) {
54+
TemplateScript.Factory indexNamePrefixTemplate, TemplateScript.Factory dateRoundingTemplate,
55+
TemplateScript.Factory indexNameFormatTemplate) {
5656
super(tag);
5757
this.field = field;
5858
this.timezone = timezone;
5959
this.dateFormats = dateFormats;
60-
this.indexNamePrefix = indexNamePrefix;
61-
this.dateRounding = dateRounding;
62-
this.indexNameFormat = indexNameFormat;
63-
this.scriptService = scriptService;
60+
this.indexNamePrefixTemplate = indexNamePrefixTemplate;
61+
this.dateRoundingTemplate = dateRoundingTemplate;
62+
this.indexNameFormatTemplate = indexNameFormatTemplate;
6463
}
6564

6665
@Override
@@ -87,6 +86,9 @@ public void execute(IngestDocument ingestDocument) throws Exception {
8786
if (dateTime == null) {
8887
throw new IllegalArgumentException("unable to parse date [" + date + "]", lastException);
8988
}
89+
String indexNamePrefix = ingestDocument.renderTemplate(indexNamePrefixTemplate);
90+
String indexNameFormat = ingestDocument.renderTemplate(indexNameFormatTemplate);
91+
String dateRounding = ingestDocument.renderTemplate(dateRoundingTemplate);
9092

9193
DateTimeFormatter formatter = DateTimeFormat.forPattern(indexNameFormat);
9294
StringBuilder builder = new StringBuilder()
@@ -98,7 +100,7 @@ public void execute(IngestDocument ingestDocument) throws Exception {
98100
.append('}')
99101
.append('>');
100102
String dynamicIndexName = builder.toString();
101-
ingestDocument.setFieldValue(IngestDocument.MetaData.INDEX.getFieldName(), ValueSource.wrap(dynamicIndexName, scriptService));
103+
ingestDocument.setFieldValue(IngestDocument.MetaData.INDEX.getFieldName(), dynamicIndexName);
102104
}
103105

104106
@Override
@@ -110,16 +112,16 @@ String getField() {
110112
return field;
111113
}
112114

113-
String getIndexNamePrefix() {
114-
return indexNamePrefix;
115+
TemplateScript.Factory getIndexNamePrefixTemplate() {
116+
return indexNamePrefixTemplate;
115117
}
116118

117-
String getDateRounding() {
118-
return dateRounding;
119+
TemplateScript.Factory getDateRoundingTemplate() {
120+
return dateRoundingTemplate;
119121
}
120122

121-
String getIndexNameFormat() {
122-
return indexNameFormat;
123+
TemplateScript.Factory getIndexNameFormatTemplate() {
124+
return indexNameFormatTemplate;
123125
}
124126

125127
DateTimeZone getTimezone() {
@@ -164,10 +166,16 @@ public DateIndexNameProcessor create(Map<String, Processor.Factory> registry, St
164166

165167
String field = ConfigurationUtils.readStringProperty(TYPE, tag, config, "field");
166168
String indexNamePrefix = ConfigurationUtils.readStringProperty(TYPE, tag, config, "index_name_prefix", "");
169+
TemplateScript.Factory indexNamePrefixTemplate =
170+
ConfigurationUtils.compileTemplate(TYPE, tag, "index_name_prefix", indexNamePrefix, scriptService);
167171
String dateRounding = ConfigurationUtils.readStringProperty(TYPE, tag, config, "date_rounding");
172+
TemplateScript.Factory dateRoundingTemplate =
173+
ConfigurationUtils.compileTemplate(TYPE, tag, "date_rounding", dateRounding, scriptService);
168174
String indexNameFormat = ConfigurationUtils.readStringProperty(TYPE, tag, config, "index_name_format", "yyyy-MM-dd");
169-
return new DateIndexNameProcessor(tag, field, dateFormats, timezone, indexNamePrefix,
170-
dateRounding, indexNameFormat, scriptService);
175+
TemplateScript.Factory indexNameFormatTemplate =
176+
ConfigurationUtils.compileTemplate(TYPE, tag, "index_name_format", indexNameFormat, scriptService);
177+
return new DateIndexNameProcessor(tag, field, dateFormats, timezone, indexNamePrefixTemplate,
178+
dateRoundingTemplate, indexNameFormatTemplate);
171179
}
172180
}
173181

modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DateIndexNameFactoryTests.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.joda.time.DateTimeZone;
2727

2828
import java.util.Arrays;
29+
import java.util.Collections;
2930
import java.util.HashMap;
3031
import java.util.Map;
3132

@@ -40,9 +41,9 @@ public void testDefaults() throws Exception {
4041
DateIndexNameProcessor processor = factory.create(null, null, config);
4142
assertThat(processor.getDateFormats().size(), Matchers.equalTo(1));
4243
assertThat(processor.getField(), Matchers.equalTo("_field"));
43-
assertThat(processor.getIndexNamePrefix(), Matchers.equalTo(""));
44-
assertThat(processor.getDateRounding(), Matchers.equalTo("y"));
45-
assertThat(processor.getIndexNameFormat(), Matchers.equalTo("yyyy-MM-dd"));
44+
assertThat(processor.getIndexNamePrefixTemplate().newInstance(Collections.emptyMap()).execute(), Matchers.equalTo(""));
45+
assertThat(processor.getDateRoundingTemplate().newInstance(Collections.emptyMap()).execute(), Matchers.equalTo("y"));
46+
assertThat(processor.getIndexNameFormatTemplate().newInstance(Collections.emptyMap()).execute(), Matchers.equalTo("yyyy-MM-dd"));
4647
assertThat(processor.getTimezone(), Matchers.equalTo(DateTimeZone.UTC));
4748
}
4849

@@ -64,7 +65,7 @@ public void testSpecifyOptionalSettings() throws Exception {
6465
config.put("index_name_format", "yyyyMMdd");
6566

6667
processor = factory.create(null, null, config);
67-
assertThat(processor.getIndexNameFormat(), Matchers.equalTo("yyyyMMdd"));
68+
assertThat(processor.getIndexNameFormatTemplate().newInstance(Collections.emptyMap()).execute(), Matchers.equalTo("yyyyMMdd"));
6869

6970
config = new HashMap<>();
7071
config.put("field", "_field");
@@ -81,7 +82,7 @@ public void testSpecifyOptionalSettings() throws Exception {
8182
config.put("date_rounding", "y");
8283

8384
processor = factory.create(null, null, config);
84-
assertThat(processor.getIndexNamePrefix(), Matchers.equalTo("_prefix"));
85+
assertThat(processor.getIndexNamePrefixTemplate().newInstance(Collections.emptyMap()).execute(), Matchers.equalTo("_prefix"));
8586
}
8687

8788
public void testRequiredFields() throws Exception {

modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DateIndexNameProcessorTests.java

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,24 @@
2020

2121
import org.elasticsearch.ingest.IngestDocument;
2222
import org.elasticsearch.ingest.TestTemplateService;
23-
import org.elasticsearch.script.Script;
24-
import org.elasticsearch.script.ScriptContext;
25-
import org.elasticsearch.script.ScriptService;
26-
import org.elasticsearch.script.TemplateScript;
2723
import org.elasticsearch.test.ESTestCase;
2824
import org.joda.time.DateTime;
2925
import org.joda.time.DateTimeZone;
30-
import org.mockito.Matchers;
31-
import org.mockito.Mockito;
26+
import org.joda.time.format.DateTimeFormat;
3227

3328
import java.util.Collections;
29+
import java.util.List;
3430
import java.util.Locale;
3531
import java.util.function.Function;
3632

37-
import static org.elasticsearch.script.Script.DEFAULT_TEMPLATE_LANG;
3833
import static org.hamcrest.CoreMatchers.equalTo;
39-
import static org.mockito.Matchers.any;
40-
import static org.mockito.Mockito.mock;
41-
import static org.mockito.Mockito.when;
4234

4335
public class DateIndexNameProcessorTests extends ESTestCase {
4436

4537
public void testJodaPattern() throws Exception {
4638
Function<String, DateTime> function = DateFormat.Joda.getFunction("yyyy-MM-dd'T'HH:mm:ss.SSSZ", DateTimeZone.UTC, Locale.ROOT);
47-
DateIndexNameProcessor processor = new DateIndexNameProcessor(
48-
"_tag", "_field", Collections.singletonList(function), DateTimeZone.UTC,
49-
"events-", "y", "yyyyMMdd", TestTemplateService.instance()
50-
);
51-
39+
DateIndexNameProcessor processor = createProcessor("_field", Collections.singletonList(function),
40+
DateTimeZone.UTC, "events-", "y", "yyyyMMdd");
5241
IngestDocument document = new IngestDocument("_index", "_type", "_id", null, null, null,
5342
Collections.singletonMap("_field", "2016-04-25T12:24:20.101Z"));
5443
processor.execute(document);
@@ -57,8 +46,8 @@ public void testJodaPattern() throws Exception {
5746

5847
public void testTAI64N()throws Exception {
5948
Function<String, DateTime> function = DateFormat.Tai64n.getFunction(null, DateTimeZone.UTC, null);
60-
DateIndexNameProcessor dateProcessor = new DateIndexNameProcessor("_tag", "_field", Collections.singletonList(function),
61-
DateTimeZone.UTC, "events-", "m", "yyyyMMdd", TestTemplateService.instance());
49+
DateIndexNameProcessor dateProcessor = createProcessor("_field", Collections.singletonList(function),
50+
DateTimeZone.UTC, "events-", "m", "yyyyMMdd");
6251
IngestDocument document = new IngestDocument("_index", "_type", "_id", null, null, null,
6352
Collections.singletonMap("_field", (randomBoolean() ? "@" : "") + "4000000050d506482dbdf024"));
6453
dateProcessor.execute(document);
@@ -67,8 +56,8 @@ public void testTAI64N()throws Exception {
6756

6857
public void testUnixMs()throws Exception {
6958
Function<String, DateTime> function = DateFormat.UnixMs.getFunction(null, DateTimeZone.UTC, null);
70-
DateIndexNameProcessor dateProcessor = new DateIndexNameProcessor("_tag", "_field", Collections.singletonList(function),
71-
DateTimeZone.UTC, "events-", "m", "yyyyMMdd", TestTemplateService.instance());
59+
DateIndexNameProcessor dateProcessor = createProcessor("_field", Collections.singletonList(function),
60+
DateTimeZone.UTC, "events-", "m", "yyyyMMdd");
7261
IngestDocument document = new IngestDocument("_index", "_type", "_id", null, null, null,
7362
Collections.singletonMap("_field", "1000500"));
7463
dateProcessor.execute(document);
@@ -82,29 +71,41 @@ public void testUnixMs()throws Exception {
8271

8372
public void testUnix()throws Exception {
8473
Function<String, DateTime> function = DateFormat.Unix.getFunction(null, DateTimeZone.UTC, null);
85-
DateIndexNameProcessor dateProcessor = new DateIndexNameProcessor("_tag", "_field", Collections.singletonList(function),
86-
DateTimeZone.UTC, "events-", "m", "yyyyMMdd", TestTemplateService.instance());
74+
DateIndexNameProcessor dateProcessor = createProcessor("_field", Collections.singletonList(function),
75+
DateTimeZone.UTC, "events-", "m", "yyyyMMdd");
8776
IngestDocument document = new IngestDocument("_index", "_type", "_id", null, null, null,
8877
Collections.singletonMap("_field", "1000.5"));
8978
dateProcessor.execute(document);
9079
assertThat(document.getSourceAndMetadata().get("_index"), equalTo("<events-{19700101||/m{yyyyMMdd|UTC}}>"));
9180
}
9281

93-
public void testTemplateSupported() throws Exception {
94-
ScriptService scriptService = mock(ScriptService.class);
95-
TestTemplateService.MockTemplateScript.Factory factory = new TestTemplateService.MockTemplateScript.Factory("script_result");
96-
when(scriptService.compile(any(Script.class), Matchers.<ScriptContext<TemplateScript.Factory>>any())).thenReturn(factory);
97-
when(scriptService.isLangSupported(DEFAULT_TEMPLATE_LANG)).thenReturn(true);
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);
9892

99-
DateIndexNameProcessor dateProcessor = new DateIndexNameProcessor("_tag", "_field",
100-
Collections.singletonList(DateFormat.Unix.getFunction(null, DateTimeZone.UTC, null)),
101-
DateTimeZone.UTC, "events-", "m", "yyyyMMdd", scriptService);
10293
IngestDocument document = new IngestDocument("_index", "_type", "_id", null, null, null,
103-
Collections.singletonMap("_field", "1000.5"));
94+
Collections.singletonMap("_field", date));
10495
dateProcessor.execute(document);
10596

106-
// here we only care that the script was compiled and that it returned what we expect.
107-
Mockito.verify(scriptService).compile(any(Script.class), Matchers.<ScriptContext<TemplateScript.Factory>>any());
108-
assertThat(document.getSourceAndMetadata().get("_index"), equalTo("script_result"));
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+
);
109110
}
110111
}

server/src/main/java/org/elasticsearch/ingest/IngestDocument.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -406,21 +406,6 @@ public void setFieldValue(String path, Object value) {
406406
setFieldValue(path, value, false);
407407
}
408408

409-
/**
410-
* Sets the provided value to the provided path in the document.
411-
* Any non existing path element will be created.
412-
* If the last item in the path is a list, the value will replace the existing list as a whole.
413-
* Use {@link #appendFieldValue(String, Object)} to append values to lists instead.
414-
* @param path The path within the document in dot-notation
415-
* @param valueSource The value source that will produce the value or values to append to the existing ones
416-
* @throws IllegalArgumentException if the path is null, empty, invalid or if the value cannot be set to the
417-
* item identified by the provided path.
418-
*/
419-
public void setFieldValue(String path, ValueSource valueSource) {
420-
Map<String, Object> model = valueSource == null ? null : createTemplateModel();
421-
setFieldValue(path, valueSource == null ? null : valueSource.copyAndResolve(model), false);
422-
}
423-
424409
/**
425410
* Sets the provided value to the provided path in the document.
426411
* Any non existing path element will be created. If the last element is a list,

0 commit comments

Comments
 (0)