Skip to content

Commit 17bd559

Browse files
authored
Fix the timestamp field of a data stream to @timestamp (#59210)
Backport of #59076 to 7.x branch. The commit makes the following changes: * The timestamp field of a data stream definition in a composable index template can only be set to '@timestamp'. * Removed custom data stream timestamp field validation and reuse the validation from `TimestampFieldMapper` and instead only check that the _timestamp field mapping has been defined on a backing index of a data stream. * Moved code that injects _timestamp meta field mapping from `MetadataCreateIndexService#applyCreateIndexRequestWithV2Template58956(...)` method to `MetadataIndexTemplateService#collectMappings(...)` method. * Fixed a bug (#58956) that cases timestamp field validation to be performed for each template and instead of the final mappings that is created. * only apply _timestamp meta field if index is created as part of a data stream or data stream rollover, this fixes a docs test, where a regular index creation matches (logs-*) with a template with a data stream definition. Relates to #58642 Relates to #53100 Closes #58956 Closes #58583
1 parent 6a7ac38 commit 17bd559

File tree

39 files changed

+286
-388
lines changed

39 files changed

+286
-388
lines changed

client/rest-high-level/src/test/java/org/elasticsearch/client/indices/GetComposableIndexTemplatesResponseTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ private static ComposableIndexTemplate randomIndexTemplate() {
8686
Long priority = randomBoolean() ? null : randomNonNegativeLong();
8787
Long version = randomBoolean() ? null : randomNonNegativeLong();
8888
if (randomBoolean()) {
89-
dataStreamTemplate = new ComposableIndexTemplate.DataStreamTemplate(randomAlphaOfLength(8));
89+
dataStreamTemplate = new ComposableIndexTemplate.DataStreamTemplate("@timestamp");
9090
}
9191
return new ComposableIndexTemplate(patterns, randomTemplate(), composedOf, priority, version, meta, dataStreamTemplate);
9292
}

client/rest-high-level/src/test/java/org/elasticsearch/client/indices/GetDataStreamResponseTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ private static DataStreamInfo randomInstance() {
5555
long generation = indices.size() + randomLongBetween(1, 128);
5656
String dataStreamName = randomAlphaOfLength(10).toLowerCase(Locale.ROOT);
5757
indices.add(new Index(getDefaultBackingIndexName(dataStreamName, generation), UUIDs.randomBase64UUID(random())));
58-
DataStream dataStream = new DataStream(dataStreamName, createTimestampField(randomAlphaOfLength(10)), indices, generation);
58+
DataStream dataStream = new DataStream(dataStreamName, createTimestampField("@timestamp"), indices, generation);
5959
return new DataStreamInfo(dataStream, ClusterHealthStatus.YELLOW, randomAlphaOfLengthBetween(2, 10),
6060
randomAlphaOfLengthBetween(2, 10));
6161
}

docs/reference/indices/rollover-index.asciidoc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -235,14 +235,14 @@ PUT _index_template/template
235235
"template": {
236236
"mappings": {
237237
"properties": {
238-
"date": {
238+
"@timestamp": {
239239
"type": "date"
240240
}
241241
}
242242
}
243243
},
244244
"data_stream": {
245-
"timestamp_field": "date"
245+
"timestamp_field": "@timestamp"
246246
}
247247
}
248248
-----------------------------------
@@ -257,15 +257,15 @@ PUT /_data_stream/my-data-stream <1>
257257
POST /my-data-stream/_rollover <2>
258258
{
259259
"conditions" : {
260-
"max_age": "7d",
261-
"max_docs": 1000,
262-
"max_size": "5gb"
260+
"max_age": "7d",
261+
"max_docs": 1000,
262+
"max_size": "5gb"
263263
}
264264
}
265265
--------------------------------------------------
266266
// TEST[continued]
267267
// TEST[setup:huge_twitter]
268-
// TEST[s/# Add > 1000 documents to my-data-stream/POST _reindex?refresh\n{"source":{"index":"twitter"},"dest":{"index":"my-data-stream","op_type":"create"}}/]
268+
// TEST[s/# Add > 1000 documents to my-data-stream/POST _reindex?refresh\n{"source":{"index":"twitter"},"dest":{"index":"my-data-stream","op_type":"create"},"script":{"source":"ctx._source.put('@timestamp',ctx._source.remove('date'))"}}/]
269269
<1> Creates a data stream called `my-data-stream` with one initial backing index
270270
named `my-data-stream-000001`.
271271
<2> This request creates a new backing index, `my-data-stream-000002`, and adds
@@ -307,7 +307,7 @@ The API returns the following response:
307307
[source,console]
308308
-----------------------------------
309309
DELETE /_data_stream/my-data-stream
310-
DELETE /_index_template/*
310+
DELETE /_index_template/template
311311
-----------------------------------
312312
// TEST[continued]
313313
////

modules/reindex/src/test/resources/rest-api-spec/test/reindex/96_data_streams.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ setup:
1010
body:
1111
index_patterns: logs-*
1212
data_stream:
13-
timestamp_field: timestamp
13+
timestamp_field: '@timestamp'
1414
template:
1515
mappings:
1616
properties:
17-
timestamp:
17+
'@timestamp':
1818
type: date
1919

2020
---
@@ -36,7 +36,7 @@ teardown:
3636
refresh: true
3737
body:
3838
foo: bar
39-
timestamp: '2020-12-12'
39+
'@timestamp': '2020-12-12'
4040

4141
- do:
4242
reindex:
@@ -69,7 +69,7 @@ teardown:
6969
refresh: true
7070
body:
7171
foo: bar
72-
timestamp: '2020-12-12'
72+
'@timestamp': '2020-12-12'
7373

7474
- do:
7575
reindex:
@@ -102,7 +102,7 @@ teardown:
102102
refresh: true
103103
body:
104104
foo: bar
105-
timestamp: '2020-12-12'
105+
'@timestamp': '2020-12-12'
106106

107107
- do:
108108
reindex:

qa/multi-cluster-search/src/test/resources/rest-api-spec/test/multi_cluster/100_resolve_index.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,4 @@
5151
- match: {data_streams.1.name: my_remote_cluster:simple-data-stream2}
5252
- match: {data_streams.1.backing_indices.0: .ds-simple-data-stream2-000001}
5353
- match: {data_streams.1.backing_indices.1: .ds-simple-data-stream2-000002}
54-
- match: {data_streams.1.timestamp_field: "@timestamp2"}
54+
- match: {data_streams.1.timestamp_field: "@timestamp"}

qa/multi-cluster-search/src/test/resources/rest-api-spec/test/remote_cluster/10_basic.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
template:
2828
mappings:
2929
properties:
30-
'@timestamp2':
30+
'@timestamp':
3131
type: date
3232
data_stream:
33-
timestamp_field: '@timestamp2'
33+
timestamp_field: '@timestamp'
3434

3535
- do:
3636
indices.create_data_stream:

rest-api-spec/src/main/resources/rest-api-spec/test/indices.data_stream/10_basic.yml

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ setup:
2727
template:
2828
mappings:
2929
properties:
30-
'@timestamp2':
30+
'@timestamp':
3131
type: date
3232
data_stream:
33-
timestamp_field: '@timestamp2'
33+
timestamp_field: '@timestamp'
3434

3535
---
3636
"Create data stream":
@@ -59,7 +59,7 @@ setup:
5959
- match: { data_streams.0.status: 'GREEN' }
6060
- match: { data_streams.0.template: 'my-template1' }
6161
- match: { data_streams.1.name: simple-data-stream2 }
62-
- match: { data_streams.1.timestamp_field.name: '@timestamp2' }
62+
- match: { data_streams.1.timestamp_field.name: '@timestamp' }
6363
- match: { data_streams.0.generation: 1 }
6464
- length: { data_streams.1.indices: 1 }
6565
- match: { data_streams.1.indices.0.index_name: '.ds-simple-data-stream2-000001' }
@@ -129,31 +129,26 @@ setup:
129129
indices.get_data_stream: {}
130130
- match: { data_streams.0.name: simple-data-stream1 }
131131
- match: { data_streams.0.timestamp_field.name: '@timestamp' }
132-
- match: { data_streams.0.timestamp_field.mapping: {type: date} }
133132
- match: { data_streams.0.generation: 1 }
134133
- match: { data_streams.1.name: simple-data-stream2 }
135-
- match: { data_streams.1.timestamp_field.name: '@timestamp2' }
136-
- match: { data_streams.1.timestamp_field.mapping: {type: date} }
134+
- match: { data_streams.1.timestamp_field.name: '@timestamp' }
137135
- match: { data_streams.1.generation: 1 }
138136

139137
- do:
140138
indices.get_data_stream:
141139
name: simple-data-stream1
142140
- match: { data_streams.0.name: simple-data-stream1 }
143141
- match: { data_streams.0.timestamp_field.name: '@timestamp' }
144-
- match: { data_streams.0.timestamp_field.mapping: {type: date} }
145142
- match: { data_streams.0.generation: 1 }
146143

147144
- do:
148145
indices.get_data_stream:
149146
name: simple-data-stream*
150147
- match: { data_streams.0.name: simple-data-stream1 }
151148
- match: { data_streams.0.timestamp_field.name: '@timestamp' }
152-
- match: { data_streams.0.timestamp_field.mapping: {type: date} }
153149
- match: { data_streams.0.generation: 1 }
154150
- match: { data_streams.1.name: simple-data-stream2 }
155-
- match: { data_streams.1.timestamp_field.name: '@timestamp2' }
156-
- match: { data_streams.1.timestamp_field.mapping: {type: date} }
151+
- match: { data_streams.1.timestamp_field.name: '@timestamp' }
157152
- match: { data_streams.1.generation: 1 }
158153

159154
- do:
@@ -240,35 +235,35 @@ setup:
240235
template:
241236
mappings:
242237
properties:
243-
'timestamp':
238+
'@timestamp':
244239
type: date
245240
data_stream:
246-
timestamp_field: timestamp
241+
timestamp_field: '@timestamp'
247242

248243
- do:
249244
index:
250245
index: logs-foobar
251-
body: { timestamp: '2020-12-12' }
246+
body: { '@timestamp': '2020-12-12' }
252247
- match: { _index: .ds-logs-foobar-000001 }
253248

254249
- do:
255250
catch: bad_request
256251
index:
257252
index: .ds-logs-foobar-000001
258-
body: { timestamp: '2020-12-12' }
253+
body: { '@timestamp': '2020-12-12' }
259254

260255
- do:
261256
bulk:
262257
body:
263258
- create:
264259
_index: .ds-logs-foobar-000001
265-
- timestamp: '2020-12-12'
260+
- '@timestamp': '2020-12-12'
266261
- index:
267262
_index: .ds-logs-foobar-000001
268-
- timestamp: '2020-12-12'
263+
- '@timestamp': '2020-12-12'
269264
- create:
270265
_index: logs-foobar
271-
- timestamp: '2020-12-12'
266+
- '@timestamp': '2020-12-12'
272267
- match: { errors: true }
273268
- match: { items.0.create.status: 400 }
274269
- match: { items.0.create.error.type: illegal_argument_exception }
@@ -301,10 +296,10 @@ setup:
301296
template:
302297
mappings:
303298
properties:
304-
'timestamp':
299+
'@timestamp':
305300
type: date
306301
data_stream:
307-
timestamp_field: timestamp
302+
timestamp_field: '@timestamp'
308303

309304
- do:
310305
catch: bad_request
@@ -320,19 +315,19 @@ setup:
320315
- foo: bar
321316
- create:
322317
_index: logs-foobar
323-
- timestamp: '2020-12-12'
318+
- '@timestamp': '2020-12-12'
324319
- create:
325320
_index: logs-foobar
326-
- timestamp: ['2020-12-12', '2022-12-12']
321+
- '@timestamp': ['2020-12-12', '2022-12-12']
327322
- match: { errors: true }
328323
- match: { items.0.create.status: 400 }
329324
- match: { items.0.create.error.caused_by.type: illegal_argument_exception }
330-
- match: { items.0.create.error.caused_by.reason: "data stream timestamp field [timestamp] is missing" }
325+
- match: { items.0.create.error.caused_by.reason: "data stream timestamp field [@timestamp] is missing" }
331326
- match: { items.1.create.result: created }
332327
- match: { items.1.create._index: .ds-logs-foobar-000001 }
333328
- match: { items.2.create.status: 400 }
334329
- match: { items.2.create.error.caused_by.type: illegal_argument_exception }
335-
- match: { items.2.create.error.caused_by.reason: "data stream timestamp field [timestamp] encountered multiple values" }
330+
- match: { items.2.create.error.caused_by.reason: "data stream timestamp field [@timestamp] encountered multiple values" }
336331

337332
- do:
338333
indices.delete_data_stream:

rest-api-spec/src/main/resources/rest-api-spec/test/indices.data_stream/30_auto_create_data_stream.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@
1313
body:
1414
index_patterns: logs-*
1515
data_stream:
16-
timestamp_field: timestamp
16+
timestamp_field: '@timestamp'
1717
template:
1818
settings:
1919
number_of_shards: 1
2020
number_of_replicas: 0
2121
mappings:
2222
properties:
23-
timestamp:
23+
'@timestamp':
2424
type: date
2525

2626
- do:
2727
index:
2828
index: logs-foobar
2929
refresh: true
3030
body:
31-
'timestamp': '2020-12-12'
31+
'@timestamp': '2020-12-12'
3232
foo: bar
3333

3434
- do:
@@ -43,7 +43,7 @@
4343
indices.get_data_stream:
4444
name: logs-foobar
4545
- match: { data_streams.0.name: logs-foobar }
46-
- match: { data_streams.0.timestamp_field.name: 'timestamp' }
46+
- match: { data_streams.0.timestamp_field.name: '@timestamp' }
4747
- length: { data_streams.0.indices: 1 }
4848
- match: { data_streams.0.indices.0.index_name: '.ds-logs-foobar-000001' }
4949

rest-api-spec/src/main/resources/rest-api-spec/test/indices.resolve_index/10_basic_resolve_index.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ setup:
105105
- match: {data_streams.1.name: simple-data-stream2}
106106
- match: {data_streams.1.backing_indices.0: .ds-simple-data-stream2-000001}
107107
- match: {data_streams.1.backing_indices.1: .ds-simple-data-stream2-000002}
108-
- match: {data_streams.1.timestamp_field: "@timestamp2"}
108+
- match: {data_streams.1.timestamp_field: "@timestamp"}
109109

110110
- do:
111111
indices.delete_data_stream:
@@ -165,7 +165,7 @@ setup:
165165
- match: {data_streams.1.name: simple-data-stream2}
166166
- match: {data_streams.1.backing_indices.0: .ds-simple-data-stream2-000001}
167167
- match: {data_streams.1.backing_indices.1: .ds-simple-data-stream2-000002}
168-
- match: {data_streams.1.timestamp_field: "@timestamp2"}
168+
- match: {data_streams.1.timestamp_field: "@timestamp"}
169169

170170
- do:
171171
indices.delete_data_stream:

0 commit comments

Comments
 (0)