Commands: Indexes: Put

There are few methods that allow you to insert index into a database:
- PutIndex
- PutIndex - using IndexDefinitionBuilder
- PutIndexes

PutIndex

PutIndex is used to insert an index into a database.

Syntax

string PutIndex(string name, IndexDefinition indexDef);

string PutIndex(string name, IndexDefinition indexDef, bool overwrite);
Parameters
name string Name of an index
indexDef IndexDefinition Definition of an index
overwrite bool Indicates if an index could be overwritten (if false then exception will be thrown if index exists)
Return Value
string Index name

Example

string indexName = store
	.DatabaseCommands
	.PutIndex(
		"Orders/Totals",
		new IndexDefinition
			{
				Map = @"from order in docs.Orders
						select new 
						{
							order.Employee,
							order.Company,
							Total = order.Lines.Sum(l => (l.Quantity * l.PricePerUnit) * (1 - l.Discount))
						}"
			});

PutIndex - using IndexDefinitionBuilder

PutIndex is used to insert an index into a database.

To help users create their indexes, IndexDefinitionBuilder was created that enables users to create indexes using strongly-typed syntax.

Syntax

string PutIndex<TDocument, TReduceResult>(string name, IndexDefinitionBuilder<TDocument, TReduceResult> indexDef);

string PutIndex<TDocument, TReduceResult>(string name, IndexDefinitionBuilder<TDocument, TReduceResult> indexDef, bool overwrite);
Parameters
name string Name of an index
indexDef IndexDefinitionBuilder<TDocument, TReduceResult> strongly-typed index definition
overwrite bool Indicates if an index could be overwritten (if false then exception will be thrown if index exists)
Return Value
string Index name.

Example

string indexName = store
	.DatabaseCommands
	.PutIndex(
		"Orders/Totals",
		new IndexDefinitionBuilder<Order>
			{
				Map = orders => from order in orders
								select new
								{
									order.Employee,
									order.Company,
									Total = order.Lines.Sum(l => (l.Quantity * l.PricePerUnit) * (1 - l.Discount))
								}
			});

Remarks

If overwrite is set to true and IndexDefinition has not changed, no action will be taken on server-side and no indexing data will be lost.

Safe By Default

By default, PutIndex method does not allow indexes to be overwritten because this causes all previous indexing data to be lost, which is not desired in many cases.

PutIndexes

PutIndexes creates multiple indexes with specified names, using given index definitions and priorities.

Syntax

string[] PutIndexes(IndexToAdd[] indexesToAdd);
Parameters
indexesToAdd IndexToAdd[] Indexes to create
Return Value
string[] Names of created indexes.

Example

store.DatabaseCommands.PutIndexes(new IndexToAdd[]
{
	new IndexToAdd
	{
		Name = "Orders/Totals",
		Priority = IndexingPriority.Normal,
		Definition = new IndexDefinition
		{
			Map = @"from order in docs.Orders
						select new 
						{
							order.Employee,
							order.Company,
							Total = order.Lines.Sum(l => (l.Quantity * l.PricePerUnit) * (1 - l.Discount))
						}"
		}
	},
	new IndexToAdd
	{
		Name = "Orders/ByCompany",
		Priority = IndexingPriority.Normal,
		Definition = new IndexDefinition
		{
			Map = @"from order in docs.Orders
						select new 
						{ 
							order.Company, 
							Count = 1, 
							Total = order.Lines.Sum(l = >(l.Quantity * l.PricePerUnit) *  ( 1 - l.Discount))
						}",

			Reduce = @"from result in results
						group result by result.Company into g
						select new
						{
							Company = g.Key,
							Count = g.Sum(x=>x.Count),
							Total = g.Sum(x=>x.Total)
						}"
		}
	}
});