Read my blog here
Created
January 27, 2025 08:17
-
-
Save jstemerdink/5f398cd3f0c30b6fba0d029ca6e8f6e0 to your computer and use it in GitHub Desktop.
Make Best Bets and synonyms work with wildcard queries in Optimizely Search and Navigation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static IQueriedSearch<TSource, QueryStringQuery> UsingSynonyms<TSource>( | |
this IQueriedSearch<TSource> search, string queryString) | |
{ | |
search.ValidateNotNullArgument(nameof (search)); | |
if (search.Client.Settings.Admin) | |
{ | |
return new Search<TSource, QueryStringQuery>(search, context => | |
{ | |
if (!(context.RequestBody.Query is QueryStringQuery query2)) | |
{ | |
Trace.Instance.Add(new TraceEvent(search, "The use of synonyms are only supported för QueryStringQueries, i.e. with the use of the .For() -extensions. The query will be executed without the use of synonyms.") | |
{ | |
IsError = false | |
}); | |
} | |
else | |
{ | |
string str = queryString ?? string.Empty; | |
((QueryStringQuery) context.RequestBody.Query).Query = str.Replace(' ', '¤'); | |
MultiFieldQueryStringQuery queryStringQuery = query2 as MultiFieldQueryStringQuery; | |
if (queryStringQuery.IsNull()) | |
{ | |
((QueryStringQuery) context.RequestBody.Query).Analyzer = "synonym"; | |
} | |
else if (queryStringQuery.Fields.Except<string>(["_all"]).Any<string>()) | |
{ | |
((QueryStringQuery) context.RequestBody.Query).Analyzer = context.Analyzer?.SynonymQueryAnalyzer; | |
} | |
else | |
{ | |
((QueryStringQuery) context.RequestBody.Query).Analyzer = "synonym"; | |
} | |
} | |
}); | |
} | |
Trace.Instance.Add(new TraceEvent(search, "Your index does not support synonyms. Please contact support to have your account upgraded. Falling back to search without synonyms.") | |
{ | |
IsError = false | |
}); | |
return new Search<TSource, QueryStringQuery>(search, context => { }); | |
} | |
public static IQueriedSearch<TSource, QueryStringQuery> UsingSynonyms<TSource>( | |
this IQueriedSearch<TSource> search) | |
{ | |
search.ValidateNotNullArgument(nameof (search)); | |
if (search.Client.Settings.Admin) | |
return (IQueriedSearch<TSource, QueryStringQuery>) new Search<TSource, QueryStringQuery>((ISearch) search, (Action<ISearchContext>) (context => | |
{ | |
if (!(context.RequestBody.Query is QueryStringQuery query2)) | |
{ | |
Trace.Instance.Add((ITraceEvent) new TraceEvent((ITraceable) search, "The use of synonyms are only supported för QueryStringQueries, i.e. with the use of the .For() -extensions. The query will be executed without the use of synonyms.") | |
{ | |
IsError = false | |
}); | |
} | |
else | |
{ | |
string str = (query2.Query ?? (FieldFilterValue) string.Empty).ToString(); | |
// If you used the reverse method as described in https://docs.developers.optimizely.com/digital-experience-platform/v1.1.0-search-and-navigation/docs/breaking-changes-for-find15 | |
// You will need to reverse that again, else no match will be found ever | |
// new string(searchPhrase.Reverse().ToArray()) | |
// Remove wildcard | |
string cleanedSearchPhrase = str.Replace("*", string.Empty).Replace("?", string.Empty); | |
((QueryStringQuery) context.RequestBody.Query).Query = (FieldFilterValue) cleanedSearchPhrase.Replace(' ', '¤'); | |
MultiFieldQueryStringQuery queryStringQuery = query2 as MultiFieldQueryStringQuery; | |
if (queryStringQuery.IsNull()) | |
((QueryStringQuery) context.RequestBody.Query).Analyzer = "synonym"; | |
else if (queryStringQuery.Fields.Except<string>((IEnumerable<string>) new string[1] | |
{ | |
"_all" | |
}).Any<string>()) | |
((QueryStringQuery) context.RequestBody.Query).Analyzer = context.Analyzer?.SynonymQueryAnalyzer; | |
else | |
((QueryStringQuery) context.RequestBody.Query).Analyzer = "synonym"; | |
} | |
})); | |
Trace.Instance.Add((ITraceEvent) new TraceEvent((ITraceable) search, "Your index does not support synonyms. Please contact support to have your account upgraded. Falling back to search without synonyms.") | |
{ | |
IsError = false | |
}); | |
return (IQueriedSearch<TSource, QueryStringQuery>) new Search<TSource, QueryStringQuery>((ISearch) search, (Action<ISearchContext>) (context => { })); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[EPiServerDataStore( | |
AutomaticallyRemapStore = true, | |
AutomaticallyCreateStore = true, | |
StoreName = "EPiServer.Find.Framework.BestBets.DefaultPhraseCriterion" | |
)] | |
public class WildCardPhraseCriterion : DefaultPhraseCriterion, IPhraseCriterion | |
{ | |
public override bool Match(string searchPhrase) | |
{ | |
if (string.IsNullOrWhiteSpace(searchPhrase) || Phrase == null) | |
{ | |
return false; | |
} | |
// Remove wildcard | |
string cleanedSearchPhrase = searchPhrase.Replace("*", string.Empty).Replace("?", string.Empty); | |
return base.Match(cleanedSearchPhrase); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment