World is now on Opti ID! Learn more

Reindex Product on Association change

Vote:
 

I have to reindex (Find document) for particular Product on Association change (Product-Product association), but I'm thinking about how to do it best:

In initialization module I added code like this:

var catalogEvents = ServiceLocator.Current.GetInstance<ICatalogEvents>();
catalogEvents.AssociationUpdated += AssociationUpdate;

private void AssociationUpdate(object? sender, AssociationEventArgs e)
{

var productEntryId = e.Changes.Select(x => x.ChildEntryId).Distinct().ToList();
//code there, foreach product, check if it's valid case and reindex}

Will that do the job, or is there better approach possible?


And followup question, how to trigger that reindex? Is that the proper way?

var actualProduct = //GET product based on entryId//

var client = ServiceLocator.Current.GetInstance<IClient>();
client.Index(actualProduct);
#338027
Edited, May 07, 2025 9:00
Vote:
 

If you install Find.Commerce package, this should be handled automatically 

#338028
May 07, 2025 11:07
Vote:
 

Does it work for both association entries-products (Source/Target)?

The case is: that Parent-Product has association to Product-Accessories
For Product-Accesory we added a convention to store the sort orders obtained from the "Parent-Product TO  Product-Accessory" association.

We query for Product-Accessories (explicit `.Filter(p => p.ContentLink.ID.In(contentIds))`)
And we need the result to be ordered based on the SortOrder on the Parent-Product TO Product-Accessory association (mostly paging purposes).

Which simply means, "Parent-Product TO  Product-Accessory" association update under the Parent-Product, has to trigger reindex also of the Target entry (Product-Accessoriy)

#338032
Edited, May 07, 2025 11:33
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.