---
title: "Put Indexes Operation"
url: "https://docs.ravendb.net/6.2/client-api/operations/maintenance/indexes/put-indexes"
version: "6.2"
language: "csharp"
---

# Put Indexes Operation

> Client language: C#. Also available for [Java](https://docs.ravendb.net/6.2/client-api/operations/maintenance/indexes/put-indexes.java.md), [Python](https://docs.ravendb.net/6.2/client-api/operations/maintenance/indexes/put-indexes.python.md), [PHP](https://docs.ravendb.net/6.2/client-api/operations/maintenance/indexes/put-indexes.php.md), [Node.js](https://docs.ravendb.net/6.2/client-api/operations/maintenance/indexes/put-indexes.nodejs.md).
>
> RavenDB publishes an official agent skill for coding assistants at https://docs.ravendb.net/skills/ravendb/SKILL.md (also installable with `npx skills add ravendb/skills`). The skill describes itself as: "Read this before writing any RavenDB query, client call or /databases/ request. Covers server setup, document modelling, the Node.js, .NET and Python clients, RQL, indexes, vector search and AI agents. Code written from recall tends to run and return the wrong rows, because RQL resembles SQL and the clients take field names as plain strings."

> **Note**
>
> - There are a few ways to create and deploy indexes in a database.
>
> - This page describes deploying a **static-index** using the `PutIndexesOperation` Operation.  
>   For a general description of Operations see [what are operations](https://docs.ravendb.net/6.2/client-api/operations/what-are-operations.md).
>
> - In this page:
>   - [Ways to deploy indexes - short summary](#ways-to-deploy-indexes---short-summary)
>   - [Put indexes operation with IndexDefinition](#put-indexes-operation-with-indexdefinition)
>   - [Put indexes operation with IndexDefinitionBuilder](#put-indexes-operation-with-indexdefinitionbuilder)
>   - [Syntax](#syntax)

## Ways to deploy indexes - short summary

> **Note**
>
> ##### 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](https://docs.ravendb.net/6.2/indexes/creating-and-deploying.md#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**.

> **Note**
>
> ##### Auto-indexes:
>
> - RavenDB uses a suitable existing auto-index, or creates one if needed,  
>   when a dynamic query includes **one or more of the following**:
>
>   - A `where` condition on fields other than the document ID,  
>     or an ID condition other than a simple ID lookup (`id() = ...`, `id() in (...)`, `startsWith(id(), ...)`).
>   - An `order by` clause to sort the results.
>   - A `group by` clause to group the results.
>   - A `suggest()` call in the `select` clause to request term suggestions.
>
>   Learn more in [Creating auto indexes](https://docs.ravendb.net/6.2/indexes/creating-and-deploying.md#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*.

### LINQ-index-Sync

```csharp
// 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);
```

### LINQ-index-Async

```csharp
// 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.SendAsync
await store.Maintenance.SendAsync(putIndexesOp);
```

### JavaScript-index

```csharp
// 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>
  {
      @"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)
            };
      });"
  },
  
  // 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.

### Sync

```csharp
// 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);
```

### Async

```csharp
// 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.SendAsync
await store.Maintenance.SendAsync(putIndexesOp);
```

## Syntax

```csharp
public PutIndexesOperation(params IndexDefinition[] indexesToAdd)
```

| Parameter | Type | Description |
| - | - | - |
| **indexesToAdd** | `params IndexDefinition[]` | Definitions of indexes to deploy |

| `IndexDefinition` parameter | Type | Description |
| - | - | - |
| Name | `string` | Name of the index, a unique identifier |
| Maps | `HashSet<string>` | All the map functions for the index |
| Reduce | `string` | The index reduce function |
| DeploymentMode | `IndexDeploymentMode?` | Deployment mode<br />(Parallel, Rolling) |
| State | `IndexState?` | State of index<br />(Normal, Disabled, Idle, Error) |
| Priority | `IndexPriority?` | Priority of index<br />(Low, Normal, High) |
| LockMode | `IndexLockMode?` | Lock mode of index<br />(Unlock, LockedIgnore, LockedError) |
| Fields | `Dictionary<string, IndexFieldOptions>` | *IndexFieldOptions* per index field |
| AdditionalSources | `Dictionary<string, string>` | Additional code files to be compiled with this index |
| AdditionalAssemblies | `HashSet<AdditionalAssembly>` | Additional assemblies that are referenced |
| Configuration | `IndexConfiguration` | Can override [indexing configuration](https://docs.ravendb.net/6.2/server/configuration/indexing-configuration.md) by setting this dictionary |
| OutputReduceToCollection | `string` | A collection name for saving the reduce results as documents |
| ReduceOutputIndex | `long?` | 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 |
| - | - |
| `PutIndexResult[]` | List of *PutIndexResult* per index |

| `PutIndexResult` parameter | Type | Description |
| - | - | - |
| Index | `string` | Name of the index that was added |
| RaftCommandIndex | `long` | Index of raft command that was executed |

## See also

- [What are Indexes](https://docs.ravendb.net/6.2/indexes/what-are-indexes.md)
- [Creating and Deploying Indexes](https://docs.ravendb.net/6.2/indexes/creating-and-deploying.md)
- [Index Administration](https://docs.ravendb.net/6.2/indexes/index-administration.md)
- [What are operations](https://docs.ravendb.net/6.2/client-api/operations/what-are-operations.md)
- [How to Get Indexes](https://docs.ravendb.net/6.2/client-api/operations/maintenance/indexes/get-indexes.md)
- [How to Delete Index](https://docs.ravendb.net/6.2/client-api/operations/maintenance/indexes/delete-index.md)
- [How to Reset Index](https://docs.ravendb.net/6.2/client-api/operations/maintenance/indexes/reset-index.md)
