Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save netaddicts/0c64efdde44cbebe55013238ce8a5763 to your computer and use it in GitHub Desktop.

Select an option

Save netaddicts/0c64efdde44cbebe55013238ce8a5763 to your computer and use it in GitHub Desktop.
ExamineX Setting analyzers for variant fields
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Microsoft.Azure.Search.Models;
namespace Presite.Core.Startup.Composers.Indexing
{
public class CultureIsoCodeFieldNameMatchExpression
{
/// <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 IsoCodeExpression = new Regex("^([_\\w]+)_([a-z]{2})$", RegexOptions.Compiled);
public static readonly Dictionary<string, AnalyzerName> Analyzers = new Dictionary<string, AnalyzerName>
{
{"it", AnalyzerName.ItMicrosoft}, {"de", AnalyzerName.DeMicrosoft}, {"sl", AnalyzerName.SlMicrosoft},
{"fr", AnalyzerName.FrMicrosoft}, {"en", AnalyzerName.EnMicrosoft}, {"hu", AnalyzerName.HuMicrosoft}
};
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using Examine;
using ExamineX.AzureSearch;
using ExamineX.AzureSearch.Umbraco;
using ExamineX.Shared;
using Microsoft.Azure.Search.Models;
using Umbraco.Core;
using Umbraco.Core.Composing;
using Umbraco.Core.Services;
using Umbraco.Examine;
using Umbraco.Web.Search;
namespace Presite.Core.Startup.Composers.Indexing
{
[ComposeAfter(typeof(ExamineXComposer))]
[RuntimeLevel(MinLevel = RuntimeLevel.Run)]
public class MyComposer : IUserComposer
{
public void Compose(Composition composition)
{
// Replace the default ExamineX implementation
composition.RegisterUnique<IUmbracoIndexesCreator, MyUmbracoIndexesCreator>();
}
}
public class MyUmbracoIndexesCreator : ExamineXIndexFactory
{
protected readonly ILicenseManager _licenceManager;
protected readonly IExamineXLogger _logger;
protected readonly ExamineXConfig _examineXConfig;
protected readonly IRuntimeState _runtimeState;
protected readonly IUmbracoIndexConfig _umbracoIndexConfig;
public MyUmbracoIndexesCreator(
ExamineXConfig examineXConfig, IUmbracoIndexConfig umbracoIndexConfig, IExamineXLogger logger,
ILicenseManager licenceManager, IRuntimeState runtimeState, UmbracoIndexesCreator defaultFactory)
: base(examineXConfig, umbracoIndexConfig, logger, licenceManager, runtimeState, defaultFactory)
{
_umbracoIndexConfig = umbracoIndexConfig;
_examineXConfig = examineXConfig;
_licenceManager = licenceManager;
_logger = logger;
_runtimeState = runtimeState;
}
// Replace the default analyzer for the external index to be
// Microsoft's English Analyzer
protected override IIndex CreateExternalIndex(string defaultAnalyzer)
{
// 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.IsoCodeExpression.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 "de":
return new AzureSearchFieldValueType(
fieldName,
AzureSearchFieldValueType.StringCollectionType,
AnalyzerName.AsString.DeMicrosoft);
case "fr":
return new AzureSearchFieldValueType(
fieldName,
AzureSearchFieldValueType.StringCollectionType,
AnalyzerName.AsString.FrMicrosoft);
case "sl":
return new AzureSearchFieldValueType(
fieldName,
AzureSearchFieldValueType.StringCollectionType,
AnalyzerName.AsString.SlMicrosoft);
case "hu":
return new AzureSearchFieldValueType(
fieldName,
AzureSearchFieldValueType.StringCollectionType,
AnalyzerName.AsString.HuMicrosoft);
case "en":
return new AzureSearchFieldValueType(
fieldName,
AzureSearchFieldValueType.StringCollectionType,
AnalyzerName.AsString.EnMicrosoft);
case "it":
return new AzureSearchFieldValueType(
fieldName,
AzureSearchFieldValueType.StringCollectionType,
AnalyzerName.AsString.ItMicrosoft);
}
}
// return the default
return new AzureSearchFieldValueType(
fieldName,
AzureSearchFieldValueType.StringCollectionType,
AnalyzerName.AsString.StandardLucene,
false);
}),
};
var index = new UmbracoAzureSearchContentIndex(
Umbraco.Core.Constants.UmbracoIndexes.ExternalIndexName,
_licenceManager,
_logger,
_examineXConfig,
_runtimeState,
_umbracoIndexConfig.GetContentValueSetValidator(),
new DynamicCultureFieldDefinitionCollection(),
defaultAnalyzer,
// Pass in the custom field types
customValueTypeFactory);
return index;
}
}
// [ComposeAfter(typeof(ExamineXComposer))]
// [RuntimeLevel(MinLevel = RuntimeLevel.Run)]
// public class MyComposer2 : ComponentComposer<MyComponent2>, IUserComposer
// {
// }
//
// public class MyComponent2 : IComponent
// {
// private readonly IExamineManager _examineManager;
//
// public MyComponent2(IExamineManager examineManager)
// {
// _examineManager = examineManager;
// }
//
// public void Initialize()
// {
// if (!_examineManager.TryGetIndex(
// Umbraco.Core.Constants.UmbracoIndexes.ExternalIndexName,
// out var externalIndex))
// {
// throw new InvalidOperationException(
// $"No index found with name {Umbraco.Core.Constants.UmbracoIndexes.ExternalIndexName}");
// }
//
// if (!(externalIndex is AzureSearchIndex))
// {
// return;
// }
//
// var azureSearchIndex = (AzureSearchIndex) externalIndex;
//
// // Set the field definition for "productPrice" to be a Double
// azureSearchIndex.CreatingOrUpdatingIndex += AzureSearchIndexOnCreatingOrUpdatingIndex;
// }
//
// private void AzureSearchIndexOnCreatingOrUpdatingIndex(object sender, CreatingOrUpdatingIndexEventArgs e)
// {
// if (!e.AzureSearchIndexDefinition.Name.InvariantEquals(Umbraco.Core.Constants.UmbracoIndexes
// .ExternalIndexName))
// {
// return;
// }
//
// var fields = e.AzureSearchIndexDefinition.Fields;
//
// foreach (var field in fields)
// {
// var match = CultureIsoCodeFieldNameMatchExpression.IsoCodeExpression.Match(field.Name);
// if (match.Success && match.Groups.Count == 3)
// {
// field.Analyzer = CultureIsoCodeFieldNameMatchExpression.Analyzers[match.Groups[2].Value];
// }
// }
// }
//
// public void Terminate() { }
//}
}
using Examine;
using Umbraco.Examine;
namespace Presite.Core.Startup.Composers.Indexing
{
public class DynamicCultureFieldDefinitionCollection : FieldDefinitionCollection
{
public DynamicCultureFieldDefinitionCollection() : base()
{
}
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.IsoCodeExpression.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