Skip to main content

Put Indexes Operation

Ways to deploy indexes - short summary

Static-indexes:

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

  • The following methods are explained in section Deploy a static-index:

    • Call Execute() on a specific index instance.
    • Call ExecuteIndex() or ExecuteIndexes() on your DocumentStore object.
    • Call IndexCreation.CreateIndexes().
  • Alternatively, you can execute the PutIndexesOperation maintenance operation on the DocumentStore, as explained below.

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

Put indexes operation with IndexDefinition

Using PutIndexesOperation with IndexDefinition allows you to:

  • Choose any name for the index.
    This string-based name is specified when querying the index.
  • Set low-level properties available in IndexDefinition.
// Create an index definition
var indexDefinition = new IndexDefinition
{
// Name is mandatory, can use any string
Name = "OrdersByTotal",

// Define the index Map functions, string format
// A single string for a map-index, multiple strings for a multi-map-index
Maps = new HashSet<string>
{
@"
// 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))
}"
},

// Reduce = ...,

// Can provide other index definitions available on the IndexDefinition class
// Override the default values, e.g.:
DeploymentMode = IndexDeploymentMode.Rolling,
Priority = IndexPriority.High,
Configuration = new IndexConfiguration
{
{ "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
IMaintenanceOperation<PutIndexResult[]> putIndexesOp = new PutIndexesOperation(indexDefinition);

// Execute the operation by passing it to Maintenance.Send
store.Maintenance.Send(putIndexesOp);

Put indexes operation with IndexDefinitionBuilder

Using PutIndexesOperation with an IndexDefinition created from an IndexDefinitionBuilder allows:

  • Creating an index definition using a strongly typed LINQ syntax.
  • Setting low-level properties available in IndexDefinitionBuilder.
  • Note:
    Only map or map-reduce indexes can be generated by the IndexDefinitionBuilder.
    To generate multi-map indexes use the above IndexDefinition option.
// Create an index definition builder
var builder = new IndexDefinitionBuilder<Order>
{
// Define the map function, strongly typed LINQ format
Map =
// Define the collection that will be indexed:
orders => from order in 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))
},

// Can provide other properties available on the IndexDefinitionBuilder class, e.g.:
DeploymentMode = IndexDeploymentMode.Rolling,
Priority = IndexPriority.High,
// Reduce = ..., etc.
};

// Generate index definition from builder
// Pass the conventions, needed for building the Maps property
var indexDefinition = builder.ToIndexDefinition(store.Conventions);

// Optionally, set the index name, can use any string
// If not provided then default name from builder is used, e.g.: "IndexDefinitionBuildersOfOrders"
indexDefinition.Name = "OrdersByTotal";

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

// Execute the operation by passing it to Maintenance.Send
store.Maintenance.Send(putIndexesOp);

Syntax

public PutIndexesOperation(params IndexDefinition[] indexesToAdd)
ParameterTypeDescription
indexesToAddparams IndexDefinition[]Definitions of indexes to deploy
IndexDefinition parameterTypeDescription
NamestringName of the index, a unique identifier
MapsHashSet<string>All the map functions for the index
ReducestringThe index reduce function
DeploymentModeIndexDeploymentMode?Deployment mode
(Parallel, Rolling)
StateIndexState?State of index
(Normal, Disabled, Idle, Error)
PriorityIndexPriority?Priority of index
(Low, Normal, High)
LockModeIndexLockMode?Lock mode of index
(Unlock, LockedIgnore, LockedError)
FieldsDictionary<string, IndexFieldOptions>IndexFieldOptions per index field
AdditionalSourcesDictionary<string, string>Additional code files to be compiled with this index
AdditionalAssembliesHashSet<AdditionalAssembly>Additional assemblies that are referenced
ConfigurationIndexConfigurationCan override indexing configuration by setting this dictionary
OutputReduceToCollectionstringA collection name for saving the reduce results as documents
ReduceOutputIndexlong?This number will be part of the reduce results documents IDs
PatternForOutputReduceToCollectionReferencesstringPattern for documents IDs which reference IDs of reduce results documents
PatternReferencesCollectionNamestringA collection name for the reference documents created based on provided pattern
store.Maintenance.Send(putIndexesOp) return valueDescription
PutIndexResult[]List of PutIndexResult per index
PutIndexResult parameterTypeDescription
IndexstringName of the index that was added
RaftCommandIndexlongIndex of raft command that was executed