Developer forum

Forum » Development » Start Lucene search index update from code

Start Lucene search index update from code

Martin Nielsen
Reply
 Is it possible to start an update or a partial update of the Lucene index from custom code?

I'm importing a lot of filters and products via some activites, and i'd like to update the index once all these imports are done.

Regards
 Martin

Replies

 
Yury Zhukovskiy
Reply

Hi, try please this code:

 

Imports Dynamicweb.Extensibility.Searching

.

.

.

        ' *** Dynamicweb Searching ***

        ' Running all pending index updates

        If Not ScheduledUpdate.StartupTaskIsExecuted Then

            If Not HttpRuntime.UsingIntegratedPipeline Then

                For Each u As ScheduledUpdate In ScheduledUpdate.GetPendingUpdates(ScheduledUpdateType.StartupTask)

                    ' Update will be performed asynchronously

                    Dynamicweb.Searching.Management.IndexUpdateService.Current.CreateTask(u.Path, u.IsFullUpdate)

 

                    ' Updating "Last executed" timestamp

                    u.LastExecuted = DateTime.Now

                    u.Save()

                Next

            End If

 

            ScheduledUpdate.StartupTaskIsExecuted = True

        End If
 

Kind Regards

Zhukovskiy Yury

 
Pavel Volgarev
Reply
This post has been marked as an answer
 Hi Martin and Yury,

Yury, this approach won't work because the code relies on a "Scheduled updates" settings (Management Center -> eCommerce -> Advanced configuration -> Searching -> Right click on "Products" -> "Scheduled updates") so if you didn't configure that then the update won't execute.

There is a much simpler (and recommended) approach:

1. Non-blocking (asynchronous) update:
var isFullUpdate = false;
Dynamicweb.Searching.Management.IndexUpdateService.Current.CreateTask("Products", isFullUpdate);
2. Blocking (synchronous) update:
var isFullUpdate = false;
Dynamicweb.Searching.IndexManager.Current.UpdateIndex("Products", isFullUpdate);
Please note that you need to add a reference to "Dynamicweb.Searching" in your project before you can use this APIs.

-- Pavel
Votes for this answer: 0
 
Mikkel Høst
Reply
 Hi Everybody.

FYI - If your DW solution is Integrated with Navision and you want to update the index after import. Create a PipeLine with Pavel's solution 2. (Synchronous) and place it as the last pipeline in the import. Otherwise the index will, in some cases, fail with an object reference not set to an instance of an object. Don't know why, but it works with 
Dynamicweb.Searching.IndexManager.Current.UpdateIndex("Products", isFullUpdate);
and not with

Dynamicweb.Searching.Management.IndexUpdateService.Current.CreateTask("Products", isFullUpdate);


 
Morten Snedker
Reply
 I can just add that 
Dynamicweb.Searching.IndexManager.Current.UpdateIndex("Products", isFullUpdate);
is in use in our Product Import Activity from next release (so we've done the same changes as you, based on your case). ;-)

Regards /Snedker

 

You must be logged in to post in the forum