I have IConfigureNamedOptions set up and have debugged the code and it is getting hit.
public class ConfigureExternalIndexOptions : IConfigureNamedOptions<LuceneDirectoryIndexOptions>
{
public void Configure(string? name, LuceneDirectoryIndexOptions options)
{
if (name is global::Umbraco.Cms.Core.Constants.UmbracoIndexes.ExternalIndexName)
{
options.FieldDefinitions.AddOrUpdate(new FieldDefinition("blogDate", FieldDefinitionTypes.DateTime));
options.FieldDefinitions.AddOrUpdate(new FieldDefinition(Constants.ExamineFields.SearchableBlogCategories, FieldDefinitionTypes.FullText));
}
}
// Part of the interface, but does not need to be implemented for this.
public void Configure(LuceneDirectoryIndexOptions options)
{
throw new System.NotImplementedException();
}
}
I then have a IComponent which gets the values and adds them to the index to the new field.
public class IndexerComponent : IComponent
{
private readonly IExamineManager _examineManager;
private readonly IUmbracoContextFactory _umbracoContextFactory;
public IndexerComponent(IExamineManager examineManager,
IUmbracoContextFactory umbracoContextFactory)
{
_examineManager = examineManager ?? throw new ArgumentNullException(nameof(examineManager));
_umbracoContextFactory =
umbracoContextFactory ?? throw new ArgumentNullException(nameof(umbracoContextFactory));
}
public void Initialize()
{
if (_examineManager.TryGetIndex(global::Umbraco.Cms.Core.Constants.UmbracoIndexes.ExternalIndexName, out var externalIndex))
{
((BaseIndexProvider)externalIndex).TransformingIndexValues += IndexerComponent_TransformingIndexValues;
}
}
private void IndexerComponent_TransformingIndexValues(object? sender, IndexingItemEventArgs e)
{
if (int.TryParse(e.ValueSet.Id, out var nodeId))
{
switch (e.ValueSet.ItemType)
{
case "blogPage":
using (var umbracoContext = _umbracoContextFactory.EnsureUmbracoContext())
{
var contentNode = umbracoContext.UmbracoContext.Content?.GetById(nodeId);
if (contentNode != null)
{
var categories = contentNode.Value<IEnumerable<IPublishedContent>>("blogCategories");
var publishedContents = categories as IPublishedContent[] ?? categories?.ToArray();
var updatedValues = e.ValueSet.Values.ToDictionary(x => x.Key, x => x.Value.ToList());
if (publishedContents != null && publishedContents.Any())
{
if (updatedValues.ContainsKey(Constants.ExamineFields.SearchableBlogCategories))
{
updatedValues[Constants.ExamineFields.SearchableBlogCategories] = new List<object> { string.Join(" ", publishedContents.Select(x => x.Key.ToString("N"))) };
}
else
{
updatedValues.Add(Constants.ExamineFields.SearchableBlogCategories,new List<object> { string.Join(" ", publishedContents.Select(x => x.Key.ToString("N"))) });
}
}
e.SetValues(updatedValues.ToDictionary(x => x.Key, x => (IEnumerable<object>)x.Value));
}
}
break;
}
}
}
public void Terminate()
{
}
}
This is all being hit, and the value added to the updateValues dictionary when I hit save. But, when I hit save again, those values are not in the index?
Unable To Get An Examine Custom Index Item To Work In V11
I am unable to get the following code to work. I was trying to do exactly the same a Paul has done in his blog, only for V11.
https://codeshare.co.uk/blog/how-to-search-by-picked-multi-node-tree-picker-values-in-umbraco-v8/
I have
IConfigureNamedOptions
set up and have debugged the code and it is getting hit.I then have a
IComponent
which gets the values and adds them to the index to the new field.This is all being hit, and the value added to the updateValues dictionary when I hit save. But, when I hit save again, those values are not in the index?
Any idea where I am going wrong with this?
Seems this was working! A local development environment issue I think
I'm coming across something similar... Any idea what the local environment issue was?
Did you find a solution ? :) Having same issue
It actullay worked in 11 but not in 12.3.3 now
For anybody finding this, you shouldn't perform service lookups within the TransformingIndexValues event.
ie. umbracoContext.UmbracoContext.Content?.GetById(nodeId);
Umbraco Pitfalls
is working on a reply...