_tommadden
05/21/2024, 9:26 AMJemayn
05/21/2024, 10:45 AMAmbert
05/21/2024, 12:07 PMProNotion
05/22/2024, 4:08 PMContentValueSetBuilder
and override GetValueSets
to insert my custom field but it seems to have failed and it never seems to get called.
I registered it in my startup.cs class as follows:
public void ConfigureServices(IServiceCollection services)
{
services.AddUnique<IContentValueSetBuilder, MyContentValueSetBuilder>();
...
}
I then have an IComposer
as follows:
public class ExamineComposer : IComposer
{
public void Compose(IUmbracoBuilder builder)
{
builder.Services.AddSingleton<MyContentValueSetBuilder>(factory =>
new MyContentValueSetBuilder(
factory.GetRequiredService<PropertyEditorCollection>(),
factory.GetRequiredService<UrlSegmentProviderCollection>(),
factory.GetRequiredService<IUserService>(),
factory.GetRequiredService<IShortStringHelper>(),
factory.GetRequiredService<IScopeProvider>(),
true,
factory.GetRequiredService<ILocalizationService>(),
factory.GetRequiredService<IUmbracoContextFactory>()));
}
}
I had all sorts of trouble with the example in the documentation and the dependency injection so had to dig into the v12 source code to come up with the above.
If anyone has had success with this I'd be interested in any pointers.
I don't think the example from @Jemayn is suited to my situation when I have 50,000+ nodes to index and it seems like a legacy way of doing it (but could be wrong!).Lamont
05/22/2024, 5:35 PMpublic class ExamineComponents : IComponent
{
// ... inits'n such
private void IndexProviderTransformNewsValues(object? sender, IndexingItemEventArgs e) {
// validate
// Transform dates to use author set fields
var aliases = new[] { "article", "event" };
const string customSortField = "sortedDate"; // match custom index field name
try
{
var contentTypeIds = _contentTypeService.GetAllContentTypeIds(aliases).ToList().ConvertAll(i => i.ToString());
var values = e.ValueSet.Values.ToDictionary(
x => x.Key, x => x.Value.ToList());
values.TryGetValue("nodeType", out var nodeType);
values.TryGetValue("createDate", out var fallbackDate);
// document fields; 'good to know': dates are saved in DateTime.Ticks
values.TryGetValue("publishedDate", out var publishedDate);
values.TryGetValue("updatedDate", out var updatedDate);
var nodeId = nodeType?.FirstOrDefault()?.ToString();
// validation here
if (publishedDate == null)
{ // fallbacks, see below for sample }
else
{
values[customSortField] = updatedDate?.FirstOrDefault()?.ToString() != null
? updatedDate
: publishedDate;
}
e.SetValues(values.ToDictionary(x => x.Key, x => (IEnumerable<object>)x.Value));
Lamont
05/22/2024, 5:39 PM_tommadden
05/23/2024, 9:52 AMProNotion
05/23/2024, 9:58 AMJemayn
05/23/2024, 10:03 AMProNotion
05/23/2024, 10:11 AMProNotion
05/23/2024, 10:33 AMINotificationHandler
in conjunction with my ContentValueSetBuilder
ProNotion
05/23/2024, 1:23 PMUmbracoApplicationStartedNotification
and register it in your composer. Inside your handler you will add an event handler for the Examine Index TransformingIndexValues
(thanks @Jemayn & @Lamont)
My original approach of using a ContentValueSetBuilder
was fine but only works (for me) when publishing nodes but I need this to happen whenever the index is updated.A hub and casual space for you to interact with fellow community members and learn more about Umbraco!
Powered by