Is it possible to filter synonyms by site ?

Vote:
 

Hi

I know out of the box we can't filter synonyms by site

But I'm wondering if is it possible to filter by site using the tags, for instance I can add a synonyms adding values to the tags property (by code): 

Is it possible to query a search filtering the synonyms based on tags (or filter by site)? 

Thanks

#252186
Mar 30, 2021 12:33
Vote:
 

Well, for those interested in the answer, I just came accross this package : EPiServer.Labs.Find.Toolbox

Here's the approach from this package (extracted from the readme) "Currently the synonym expansion is done in backend (Elastic Search) and relies on a synonym index. We solve this by retrieving and caching the synonym list and expand the matching synonyms on the query, on the client side."

Neat, it means I can use the .UsingSynonymsImproved() and change the SynonymLoader to also filter by siteid, something like so : 

public Dictionary<String, HashSet<String>> GetSynonyms(int synonymBatchSize, RuntimeCacheAdapter cache, StaticCachePolicy staticCachePolicy)
{
	var statisticLanguageTags = GetStatisticLanguageTags().ToList();
	var httpRequest = HttpContext.Current.GetRequestContext().HttpContext;
	var site = GetSiteDefinitionByRequest(httpRequest.Request.Url);
	statisticLanguageTags.Add($"siteid:{site.Id}");

	string synonymCacheKey = GetSynonymCacheKey(statisticLanguageTags);

	Dictionary<String, HashSet<String>> synonymsCached;
	if (TryGetCachedSynonym(synonymCacheKey, cache, out synonymsCached))
	{
		return synonymsCached;
	}

	var loadedSynonyms = LoadSynonymsFromSourceIndex(synonymBatchSize, statisticLanguageTags);
	var synonymDictionary = CreateSynonymDictionary(loadedSynonyms);

	cache.AddOrUpdate(synonymCacheKey, staticCachePolicy, synonymDictionary);
	return synonymDictionary;
}

Adding Synonym is super easy (_settingsService is a service layer I use to call SiteDefinition)

public ActionResult AddSynonym(string phrase, string synonym)
{
	var site = _settingsService.GetSiteDefinitionByRequest(Request.Url);
	var synonymInstance = new EPiServer.Find.Optimizations.Synonyms.Api.Synonym(phrase, synonym);

	synonymInstance.Tags = (synonymInstance.Tags ?? new List<string>()).Concat(new string[]
		{
			//"language:en", do not add manually, use the parameter on .Add() otherwise it will add as all languages
			$"siteid:{site.Id}"
		});

	var result = _findClient
		.Optimizations()
		.Synonyms()
		.Add(synonymInstance, Language.English);

	return RedirectToAction("Index");
}

That's all.

#252190
Mar 30, 2021 18:53
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.