Put Indexes Operation



Ways to deploy indexes - short summary

Static index:

There are a few ways to deploy a static-index from the Client API:

  • Call execute() on a specific index instance
  • Call IndexCreation.createIndexes() to deploy multiple indexes
  • Execute PutIndexesOperation maintenance operation on the Document Store - see below
  • Learn more in static indexes

Auto index:

  • An auto-index is created by the server when making a filtering query that doesn't specify which index to use
  • Learn more in auto indexes

Put indexes operation with IndexDefinition

Using PutIndexesOperation with IndexDefinition allows the following:

  • Choosing any name for the index
  • Setting low-level properties available in IndexDefinition

// Create an index definition
const indexDefinition = new IndexDefinition();

// Name is mandatory, can use any string
indexDefinition.name = "OrdersByTotal";

// Define the index map functions, string format
// A single string for a map-index, multiple strings for a multi-map-index
indexDefinition.maps = new Set([`
    // Define the collection that will be indexed:
    from order in docs.Orders

        // Define the index-entry:
        select new 
        {
            // Define the index-fields within each index-entry:
            Employee = order.Employee,
            Company = order.Company,
            Total = order.Lines.Sum(l => (l.Quantity * l.PricePerUnit) * (1 - l.Discount))
        }`
]);

 // indexDefinition.reduce = ...

// Can provide other index definitions available on the IndexDefinition class
// Override the default values, e.g.:
indexDefinition.deploymentMode = "Rolling";
indexDefinition.priority = "High";
indexDefinition.configuration = {
    "Indexing.IndexMissingFieldsAsNull": "true"
};
// See all available properties in syntax below

// Define the put indexes operation, pass the index definition
// Note: multiple index definitions can be passed, see syntax below
const putIndexesOp = new PutIndexesOperation(indexDefinition);

// Execute the operation by passing it to maintenance.send
await documentStore.maintenance.send(putIndexesOp);
// Create an index definition
const indexDefinition = new IndexDefinition();

// Name is mandatory, can use any string
indexDefinition.name = "OrdersByTotal";

// Define the index map functions, string format
// A single string for a map-index, multiple strings for a multi-map-index
indexDefinition.maps = new Set([`
    map('Orders', function(order) {
        return {
            Employee: order.Employee,
            Company: order.Company,
            Total: order.Lines.reduce(function(sum, l) {
                return sum + (l.Quantity * l.PricePerUnit) * (1 - l.Discount);
            }, 0)
        };
    });`
]);

// indexDefinition.reduce = ...

// Can provide other index definitions available on the IndexDefinition class
// Override the default values, e.g.:
indexDefinition.deploymentMode = "Rolling";
indexDefinition.priority = "High";
indexDefinition.configuration = {
    "Indexing.IndexMissingFieldsAsNull": "true"
};
// See all available properties in syntax below

// Define the put indexes operation, pass the index definition
// Note: multiple index definitions can be passed, see syntax below
const putIndexesOp = new PutIndexesOperation(indexDefinition);

// Execute the operation by passing it to maintenance.send
await documentStore.maintenance.send(putIndexesOp);

Syntax

const putIndexesOperation = new PutIndexesOperation(indexesToAdd);
Parameters Type Description
indexesToAdd ...IndexDefinition[] Definitions of indexes to deploy

IndexDefinition parameter Type Description
name string Name of the index, a unique identifier
maps Set<string> All the map functions for the index
reduce string The index reduce function
deploymentMode object Deployment mode
(Parallel, Rolling)
state object State of index
(Normal, Disabled, Idle, Error)
priority object Priority of index
(Low, Normal, High)
lockMode object Lock mode of index
(Unlock, LockedIgnore, LockedError)
fields Record<string, object> IndexFieldOptions per index field
additionalSources Record<string, string> Additional code files to be compiled with this index
additionalAssemblies object[] Additional assemblies that are referenced
configuration object Can override indexing configuration by setting this Record<string, string>
outputReduceToCollection string A collection name for saving the reduce results as documents
reduceOutputIndex number This number will be part of the reduce results documents IDs
patternForOutputReduceToCollectionReferences string Pattern for documents IDs which reference IDs of reduce results documents
patternReferencesCollectionName string A collection name for the reference documents created based on provided pattern
store.maintenance.send(putIndexesOp) return value Description
object[] operation result per index
Operation result per index Type Description
index string Name of the index that was added
raftCommandIndex long Index of raft command that was executed