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

# Put Indexes Operation

> Client language: PHP. 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), [Python](https://docs.ravendb.net/6.2/client-api/operations/maintenance/indexes/put-indexes.python.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.php.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

```php
// Create an index definition
$indexDefinition = new IndexDefinition();

// Name is mandatory, can use any string
$indexDefinition->setName("OrdersByTotal");

// Define the index Map functions, string format
// A single string for a map-index, multiple strings for a multi-map-index
$indexDefinition->setMaps([
  "// 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->setReduce(...);

// Can provide other index definitions available on the IndexDefinition class
// Override the default values, e.g.:
$indexDefinition->setDeploymentMode(IndexDeploymentMode::rolling());
$indexDefinition->setPriority(IndexPriority::high());

$configuration = new IndexConfiguration();
$configuration->offsetSet("Indexing.IndexMissingFieldsAsNull", "true");
$indexDefinition->setConfiguration($configuration);

// 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
$putIndexesOp = new PutIndexesOperation($indexDefinition);

// Execute the operation by passing it to Maintenance.Send
$store->maintenance()->send($putIndexesOp);
```

### JavaScript-index

```php
// Create an index definition
$indexDefinition = new IndexDefinition();

// Name is mandatory, can use any string
$indexDefinition->setName("OrdersByTotal");

// Define the index Map functions, string format
// A single string for a map-index, multiple strings for a multi-map-index
$indexDefinition->setMaps([
  "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->setReduce(...);

// Can provide other index definitions available on the IndexDefinition class
// Override the default values, e.g.:

$indexDefinition->setDeploymentMode(IndexDeploymentMode::rolling());
$indexDefinition->setPriority(IndexPriority::high());

$configuration = new IndexConfiguration();
$configuration->offsetSet("Indexing.IndexMissingFieldsAsNull", "true");
$indexDefinition->setConfiguration($configuration);
// 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
$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 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.

```php
// Create an index definition builder
$builder = new IndexDefinitionBuilder();
$builder->setMap(
  "// 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->setDeploymentMode(IndexDeploymentMode::rolling());
$builder->setPriority(IndexPriority::high());
// $builder->setReduce(...);

// Generate index definition from builder
// Pass the conventions, needed for building the Maps property
$indexDefinition = $builder->toIndexDefinition($store->getConventions());

// Optionally, set the index name, can use any string
// If not provided then default name from builder is used, e.g.: "IndexDefinitionBuildersOfOrders"
$indexDefinition->setName("OrdersByTotal");

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

// Execute the operation by passing it to maintenance.send
$store->maintenance()->send($putIndexesOp);
```

## Syntax

```php
PutIndexesOperation(IndexDefinition|IndexDefinitionArray|array ...$indexToAdd)
```

| Parameters | Type | Description |
| - | - | - |
| **$indexToAdd** | `IndexDefinition`<br />`IndexDefinitionArray`<br />`array` | Definitions of indexes to deploy |

| `IndexDefinition` parameter | Type | Description |
| - | - | - |
| **$name** | `?string` | Name of the index, a unique identifier |
| **$state** | `?IndexState` | State of index<br />(NORMAL, DISABLED, IDLE, ERROR) |
| **$priority** | `?IndexPriority` | Priority of index<br />(LOW, NORMAL, HIGH) |
| **$maps** | `?StringSet` | All the map functions for the index |
| **$reduce** | `?string` | The index reduce function |
| **$deploymentMode** | `?IndexDeploymentMode` | Deployment mode<br />(`parallel`, `rolling`) |
| **$lockMode** | `?IndexLockMode` | Lock mode of index<br />(`Unlock`, `LockedIgnore`, `LockedError`) |
| **$fields** | `?IndexFieldOptionsArray` | *IndexFieldOptions* per index field |
| **$additionalSources** | `?AdditionalSourcesArray` | Additional code files to be compiled with this index |
| **$additionalAssemblies** | `?AdditionalAssemblySet` | 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** | `?int` | 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 |
| **$sourceType** | `?IndexSourceType` | Index source type<br />(`None`, `Documents`, `TimeSeries`, `Counters`) |
| **$type** | `?IndexType` | Index type<br />(`None`, `AutoMap`, `AutoMapReduce`, `Map`, `MapReduce`, `Faulty`, `JavaScriptMap`, `JavaScriptMapReduce`) |

## See also

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