Is there a simple way to add reindex job in a scheduled task?


Using Elastic Search, i want to reindex a custom job every 2 hours. Can’t figure a way to add it in a scheduled task.

Litium version: 7.7.1

You can inject the IEnumerable<IIndexMappingConfiguration> into your scheduled task and from that find the configuration for the index that you want to rebuild. This interface define one method for rebuild the index that can be used and is the same method that are invoked from the view in back office.

Why do you need to rebuild the index based on a scheduler?

The index contains data fetched from api that needs to be updated every 2 hours.

Also did you mean to inject IIndexMappingConfiguration and the use ‘QueueIndexRebuildAsync’?

You need to inject the IEnumerable<IIndexMappingConfiguration> becuase you have multiple registrations and you need to find the correct item in this enumerable.

Quick follow up question.
Every time we deploy the index gets “destroy”. Is this common? Or should we add reindex in a startup task?

We are using Elastic Search.

Do not understand what “destroy” means in this context. Indexes are stored outside Litium so they should be in exactly the same shape as before the new deployment.

Force an index rebuild on every restart is an bad practice and should not be done.

wrong word choice, but corrupted index. Example, we have 170 documents (which is the correct value) before restarting the application, then after the restart there are 184 documents.

ok, so it adds some pages that are not with the same templateSystemId. This is my index configuration, it shouldn’t fetch other pages with another template?

 public class StoreIndexConfiguration : IndexConfigurationBase<StoreDocument>
        private readonly DataService _dataService;
        private readonly IStringLocalizer _localizer;
        private readonly FieldTemplateService _fieldTemplateService;
        public StoreIndexConfiguration(IndexConfigurationDependencies dependencies, DataService dataService, IStringLocalizer<IndexConfigurationActionResult> localizer, FieldTemplateService fieldTemplateService)
            : base(dependencies)
            _dataService = dataService;
            _localizer = localizer;
            _fieldTemplateService = fieldTemplateService;

        protected override Task<IndexConfigurationActionResult> QueueIndexRebuildAsync(IndexQueueService indexQueueService)

            var storePageTemplate = _fieldTemplateService.Get<PageFieldTemplate>(CoptikkPageTemplateNameConstants.StorePage);

            using (var query = _dataService.CreateQuery<Websites.Page>())
                query.Filter(f => f.TemplateSystemId("eq", storePageTemplate.SystemId).Status(ContentStatus.Published));
                foreach (var systemId in query.ToSystemIdList())
                    indexQueueService.Enqueue(new IndexQueueItem<StoreDocument>(systemId));

            return Task.FromResult(new IndexConfigurationActionResult
                //Need to add "" to Administration.resx
                Message = _localizer.GetString("")


Restart of the application should not have an impact on what you have in the search index. Will it be that you listen on some events and trigger index rebuild of them? If that is the case, do you have anything that may trigger this event during startup?

ok, yes, it was an event that was faulty. Thanks for the quick response and help.

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