Skip to content

Commit ceed46e

Browse files
committed
fix(elasticsearch): canonicalize reindexed index wrappers
1 parent c3bbbaf commit ceed46e

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

src/Foundatio.Repositories.Elasticsearch/Configuration/CompatibilityIndexName.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ internal static ReadOnlySpan<char> GetCanonicalNameSpan(string index, string con
4646

4747
ReadOnlySpan<char> indexSpan = index;
4848
ReadOnlySpan<char> configuredSpan = configuredIndexName;
49+
bool hasCompatibilityPrefix = TryRemovePrefix(index, out ReadOnlySpan<char> canonicalName);
50+
if (hasCompatibilityPrefix
51+
&& (canonicalName.Equals(configuredSpan, StringComparison.Ordinal)
52+
|| (canonicalName.Length > configuredSpan.Length
53+
&& canonicalName.StartsWith(configuredSpan, StringComparison.Ordinal)
54+
&& canonicalName[configuredSpan.Length] is '-')))
55+
{
56+
return canonicalName;
57+
}
58+
4959
if (indexSpan.Equals(configuredSpan, StringComparison.Ordinal)
5060
|| (indexSpan.Length > configuredSpan.Length
5161
&& indexSpan.StartsWith(configuredSpan, StringComparison.Ordinal)
@@ -54,7 +64,7 @@ internal static ReadOnlySpan<char> GetCanonicalNameSpan(string index, string con
5464
return indexSpan;
5565
}
5666

57-
return TryRemovePrefix(index, out ReadOnlySpan<char> canonicalName) ? canonicalName : indexSpan;
67+
return hasCompatibilityPrefix ? canonicalName : indexSpan;
5868
}
5969

6070
internal static bool TryRemovePrefix(string index, out ReadOnlySpan<char> canonicalName)

tests/Foundatio.Repositories.Elasticsearch.Tests/IndexCompatibilityTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,19 @@ public void CompatibilityIndexName_ConfiguredPrefixPreservesNaturalChildName(str
614614
Assert.Equal(expected, canonicalName);
615615
}
616616

617+
[Fact]
618+
public void CompatibilityIndexName_ConfiguredReindexedName_StripsGeneratedWrapper()
619+
{
620+
const string configuredName = "reindexed";
621+
const string source = "reindexed-v1";
622+
const string target = "reindexed-v9-reindexed-v1";
623+
624+
Assert.Equal(source, CompatibilityIndexName.GetCanonicalName(source, configuredName));
625+
Assert.Equal(target, CompatibilityIndexName.Create(source, 9, configuredName));
626+
Assert.Equal(source, CompatibilityIndexName.GetCanonicalName(target, configuredName));
627+
Assert.Equal("reindexed-v10-reindexed-v1", CompatibilityIndexName.Create(target, 10, configuredName));
628+
}
629+
617630
[Theory]
618631
[InlineData(null, "7.17.19", 7)]
619632
[InlineData(null, "8.11.0", 8)]

0 commit comments

Comments
 (0)