Using Umbraco.ContentQuery.Search to find related documents
Hi!
Im using the simple search Umbraco.ContentQuery.Search(q, null, "ExternalIndex") to find products on our page.
On the products we have connected documents (Multiple media picker) but if we make a search on a document name the product that has that document connected doesn't show up.
Is that possible to do with this solution or how do I change so the externalIndex index that?
If I were you I would add new field to the index which contains the names of the media items. Right now the multiple media picker is saved as JSON in the index. So that's why they aren't showing up! I would do something like this:
public class IndexComposer : IUserComposer
{
public void Compose(Composition composition)
{
composition.Components().Append<IndexerComponent>();
}
}
public class IndexerComponent : IComponent
{
private readonly IContentService _contentService;
private readonly IExamineManager _examineManager;
public IndexerComponent(IContentService contentService, IExamineManager examineManager)
{
_contentService = contentService;
_examineManager = examineManager;
}
public void Initialize()
{
if (!_examineManager.TryGetIndex(UmbracoIndexes.ExternalIndexName, out IIndex index))
return;
((BaseIndexProvider)index).TransformingIndexValues += IndexerComponent_TransformingIndexValues;
}
public void Terminate() { }
private void IndexerComponent_TransformingIndexValues(object sender, IndexingItemEventArgs e)
{
if (int.TryParse(e.ValueSet.Id, out var nodeId))
{
foreach (var fieldValues in e.ValueSet.Values.ToList())
{
if (fieldValues.Key == "relatedDoucments")
{
var node = _contentService.GetById(nodeId);
var documents = node.GetValue<IEnumerable<IPublishedContent>>("relatedDoucments").Select(x => x.Name);
e.ValueSet.TryAdd("searchableDocumentNames", string.Join(" ", documents));
}
}
}
}
}
I wrote this a bit hasty! It should probably have some more if checks. But let me know if it works!
Using Umbraco.ContentQuery.Search to find related documents
Hi! Im using the simple search Umbraco.ContentQuery.Search(q, null, "ExternalIndex") to find products on our page.
On the products we have connected documents (Multiple media picker) but if we make a search on a document name the product that has that document connected doesn't show up.
Is that possible to do with this solution or how do I change so the externalIndex index that?
Hi Fredrik!
If I were you I would add new field to the index which contains the names of the media items. Right now the multiple media picker is saved as JSON in the index. So that's why they aren't showing up! I would do something like this:
I wrote this a bit hasty! It should probably have some more if checks. But let me know if it works!
//Johannes
Thanks! That was exactly what I needed :)
is working on a reply...