From acd0d35a06dc37de35161922fcc90ce8abd42274 Mon Sep 17 00:00:00 2001 From: Jim Ferenczi Date: Mon, 9 Sep 2024 11:58:07 +0100 Subject: [PATCH] Support recovery source new setting in custom index mode This commit fixes the support of the recovery source setting added in #111824 for mappings that use a specific index mode and an explicit _source definition. --- .../index/mapper/SourceFieldMapper.java | 6 +- .../index/mapper/SourceFieldMapperTests.java | 67 +++++++++++++++++++ 2 files changed, 70 insertions(+), 3 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/index/mapper/SourceFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/SourceFieldMapper.java index 3ab466c1daa00..9dcb5142fa725 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/SourceFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/SourceFieldMapper.java @@ -236,9 +236,9 @@ public SourceFieldMapper build() { } if (isDefault()) { return switch (indexMode) { - case TIME_SERIES -> TSDB_DEFAULT; - case LOGSDB -> LOGSDB_DEFAULT; - default -> DEFAULT; + case TIME_SERIES -> enableRecoverySource ? TSDB_DEFAULT : TSDB_DEFAULT_NO_RECOVERY_SOURCE; + case LOGSDB -> enableRecoverySource ? LOGSDB_DEFAULT : LOGSDB_DEFAULT_NO_RECOVERY_SOURCE; + default -> enableRecoverySource ? DEFAULT : DEFAULT_NO_RECOVERY_SOURCE; }; } if (supportsNonDefaultParameterValues == false) { diff --git a/server/src/test/java/org/elasticsearch/index/mapper/SourceFieldMapperTests.java b/server/src/test/java/org/elasticsearch/index/mapper/SourceFieldMapperTests.java index d7a1e0431be8c..bbffc497d6ad1 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/SourceFieldMapperTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/SourceFieldMapperTests.java @@ -442,6 +442,28 @@ public void testRecoverySourceWithLogs() throws IOException { } } + public void testRecoverySourceWithLogsCustom() throws IOException { + XContentBuilder mappings = topMapping(b -> b.startObject(SourceFieldMapper.NAME).field("mode", "synthetic").endObject()); + { + Settings settings = Settings.builder().put(IndexSettings.MODE.getKey(), IndexMode.LOGSDB.getName()).build(); + MapperService mapperService = createMapperService(settings, mappings); + DocumentMapper docMapper = mapperService.documentMapper(); + ParsedDocument doc = docMapper.parse(source(b -> { b.field("@timestamp", "2012-02-13"); })); + assertNotNull(doc.rootDoc().getField("_recovery_source")); + assertThat(doc.rootDoc().getField("_recovery_source").binaryValue(), equalTo(new BytesRef("{\"@timestamp\":\"2012-02-13\"}"))); + } + { + Settings settings = Settings.builder() + .put(IndexSettings.MODE.getKey(), IndexMode.LOGSDB.getName()) + .put(INDICES_RECOVERY_SOURCE_ENABLED_SETTING.getKey(), false) + .build(); + MapperService mapperService = createMapperService(settings, mappings); + DocumentMapper docMapper = mapperService.documentMapper(); + ParsedDocument doc = docMapper.parse(source(b -> b.field("@timestamp", "2012-02-13"))); + assertNull(doc.rootDoc().getField("_recovery_source")); + } + } + public void testRecoverySourceWithTimeSeries() throws IOException { { Settings settings = Settings.builder() @@ -477,4 +499,49 @@ public void testRecoverySourceWithTimeSeries() throws IOException { assertNull(doc.rootDoc().getField("_recovery_source")); } } + + public void testRecoverySourceWithTimeSeriesCustom() throws IOException { + String mappings = """ + { + "_doc" : { + "_source" : { + "mode" : "synthetic" + }, + "properties": { + "field": { + "type": "keyword", + "time_series_dimension": true + } + } + } + } + """; + { + Settings settings = Settings.builder() + .put(IndexSettings.MODE.getKey(), IndexMode.TIME_SERIES.getName()) + .put(IndexMetadata.INDEX_ROUTING_PATH.getKey(), "field") + .build(); + MapperService mapperService = createMapperService(settings, mappings); + DocumentMapper docMapper = mapperService.documentMapper(); + ParsedDocument doc = docMapper.parse(source("123", b -> b.field("@timestamp", "2012-02-13").field("field", "value1"), null)); + assertNotNull(doc.rootDoc().getField("_recovery_source")); + assertThat( + doc.rootDoc().getField("_recovery_source").binaryValue(), + equalTo(new BytesRef("{\"@timestamp\":\"2012-02-13\",\"field\":\"value1\"}")) + ); + } + { + Settings settings = Settings.builder() + .put(IndexSettings.MODE.getKey(), IndexMode.TIME_SERIES.getName()) + .put(IndexMetadata.INDEX_ROUTING_PATH.getKey(), "field") + .put(INDICES_RECOVERY_SOURCE_ENABLED_SETTING.getKey(), false) + .build(); + MapperService mapperService = createMapperService(settings, mappings); + DocumentMapper docMapper = mapperService.documentMapper(); + ParsedDocument doc = docMapper.parse( + source("123", b -> b.field("@timestamp", "2012-02-13").field("field", randomAlphaOfLength(5)), null) + ); + assertNull(doc.rootDoc().getField("_recovery_source")); + } + } }