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

**URL:** <https://forum.litium.com/t/is-there-a-simple-way-to-add-reindex-job-in-a-scheduled-task/2421>\
**Category:** Questions\
**Tags:** accelerator, litium-7\
**Created:** [February 11, 2022, 10:34am UTC](https://forum.litium.com/t/is-there-a-simple-way-to-add-reindex-job-in-a-scheduled-task/2421 "2022-02-11T10:34:32Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gonzalo](https://avatars.discourse-cdn.com/v4/letter/g/4bbf92/32.png) [@Gonzalo](https://forum.litium.com/u/Gonzalo)\
**Post date:** [February 11, 2022, 10:34am UTC](https://forum.litium.com/t/is-there-a-simple-way-to-add-reindex-job-in-a-scheduled-task/2421/1 "2022-02-11T10:34:32Z")

</div>

Hi

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

---

<div class="post-metadata">

**Author:** ![patric.forsgard](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/patric.forsgard/32/10_2.png) [@patric.forsgard](https://forum.litium.com/u/patric.forsgard)\
**Post date:** [February 11, 2022, 10:56am UTC](https://forum.litium.com/t/is-there-a-simple-way-to-add-reindex-job-in-a-scheduled-task/2421/2 "2022-02-11T10:56:46Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Gonzalo](https://avatars.discourse-cdn.com/v4/letter/g/4bbf92/32.png) [@Gonzalo](https://forum.litium.com/u/Gonzalo)\
**Post date:** [February 11, 2022, 12:45pm UTC](https://forum.litium.com/t/is-there-a-simple-way-to-add-reindex-job-in-a-scheduled-task/2421/3 "2022-02-11T12:45:01Z")

</div>

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’?

---

<div class="post-metadata">

**Author:** ![patric.forsgard](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/patric.forsgard/32/10_2.png) [@patric.forsgard](https://forum.litium.com/u/patric.forsgard)\
**Post date:** [February 11, 2022, 12:56pm UTC](https://forum.litium.com/t/is-there-a-simple-way-to-add-reindex-job-in-a-scheduled-task/2421/4 "2022-02-11T12:56:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Gonzalo](https://avatars.discourse-cdn.com/v4/letter/g/4bbf92/32.png) [@Gonzalo](https://forum.litium.com/u/Gonzalo)\
**Post date:** [February 16, 2022, 8:31am UTC](https://forum.litium.com/t/is-there-a-simple-way-to-add-reindex-job-in-a-scheduled-task/2421/5 "2022-02-16T08:31:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![patric.forsgard](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/patric.forsgard/32/10_2.png) [@patric.forsgard](https://forum.litium.com/u/patric.forsgard)\
**Post date:** [February 16, 2022, 11:20am UTC](https://forum.litium.com/t/is-there-a-simple-way-to-add-reindex-job-in-a-scheduled-task/2421/6 "2022-02-16T11:20:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Gonzalo](https://avatars.discourse-cdn.com/v4/letter/g/4bbf92/32.png) [@Gonzalo](https://forum.litium.com/u/Gonzalo)\
**Post date:** [February 16, 2022, 1:39pm UTC](https://forum.litium.com/t/is-there-a-simple-way-to-add-reindex-job-in-a-scheduled-task/2421/7 "2022-02-16T13:39:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Gonzalo](https://avatars.discourse-cdn.com/v4/letter/g/4bbf92/32.png) [@Gonzalo](https://forum.litium.com/u/Gonzalo)\
**Post date:** [February 16, 2022, 2:00pm UTC](https://forum.litium.com/t/is-there-a-simple-way-to-add-reindex-job-in-a-scheduled-task/2421/8 "2022-02-16T14:00:16Z")

</div>

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?

```auto
 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 "index.store.queued" to Administration.resx
                Message = _localizer.GetString("index.store.queued")
            });

        }
    }

```

---

<div class="post-metadata">

**Author:** ![patric.forsgard](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/patric.forsgard/32/10_2.png) [@patric.forsgard](https://forum.litium.com/u/patric.forsgard)\
**Post date:** [February 16, 2022, 3:12pm UTC](https://forum.litium.com/t/is-there-a-simple-way-to-add-reindex-job-in-a-scheduled-task/2421/9 "2022-02-16T15:12:05Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Gonzalo](https://avatars.discourse-cdn.com/v4/letter/g/4bbf92/32.png) [@Gonzalo](https://forum.litium.com/u/Gonzalo)\
**Post date:** [February 16, 2022, 3:24pm UTC](https://forum.litium.com/t/is-there-a-simple-way-to-add-reindex-job-in-a-scheduled-task/2421/10 "2022-02-16T15:24:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/litium/original/1X/12eddac3b8b661de1db86af10ac8151b571e6fb9.png) [@system](https://forum.litium.com/u/system)\
**Post date:** [March 16, 2022, 3:25pm UTC](https://forum.litium.com/t/is-there-a-simple-way-to-add-reindex-job-in-a-scheduled-task/2421/11 "2022-03-16T15:25:13Z")

</div>

Closed on March 16, 2022, 3:25pm UTC

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