Skip to content

Commit

Permalink
allow create alias when index does not exist in typesense
Browse files Browse the repository at this point in the history
  • Loading branch information
samihafidiDLW committed Aug 13, 2024
1 parent efa3bd2 commit 03920f5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Authors>$(Company)</Authors>
<Copyright>Copyright © $(Company) $([System.DateTime]::Now.Year)</Copyright>
<Trademark>$(Company)™</Trademark>
<VersionPrefix>1.0.7-beta-1</VersionPrefix>
<VersionPrefix>1.0.8-beta-1</VersionPrefix>
<VersionSuffix></VersionSuffix>
<PackageLicenseExpression>MIT</PackageLicenseExpression>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Collections;

using CMS.ContentEngine;
using CMS.Core;
using CMS.DataEngine;
Expand Down Expand Up @@ -115,34 +117,16 @@ public async Task<ICollection<TypesenseCollectionAliasViewModel>> GetAliases(Can
}

/// <inheritdoc />
public async Task Rebuild(string collectionName, CancellationToken cancellationToken)
public Task Rebuild(string collectionName, CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(collectionName))
{
throw new ArgumentNullException(nameof(collectionName));
}

TypesenseCollection? typesenseCollection = null;
try
{
typesenseCollection = TypesenseCollectionStore.Instance.GetRequiredCollection(collectionName);
}
//index does not exist
catch (InvalidOperationException)
{
bool isCreated = await TryCreateCollectionInternal(collectionName);
if (isCreated)
{
typesenseCollection = TypesenseCollectionStore.Instance.GetRequiredCollection(collectionName);
}
}

if (typesenseCollection is null)
{
throw new Exception($"typesenseCollection is null. Could not get {collectionName}");
}
var typesenseCollection = TypesenseCollectionStore.Instance.GetRequiredCollection(collectionName);

await RebuildInternal(typesenseCollection, cancellationToken);
return RebuildInternal(typesenseCollection, cancellationToken);
}

/// <inheritdoc />
Expand Down Expand Up @@ -226,6 +210,16 @@ private async Task RebuildInternal(TypesenseCollection typesenseCollection, Canc
}
}
}

try
{
await searchClient.RetrieveCollection(typesenseCollection.CollectionName, cancellationToken);
}
catch (TypesenseApiNotFoundException)
{
await TryCreateCollectionInternal(typesenseCollection.CollectionName);
}

await searchClient.DeleteDocuments(typesenseCollection.CollectionName, $"{BaseObjectProperties.ID}: &gt;= 0");

var (activeCollectionName, newCollectionName) = await GetCollectionNames(typesenseCollection.CollectionName);
Expand Down Expand Up @@ -349,15 +343,11 @@ private async Task<bool> TryCreateCollectionInternal(string collectionName)
var typesenseStrategy = serviceProvider.GetRequiredStrategy(typesenseCollection);
var indexSettings = await typesenseStrategy.GetTypesenseCollectionSettings();

var createdCollection =
await searchClient.CreateCollection(indexSettings.ToSchema($"{collectionName}-primary"));
if (createdCollection == null)
{
return false;
}
await searchClient.CreateCollection(indexSettings.ToSchema($"{collectionName}-primary"));

await searchClient.UpsertCollectionAlias(collectionName,
new CollectionAlias($"{collectionName}-primary"));

return true;
}

Expand Down Expand Up @@ -474,10 +464,6 @@ public async Task SwapAliasWhenRebuildIsDone(string alias, string newCollectionR
public async Task<(string activeCollectionName, string newCollectionName)> GetCollectionNames(string collectionName)
{
var currentCollection = await searchClient.RetrieveCollection(collectionName); //Search by alias the current collection
if (currentCollection == null)
{
return (string.Empty, string.Empty);
}

string newCollectionName = $"{collectionName}-primary"; //Default to primary
if (currentCollection.Name.EndsWith("-primary"))
Expand Down

0 comments on commit 03920f5

Please sign in to comment.