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

# Put Indexes Operation

> Client language: Python. Also available for [C#](https://docs.ravendb.net/6.2/client-api/operations/maintenance/indexes/put-indexes.md), [Java](https://docs.ravendb.net/6.2/client-api/operations/maintenance/indexes/put-indexes.java.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.python.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

#### 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.create_indexes()` to deploy multiple indexes
- Execute `PutIndexesOperation` maintenance operation on the Document Store - see below
- Learn more in [static indexes](https://docs.ravendb.net/6.2/indexes/creating-and-deploying.md#static-indexes)

#### Auto index:

- 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 [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 the following:

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

### Index

```python
# Create an index definition
index_definition = 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={
      """
        // 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.:
  deployment_mode=IndexDeploymentMode.ROLLING,
  priority=IndexPriority.HIGH,
  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
put_indexes_op = PutIndexesOperation(index_definition)

# Execute the operation by passing it to maintenance.send
store.maintenance.send(put_indexes_op)
```

### JavaScript-index

```python
# Create an index definition
index_definition = 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 multimap index
  maps={
      """
      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.:
  deployment_mode=IndexDeploymentMode.ROLLING,
  priority=IndexPriority.HIGH,
  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
put_indexes_op = PutIndexesOperation(index_definition)

# Execute the operation by passing it to Maintenance.Send
store.maintenance.send(put_indexes_op)
```

## Put indexes operation with IndexDefinitionBuilder

- Using `PutIndexesOperation` with an IndexDefinition created from an **IndexDefinitionBuilder**
  allows setting low-level properties available in *IndexDefinitionBuilder*.

- Note that only map or map-reduce indexes can be generated by the *IndexDefinitionBuilder*.  
  To generate multi-map indexes, use the above *IndexDefinition* option.

```python
# Create an index definition builder
builder = IndexDefinitionBuilder()
builder.map = """
        // 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))
          }               
      """
# Can provide other properties available on the IndexDefinitionBuilder class, e.g.:
builder.deployment_mode = IndexDeploymentMode.ROLLING
builder.priority = IndexPriority.HIGH
# builder.reduce = ..., etc.

# Generate index definition from builder
# Pass the conventions, needed for building the maps property
builder.to_index_definition(store.conventions)

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

# Define the put indexes operation, pass the index definition
# Note: multiple index definitions can be passed, see syntax below
put_indexes_op = PutIndexesOperation(index_definition)

# Execute the operation by passing it to Maintenance.Send
store.maintenance.send(put_indexes_op)
```

## Syntax

```python
class PutIndexesOperation(MaintenanceOperation):
  def __init__(self, *indexes_to_add: IndexDefinition): ...
```

| Parameters | Type | Description |
| - | - | - |
| **\*indexes\_to\_add** | `IndexDefinition` | Definitions of indexes to deploy |

| `IndexDefinition` parameter | Type | Description |
| - | - | - |
| **name** | `str` | Name of the index, a unique identifier |
| **maps** | `Set[str]` | All the map functions for the index |
| **reduce** | `str` | The index reduce function |
| **deployment\_mode** | `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) |
| **lock\_mode** | `IndexLockMode` | Lock mode of index<br />(UNLOCK, LOCKED\_IGNORE, LOCKED\_ERROR) |
| **fields** | `Dict[str, IndexFieldOptions]` | *IndexFieldOptions* per index field |
| **additional\_sources** | `Dict[str, str]` | Additional code files to be compiled with this index |
| **additional\_assemblies** | `Set[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 |
| **output\_reduce\_to\_collection** | `str` | A collection name for saving the reduce results as documents |
| **reduce\_output\_index** | `int` | This number will be part of the reduce results documents IDs |
| **pattern\_for\_output\_reduce\_to\_collection\_references** | `str` | Pattern for documents IDs which reference IDs of reduce results documents |
| **pattern\_references\_collection\_name** | `str` | A collection name for the reference documents created based on provided pattern |

| `store.maintenance.send(put_indexes_op)` return value | Description |
| - | - |
| `List[PutIndexResult]` | List of *PutIndexResult* per index |

| `PutIndexResult` parameter | Type | Description |
| - | - | - |
| **index** | `str` | Name of the index that was added |
| **raft\_command\_index** | `int` | Index of raft command that was executed |

## See also

- [What are Indexes](https://docs.ravendb.net/6.2/indexes/what-are-indexes.python.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.python.md)
- [How to Get Indexes](https://docs.ravendb.net/6.2/client-api/operations/maintenance/indexes/get-indexes.python.md)
- [How to Delete Index](https://docs.ravendb.net/6.2/client-api/operations/maintenance/indexes/delete-index.python.md)
- [How to Reset Index](https://docs.ravendb.net/6.2/client-api/operations/maintenance/indexes/reset-index.python.md)
