Skip to content

Toolbox

This page aims to give you the tools to accomplish certain common tasks around Relewise.

If you wish to see further examples added here, please reach out to us at support@relewise.com with your suggestions.

Copying Search Settings between Datasets

With this, you can copy the setup of your search settings from one dataset to another. Useful for, e.g., moving your settings from your development to your production dataset.

csharp

void Main()
{
	string initiator = "jane@doe.com";
	bool cleanupDestination = true; // This will delete any rule/setting at the copyTo dataset that doesn't exist in the copyFrom dataset

	var copyFrom = new SearchAdministrator(new Guid("..."), "apikey", "serverurl", TimeSpan.FromMinutes(2));
	var copyTo = new SearchAdministrator(new Guid("..."), "apikey", "serverurl", TimeSpan.FromMinutes(2));

	CopySearchIndexes(copyFrom, copyTo, initiator, cleanupDestination);
	CopySynonyms(copyFrom, copyTo, initiator, cleanupDestination);
	CopyDecompoundRules(copyFrom, copyTo, initiator, cleanupDestination);
	CopyStemmingRules(copyFrom, copyTo, initiator, cleanupDestination);
	CopySearchTermRules(copyFrom, copyTo, initiator, cleanupDestination);
	CopySearchResultModifierRules(copyFrom, copyTo, initiator, cleanupDestination);
}

void CopySearchIndexes(SearchAdministrator from, SearchAdministrator to, string initiator, bool cleanupDestination)
{
	var copyFromIndexes = RetrieveAllIndexConfigurations(from);

	if (cleanupDestination)
	{
		var copyToIndexes = RetrieveAllIndexConfigurations(to);

		if (copyToIndexes.Any())
		{
			var indexesToDelete = copyToIndexes.Where(copyToIndex => !copyFromIndexes.Any(copyFromIndex => copyFromIndex.Id == copyToIndex.Id));

			if (indexesToDelete.Any())
			{
				foreach (var index in indexesToDelete)
				{
					to.Delete(new DeleteSearchIndexRequest(index.Id, initiator));
				}
			}
		}
	}

	foreach (var index in copyFromIndexes)
	{
		to.Save(new SaveSearchIndexRequest(index, initiator));
	}

	$"Copied {copyFromIndexes.Length} search indexes from {from.DatasetId} to {to.DatasetId}".Dump();
}

void CopySynonyms(SearchAdministrator from, SearchAdministrator to, string initiator, bool cleanupDestination)
{
	var copyFromSynonyms = RetrieveAllSynonyms(from);

	if (cleanupDestination)
	{
		var copyToSynonyms = RetrieveAllSynonyms(to);

		if (copyToSynonyms.Any())
		{
			var synonymsToDelete = copyToSynonyms.Where(copyToSynonym => !copyFromSynonyms.Any(copyFromSynonym => copyFromSynonym.Id == copyToSynonym.Id));

			if (synonymsToDelete.Any())
			{
				to.Delete(new DeleteSynonymsRequest(synonymsToDelete.Select(s => s.Id).ToArray(), initiator));
			}
		}
	}

	to.Save(new SaveSynonymsRequest(copyFromSynonyms.ToArray(), initiator));

	$"Copied {copyFromSynonyms.Count} synonyms from {from.DatasetId} to {to.DatasetId}".Dump();
}

void CopyDecompoundRules(SearchAdministrator from, SearchAdministrator to, string initiator, bool cleanupDestination)
{
	var copyFromRules = RetrieveAllDecompoundRules(from);

	if (cleanupDestination)
	{
		var copyToRules = RetrieveAllDecompoundRules(to);

		if (copyToRules.Any())
		{
			var rulesToDelete = copyToRules.Where(copyToRule => !copyFromRules.Any(copyFromRule => copyFromRule.Id == copyToRule.Id));

			if (rulesToDelete.Any())
			{
				to.Delete(new DeleteDecompoundRulesRequest(rulesToDelete.Select(s => s.Id).ToArray(), initiator));
			}
		}
	}

	to.Save(new SaveDecompoundRulesRequest(copyFromRules.ToArray(), initiator));

	$"Copied {copyFromRules.Count} decompound rules from {from.DatasetId} to {to.DatasetId}".Dump();
}

void CopyStemmingRules(SearchAdministrator from, SearchAdministrator to, string initiator, bool cleanupDestination)
{
	var copyFromRules = RetrieveAllStemmingRules(from);

	if (cleanupDestination)
	{
		var copyToRules = RetrieveAllStemmingRules(to);

		if (copyToRules.Any())
		{
			var rulesToDelete = copyToRules.Where(copyToRule => !copyFromRules.Any(copyFromRule => copyFromRule.Id == copyToRule.Id));

			if (rulesToDelete.Any())
			{
				to.Delete(new DeleteStemmingRulesRequest(rulesToDelete.Select(s => s.Id).ToArray(), initiator));
			}
		}
	}

	to.Save(new SaveStemmingRulesRequest(copyFromRules.ToArray(), initiator));

	$"Copied {copyFromRules.Count} stemming rules from {from.DatasetId} to {to.DatasetId}".Dump();
}

