Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix routing_path when template has multiple path_match and multi-fields #104418

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/changelog/104418.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 104418
summary: Fix `routing_path` when template has multiple `path_match` and multi-fields
area: TSDB
type: bug
issues:
- 104400
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import java.io.UncheckedIOException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

Expand Down Expand Up @@ -177,14 +177,18 @@ private List<String> findRoutingPaths(String indexName, Settings allSettings, Li
}

MappingParserContext parserContext = mapperService.parserContext();
for (String pathMatch : template.pathMatch()) {
for (Iterator<String> iterator = template.pathMatch().iterator(); iterator.hasNext();) {
var mapper = parserContext.typeParser(mappingSnippetType)
// Since FieldMapper.parse modifies the Map passed in (removing entries for "type"), that means
// that only the first pathMatch passed in gets recognized as a time_series_dimension. To counteract
// that, we wrap the mappingSnippet in a new HashMap for each pathMatch instance.
.parse(pathMatch, new HashMap<>(mappingSnippet), parserContext)
.parse(iterator.next(), mappingSnippet, parserContext)
.build(MapperBuilderContext.root(false, false));
extractPath(routingPaths, mapper);
if (iterator.hasNext()) {
// Since FieldMapper.parse modifies the Map passed in (removing entries for "type"), that means
// that only the first pathMatch passed in gets recognized as a time_series_dimension.
// To avoid this, each parsing call uses a new mapping snippet.
// Note that a shallow copy of the mappingSnippet map is not enough if there are multi-fields.
mappingSnippet = template.mappingForName(templateName, KeywordFieldMapper.CONTENT_TYPE);
}
}
}
return routingPaths;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,55 @@ public void testGenerateRoutingPathFromDynamicTemplateWithMultiplePathMatchEntri
assertEquals(3, routingPathList.size());
}

public void testGenerateRoutingPathFromDynamicTemplateWithMultiplePathMatchEntriesMultiFields() throws Exception {
Instant now = Instant.now().truncatedTo(ChronoUnit.SECONDS);
String mapping = """
{
"_doc": {
"dynamic_templates": [
{
"labels": {
"path_match": ["xprometheus.labels.*", "yprometheus.labels.*"],
"mapping": {
"type": "keyword",
"time_series_dimension": true,
"fields": {
"text": {
"type": "text"
}
}
}
}
}
],
"properties": {
"host": {
"properties": {
"id": {
"type": "keyword",
"time_series_dimension": true
}
}
},
"another_field": {
"type": "keyword"
}
}
}
}
""";
Settings result = generateTsdbSettings(mapping, now);
assertThat(result.size(), equalTo(3));
assertThat(IndexSettings.TIME_SERIES_START_TIME.get(result), equalTo(now.minusMillis(DEFAULT_LOOK_BACK_TIME.getMillis())));
assertThat(IndexSettings.TIME_SERIES_END_TIME.get(result), equalTo(now.plusMillis(DEFAULT_LOOK_AHEAD_TIME.getMillis())));
assertThat(
IndexMetadata.INDEX_ROUTING_PATH.get(result),
containsInAnyOrder("host.id", "xprometheus.labels.*", "yprometheus.labels.*")
);
List<String> routingPathList = IndexMetadata.INDEX_ROUTING_PATH.get(result);
assertEquals(3, routingPathList.size());
}

public void testGenerateRoutingPathFromDynamicTemplate_templateWithNoPathMatch() throws Exception {
Instant now = Instant.now().truncatedTo(ChronoUnit.SECONDS);
String mapping = """
Expand Down