Appearance
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.
Copy Search Settings
Sometimes, you may want to transfer various parts of your search settings between datasets - such as when you want to synchronize your production dataset to your test, or vice versa.
The tools below are designed to copy the following: Search Index, Synonyms, Decompounding Rules, Stemming Rules, Search Term Modifiers, and Search Result Modifiers. You may run each part separately, or combine them at your leisure.
Click here to read more about Search settings and what they mean.
Backup of Data
Please keep in mind that Relewise does not keep backups of your dataset on hand. If you use these scripts to override a search setting, it cannot be undone - please make sure that you are completely certain you want to perform this action before you commit to it.
Toolbox: Initiate the code
To start with, initiate the code, and specify which search settings you wish to copy. The following code contains all six settings; make sure you edit it to contain only the settings you wish to copy.
To access the relevant data on Dataset ID, API key and Serverurl, refer to the Administration page in MyRelewise.
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", requestTimeoutInSeconds: TimeSpan.FromMinutes(2));
var copyTo = new SearchAdministrator(new Guid("..."), "apikey", "serverurl", requestTimeoutInSeconds: 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);
}
Copy Search Index
This code will copy the Search Index. Be aware that you may accomplish this via the MyRelewise UI as well; if all you wish to copy is the search index, you may more easily do so via the UI.
csharp
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();
}
SearchIndex[] RetrieveAllIndexConfigurations(SearchAdministrator admin)
{
var result = admin.Load(new SearchIndexesRequest());
return result.Indexes;
}
Copy Synonyms
This code will copy all Synonyms, active as well as inactive, to the designated dataset.
csharp
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();
}
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;
}
Copy Decompounding Rules
This code will copy all Decompounding rules, active as well as inactive, to the designated dataset.
csharp
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));
}
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;
}
Copy Stemming Rules
This code will copy all Stemming rules, active as well as inactive, to the designated dataset.
csharp
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();
}
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;
}
Copy Search Term Modifiers
This code will copy all Searh Term Modifiers, active as well as inactive, to the designated dataset.
csharp
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));
}
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;
}
Copy Search Result Modifiers
This code will copy all Search Result Modifiers, active as well as inactive, to the designated dataset.
csharp
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));
}
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;
}