Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Shazwazza/881d1883ff0e3e2eca39ed19342467e6 to your computer and use it in GitHub Desktop.
Save Shazwazza/881d1883ff0e3e2eca39ed19342467e6 to your computer and use it in GitHub Desktop.
ExamineX Dynamic Language Analyzer Value Type
/// <summary>
/// Borrowed From Umbraco Source. Matches a culture iso name suffix
/// </summary>
/// <remarks>
/// myFieldName_en-us will match the "en-us"
/// </remarks>
public static readonly Regex CultureIsoCodeFieldNameMatchExpression = new Regex("^([_\\w]+)_([a-z]{2}-[a-z0-9]{2,4})$", RegexOptions.Compiled);
// A custom value type factory defining a value type called "DynamicCulture" which will
// dynamically assign a language analyzer to a field based on it's suffix name.
// To use this, you would use the DynamicCultureFieldDefinitionCollection
// to dynamically assign the "DynamicCulture" value type for a suffixed language field.
var customValueTypeFactory = new Dictionary<string, IAzureSearchFieldValueTypeFactory>
{
["DynamicCulture"] = new AzureSearchFieldValueTypeFactory(
fieldName =>
{
// Does this field have a culture suffix?
var match = CultureIsoCodeFieldNameMatchExpression.Match(fieldName);
if (match.Success && match.Groups.Count == 3)
{
// get the matched culture and return a value type with the
// correct language analyzer
var culture = match.Groups[2].Value;
switch(culture)
{
case "ja-ja":
return new AzureSearchFieldValueType(
fieldName,
AzureSearchFieldValueType.StringCollectionType,
AnalyzerName.AsString.JaMicrosoft);
case "it-it":
return new AzureSearchFieldValueType(
fieldName,
AzureSearchFieldValueType.StringCollectionType,
AnalyzerName.AsString.ItLucene);
// etc...
}
}
// return the default
return new AzureSearchFieldValueType(
fieldName,
AzureSearchFieldValueType.StringCollectionType,
AnalyzerName.AsString.StandardLucene,
false);
}),
};
public class DynamicCultureFieldDefinitionCollection : FieldDefinitionCollection
{
public DynamicCultureFieldDefinitionCollection()
{
}
public DynamicCultureFieldDefinitionCollection(params FieldDefinition[] definitions) : base(definitions)
{
}
public override bool TryGetValue(string fieldName, out FieldDefinition fieldDefinition)
{
// Does this field have a culture suffix?
var match = CultureIsoCodeFieldNameMatchExpression.Match(fieldName);
if (match.Success && match.Groups.Count == 3)
{
fieldDefinition = new FieldDefinition(fieldName, "DynamicCulture");
return true;
}
return base.TryGetValue(fieldName, out fieldDefinition);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment