You can use synonyms to improve search results by adding uni- or bi-directional relationships between phrases. This topic explains how to manage synonyms programmatically in Episerver Find.
Note: To learn how to add synonyms through the administrative interface, see Adding synonyms for similar phrases.
To enable search results to use synonyms, use the UsingSynonyms extension method for IQueriedSearch in EPiServer.Find namespace:
var client = SearchClient.Instance; var results = client.Search<FashionProduct>(Language.English) .For("puma") .InFields(x => x.Brand) .UsingSynonyms() .Take(100) .GetContentResult();
A synonym has an id to use on update and removal. The id is defined by
The synonyms client is made available through the extension method Synonyms on the Find client.
using EPiServer.Find.Framework; using EPiServer.Find.Optimizations; var client = SearchClient.Instance;
var result = client.Optimizations().Synonyms().Add(new Synonym("bike", " bicycle ", true)); var synonymId = result.Id;
In this example, bike is the phrase, bicycle is the synonym, and true indicates that the relationship is bidirectional.
To get a synonym, pass its id to the Get method of the Synonyms client.
var result = client.Optimizations().Synonyms().Get(synonymId);
To delete a synonym, pass its id to the Delete method of the Synonyms client.
var result = client.Optimizations().Synonyms().Delete(synonymId);
To retrieve synonyms, use the List method, which has size and from arguments to allow paging.
var size = 10; var from = 120; var result = client.Optimizations().Synonyms().List(size, from);
Size indicates the number of synonyms to return per page. From indicates the synonym at which to start the list.
The default size value is 10, and the default from value is 0. If you run client.Synonyms().List() with no parameters, it returns the first 10 synonyms.
The Add, Get, Delete and List methods have overloads for passing a commandAction, which is run with the due command as an argument just before command execution.
var result = client.Optimizations().Synonyms().Delete(synonymId, command => _log.Debug(command.Id));
Last updated: Mar 27, 2019