void CopySearchTermRules(SearchAdministrator from, SearchAdministrator to, string initiator, bool cleanupDestination)
{
	var copyFromRules = RetrieveAllSearchTermRules(from);

	if (cleanupDestination)
	{
		var copyToRules = RetrieveAllSearchTermRules(to);

		if (copyToRules.Any())
		{
			var rulesToDelete = copyToRules.Where(copyToRule => !copyFromRules.Any(copyFromRule => copyFromRule.Id == copyToRule.Id));

			if (rulesToDelete.Any())
			{
				to.Delete(new DeleteSearchTermModifierRulesRequest(rulesToDelete.Select(s => s.Id).ToArray(), initiator));
			}
		}
	}

	to.Save(new SaveSearchTermModifierRulesRequest(copyFromRules.ToArray(), initiator));

	$"Copied {copyFromRules.Count} search term modifier rules from {from.DatasetId} to {to.DatasetId}".Dump();
}

void CopySearchResultModifierRules(SearchAdministrator from, SearchAdministrator to, string initiator, bool cleanupDestination)
{
	var copyFromRules = RetrieveAllSearchResultModifierRules(from);

	if (cleanupDestination)
	{
		var copyToRules = RetrieveAllSearchResultModifierRules(to);

		if (copyToRules.Any())
		{
			var rulesToDelete = copyToRules.Where(copyToRule => !copyFromRules.Any(copyFromRule => copyFromRule.Id == copyToRule.Id));

			if (rulesToDelete.Any())
			{
				to.Delete(new DeleteSearchResultModifierRulesRequest(rulesToDelete.Select(s => s.Id).ToArray(), initiator));
			}
		}
	}

	to.Save(new SaveSearchResultModifierRulesRequest(copyFromRules.ToArray(), initiator));

	$"Copied {copyFromRules.Count} search result modifier rules from {from.DatasetId} to {to.DatasetId}".Dump();
}

SearchIndex[] RetrieveAllIndexConfigurations(SearchAdministrator admin)
{
	var result = admin.Load(new SearchIndexesRequest());
	return result.Indexes;
}

List<Synonym> RetrieveAllSynonyms(SearchAdministrator admin)
{
	List<Synonym> synonyms = new List<Synonym>();

	int count = 0;
	int total = 0;

	do
	{
		var result = admin.Load(new SynonymsRequest() { Take = 1000, Skip = count });

		if (total == 0)
			total = result.Hits;

		synonyms.AddRange(result.Values);
		count += result.Values.Length;
	}
	while (count < total);

	return synonyms;
}

List<DecompoundRule> RetrieveAllDecompoundRules(SearchAdministrator admin)
{
	List<DecompoundRule> rules = new List<DecompoundRule>();

	int count = 0;
	int total = 0;

	do
	{
		var result = admin.Load(new DecompoundRulesRequest() { Take = 1000, Skip = count });

		if (total == 0)
			total = result.Hits;

		rules.AddRange(result.Rules);
		count += result.Rules.Length;
	}
	while (count < total);

	return rules;
}

List<StemmingRule> RetrieveAllStemmingRules(SearchAdministrator admin)
{
	List<StemmingRule> rules = new List<StemmingRule>();

	int count = 0;
	int total = 0;

	do
	{
		var result = admin.Load(new StemmingRulesRequest() { Take = 1000, Skip = count });

		if (total == 0)
			total = result.Hits;

		rules.AddRange(result.Rules);
		count += result.Rules.Length;
	}
	while (count < total);

	return rules;
}

List<SearchTermModifierRule> RetrieveAllSearchTermRules(SearchAdministrator admin)
{
	List<SearchTermModifierRule> rules = new List<SearchTermModifierRule>();

	int count = 0;
	int total = 0;

	do
	{
		var result = admin.Load(new SearchTermModifierRulesRequest() { Take = 1000, Skip = count });

		if (total == 0)
			total = result.Hits;

		rules.AddRange(result.Rules);
		count += result.Rules.Length;
	}
	while (count < total);

	return rules;
}

List<SearchResultModifierRule> RetrieveAllSearchResultModifierRules(SearchAdministrator admin)
{
	List<SearchResultModifierRule> rules = new List<SearchResultModifierRule>();

	int count = 0;
	int total = 0;

	do
	{
		var result = admin.Load(new SearchResultModifierRulesRequest() { Take = 1000, Skip = count });

		if (total == 0)
			total = result.Hits;

		rules.AddRange(result.Rules);
		count += result.Rules.Length;
	}
	while (count < total);

	return rules;
}

Don't know us? Don't worry - you can find more information about us, by visiting our main page www.relewise.com