Creating an index task

As the name suggests an index task represents an action you wish to perform against an index.

All index tasks must implement the following interface:

    public interface IIndexTask {
        void Execute(IIndexService indexService);
        IndexOptions IndexOptions { get; set; }
    }

When you process the tasks in the index queue, we call the execute method of each task, passing in an index service that it can use to write or create an index. Each index task also has an IndexOptions property. Since an index queue can contain tasks for many different indexes, the IndexOptions contains the location of the index so we can resolve the correct index service for that task. See Processing the Index Queue for more details.

3 common operations you will want to perform against an index are Insert, Update and Delete tasks. For this reason we created two implementations of IIndexTask - EntityUpdateTask<TEntity> and EntityDeleteTask<TEntity>.

EntityUpdateTask<TEntity>

You can use this to index a new entity, or re-index an existing entity:

            // entity to index
            var product = new Product();
            
            // index location
            var indexLocation = new FileSystemIndexLocation(new DirectoryInfo(@"c:\productindex"));
            
            // index definition
            var definition = new ProductIndexDefinition();

            // create the task
            var task = new EntityUpdateTask<Product>(product, definition, indexLocation);

            // queue the task
            IndexQueue.Instance.Queue(task);

EntityDeleteTask

You can use this to delete an entity from an index. Pass the identifier of the document (should be the same as that returned by your definition's GetIndex method):

            int deletedProductId = 10;

            // index location
            var indexLocation = new FileSystemIndexLocation(new DirectoryInfo(@"c:\productindex"));

            // create the task
            var task = new EntityDeleteTask<Product>(indexLocation, "id", deletedProductId.ToString());

            // queue the task
            IndexQueue.Instance.Queue(task);

Last edited Apr 20, 2011 at 2:42 PM by retroviz, version 4

Comments

No comments yet.