Skip to content

Commit 6640cdd

Browse files
committed
Update regex & remove extra reference
1 parent 4d971ce commit 6640cdd

File tree

3 files changed

+59
-44
lines changed

3 files changed

+59
-44
lines changed

server/src/main/java/org/elasticsearch/cluster/metadata/DataStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public final class DataStream implements SimpleDiffable<DataStream>, ToXContentO
100100
public static final DateFormatter DATE_FORMATTER = DateFormatter.forPattern("uuuu.MM.dd");
101101
public static final String TIMESTAMP_FIELD_NAME = "@timestamp";
102102
public static final Pattern DS_BACKING_PATTERN = Pattern.compile(
103-
"^(.*?" + BACKING_INDEX_PREFIX + ")(.+)-(\\d{4}.\\d{2}.\\d{2})(-[\\d]+)?$"
103+
"^(.*?" + BACKING_INDEX_PREFIX + ")(.+)-(\\d{4}.\\d{2}.\\d{2})(-[\\d]+)$"
104104
);
105105

106106
// Timeseries indices' leaf readers should be sorted by desc order of their timestamp field, as it allows search time optimizations

server/src/test/java/org/elasticsearch/cluster/metadata/DataStreamTests.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2715,21 +2715,28 @@ public void testGetEffectiveIndexTemplateDataStreamMappingsOnly() throws IOExcep
27152715
assertThat(dataStream.getEffectiveIndexTemplate(projectMetadataBuilder.build()), equalTo(expectedEffectiveTemplate));
27162716
}
27172717

2718-
public void testGetDataStreamNameFromValidBackingIndex() {
2718+
public void testGetDataStreamNameFromValidBackingIndices() {
27192719
// Test a valid backing index name with correct format: .ds-<data-stream>-<yyyy.MM.dd>-<generation>
27202720
String indexName = ".ds-my-service-logs-2024.02.05-000001";
27212721
String dataStreamName = DataStream.getDataStreamNameFromIndex(indexName);
27222722

27232723
assertEquals("my-service-logs", dataStreamName);
2724+
2725+
// Test valid backing index with extra '-' dash in the name
2726+
indexName = ".ds-my-service-logs-two-2024.02.05-000001";
2727+
dataStreamName = DataStream.getDataStreamNameFromIndex(indexName);
2728+
2729+
assertEquals("my-service-logs-two", dataStreamName);
2730+
27242731
}
27252732

27262733
public void testGetDataStreamNameFromInvalidBackingIndex() {
27272734
// Test cases that should not be recognized as valid backing indices
27282735
String[] invalidNames = {
27292736
"not-a-backing-index", // No .ds- prefix
27302737
".ds-", // Missing data stream name
2731-
".ds-logs", // Missing date and generation
2732-
".ds-logs-2024.02.05", // Missing generation
2738+
".ds-my-service-logs", // Missing date and generation
2739+
".ds-my-service-logs-2024.02.05", // Missing generation
27332740
};
27342741

27352742
for (String invalidName : invalidNames) {

server/src/test/java/org/elasticsearch/search/SearchHitTests.java

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -482,52 +482,60 @@ public void testShardTargetSetsDataStreamName() {
482482
// Create a SearchHit
483483
SearchHit hit = new SearchHit(1);
484484

485-
// Create a backing index name following the pattern: .ds-<data-stream>-<yyyy.MM.dd>-<generation>
486-
String backingIndexName = ".ds-my-service-logs-2024.02.05-000001";
487-
String nodeId = "node1";
488-
int shardId = 0;
489-
String clusterAlias = null;
490-
491-
// Create SearchShardTarget with the backing index name
492-
SearchShardTarget target = new SearchShardTarget(
493-
nodeId,
494-
new ShardId(new Index(backingIndexName, IndexMetadata.INDEX_UUID_NA_VALUE), shardId),
495-
clusterAlias
496-
);
497-
498-
// Set the shard target
499-
hit.shard(target);
500-
501-
// Verify the data-stream field is set correctly
502-
assertEquals("my-service-logs", hit.getDataStream());
503-
assertEquals(backingIndexName, hit.getIndex());
504-
assertNull(hit.getClusterAlias());
485+
try {
486+
// Create a backing index name following the pattern: .ds-<data-stream>-<yyyy.MM.dd>-<generation>
487+
String backingIndexName = ".ds-my-service-logs-2024.02.05-000001";
488+
String nodeId = "node1";
489+
int shardId = 0;
490+
String clusterAlias = null;
491+
492+
// Create SearchShardTarget with the backing index name
493+
SearchShardTarget target = new SearchShardTarget(
494+
nodeId,
495+
new ShardId(new Index(backingIndexName, IndexMetadata.INDEX_UUID_NA_VALUE), shardId),
496+
clusterAlias
497+
);
498+
499+
// Set the shard target
500+
hit.shard(target);
501+
502+
// Verify the data-stream field is set correctly
503+
assertEquals("my-service-logs", hit.getDataStream());
504+
assertEquals(backingIndexName, hit.getIndex());
505+
assertNull(hit.getClusterAlias());
506+
} finally {
507+
hit.decRef();
508+
}
505509
}
506510

507511
public void testShardTargetWithNonDataStreamIndex() {
508512
// Create a SearchHit
509513
SearchHit hit = new SearchHit(1);
510514

511-
// Create a regular index name (not a backing index)
512-
String regularIndexName = "regular-index";
513-
String nodeId = "node1";
514-
int shardId = 0;
515-
String clusterAlias = null;
516-
517-
// Create SearchShardTarget with a non-data-stream-backed index name
518-
SearchShardTarget target = new SearchShardTarget(
519-
nodeId,
520-
new ShardId(new Index(regularIndexName, IndexMetadata.INDEX_UUID_NA_VALUE), shardId),
521-
clusterAlias
522-
);
523-
524-
// Set the shard target
525-
hit.shard(target);
526-
527-
// Verify the data-stream field is null for non-backing indices
528-
assertNull(hit.getDataStream());
529-
assertEquals(regularIndexName, hit.getIndex());
530-
assertNull(hit.getClusterAlias());
515+
try {
516+
// Create a regular index name (not a backing index)
517+
String regularIndexName = "regular-index";
518+
String nodeId = "node1";
519+
int shardId = 0;
520+
String clusterAlias = null;
521+
522+
// Create SearchShardTarget with a non-data-stream-backed index name
523+
SearchShardTarget target = new SearchShardTarget(
524+
nodeId,
525+
new ShardId(new Index(regularIndexName, IndexMetadata.INDEX_UUID_NA_VALUE), shardId),
526+
clusterAlias
527+
);
528+
529+
// Set the shard target
530+
hit.shard(target);
531+
532+
// Verify the data-stream field is null for non-backing indices
533+
assertNull(hit.getDataStream());
534+
assertEquals(regularIndexName, hit.getIndex());
535+
assertNull(hit.getClusterAlias());
536+
} finally {
537+
hit.decRef();
538+
}
531539
}
532540

533541
}

0 commit comments

Comments
 (0)