Last active
May 7, 2021 17:36
-
-
Save Shazwazza/881d1883ff0e3e2eca39ed19342467e6 to your computer and use it in GitHub Desktop.
ExamineX Dynamic Language Analyzer Value Type
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
/// <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); |
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
// 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); | |
}), | |
}; |
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 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