Rebuild index after controller action

I have a big import task in Umbraco 13 that builds out property pages. However, this used to then refresh the custom index for properties on the old site, but doesn’t seem to now on the new version.

Can I trigger a re-index from a controller action, or do I have more work to do?

Hi Sandy,

Try injecting DistributedCache into your controller.

Then use:

_distributedCache.RefreshAllPublishedSnapshot();

I think this might work.

Cheers,

Marc

Hi Marc,

I’ve tried this but I now suspect I’m actually missing the notification for the index. I don’t think it’s seeing content being published as a manually created property doesn’t appear when published. Is it possible to have a custom index with 2 notification handlers? One looking for when content is published and registering it on the index?

I’m still pretty new to this and the Umbraco 13 docs don’t really go into detail.

The instructions I have given relate to updating Nucache which is the source for getting content on the frontend of your site.

When you say “Index” are you actually referring to the “Examine Index”?

Yes sorry, I wasn’t sure of the exact terminology.

It’s a custom examine index replicating what was on our site before but it’s not picking up content on it’s own, only with manual rebuilds

Ahh, okay.

In that case it looks like you can inject IIndexRebuilder to rebuild indexes. See here ion Umbraco’s own code:

I have not tried this before so cant guarantee it will work

Thanks for your help! While the code didn’t solve the problem it lead me gradually to the source of the problem.

I have no idea how our v8 site was actually updating the index as it wasn’t actually doing a “saveandpublish” action on create. I’ve tweaked it now and it fingers crossed seems to be working. I’ll do some more testing then share and mark this as solved :slight_smile:

If you have a completely custom index, you are responsible for updating the index records. I hope you don’t just rebuild the entire index all the time, because that would be torturing the CPU for no reason :stuck_out_tongue:

The data for the indexes are ValueSets that are built with a ValueSetBuilder. When some action is performed or when some notification is triggered you can update single record by creating a ValueSet and only update that value set to the indexby calling Index.IndexItems(valueSets);

1 Like

Yeah I know this but I wasn’t sure how it had been done by the previous developer. I’ve now got a proper publish notification in that indexes new items when they are published using the “SaveAndPublish” action. Seems efficient and hasn’t had any issues so far!

I had to build a handler for this: public class PropertyPublishIndexingNotificationHandler : INotificationHandler<ContentPublishedNotification>

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.