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.create_indexes() 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
$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);
// 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.

// 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

PutIndexesOperation(IndexDefinition|IndexDefinitionArray|array ...$indexToAdd)
Parameters Type Description
$indexToAdd IndexDefinition
IndexDefinitionArray
array
Definitions of indexes to deploy

IndexDefinition parameter Type Description
$name ?string Name of the index, a unique identifier
$state ?IndexState State of index
(NORMAL, DISABLED, IDLE, ERROR)
$priority ?IndexPriority Priority of index
(LOW, NORMAL, HIGH)
$maps ?StringSet All the map functions for the index
$reduce ?string The index reduce function
$deploymentMode ?IndexDeploymentMode Deployment mode
(parallel, rolling)
$lockMode ?IndexLockMode Lock mode of index
(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 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
(None, Documents, TimeSeries, Counters)
$type ?IndexType Index type
(None, AutoMap, AutoMapReduce, Map, MapReduce, Faulty, JavaScriptMap, JavaScriptMapReduce)