Indexes: JavaScript Indexes

This feature was created for users who want to create an index and prefer JavaScript over C#.
JavaScript indexes can be defined by a user with lower permissions than the C# indexes (admin not required).
All other capabilities and features are the same as C# indexes.

Creating JavaScript index

If we want to create JavaScript index we need to create an instance of our class that inherits from AbstractJavaScriptIndexCreationTask.
AbstractJavaScriptIndexCreationTask inherits from AbstractIndexCreationTask (Read more about AbstractIndexCreationTask here.)

public class Employees_ByFirstAndLastName : AbstractJavaScriptIndexCreationTask
{
    // ...
}

Map index

Map indexes, sometimes referred to as simple indexes, contain one (or more) mapping functions that indicate which fields from the documents should be indexed. They indicate which documents can be searched by which fields.

map(<collection-name>, function (document){
        return {
            // indexed properties go here e.g:
            // Name: document.Name
        };
    })

Example I - Simple map index

public class Employees_ByFirstAndLastName : AbstractJavaScriptIndexCreationTask
{
    public Employees_ByFirstAndLastName()
    {
        Maps = new HashSet<string>
        {
            @"map('Employees', function (employee){ 
                    return { 
                        FirstName : employee.FirstName, 
                        LastName : employee.LastName
                    };
                })",
        };
    }
}

Example II - Map index with additional sources

public class BlogPosts_ByCommentAuthor : AbstractJavaScriptIndexCreationTask
{
    public class Result
    {
        public string[] Authors { get; set; }

    }

    public BlogPosts_ByCommentAuthor()
    {
        Maps = new HashSet<string>()
        {
            @"map('BlogPosts', function(b){
                var names = [];
                b.Comments.forEach(x => getNames(x, names));
                return {
                    Authors : names
                };})"
        };
        AdditionalSources = new Dictionary<string, string>
        {
            ["The Script"] = @"function getNames(x, names){
                                names.push(x.Author);
                                x.Comments.forEach(x => getNames(x, names));
                         }"
        };
    }
}

Read more about map indexes here.

Multi map index

Multi-Map indexes allow you to index data from multiple collections

Example

public class Animals_ByName : AbstractJavaScriptIndexCreationTask
{
    public Animals_ByName()
    {
        Maps = new HashSet<string>()
        {
            @"map('cats', function (c){ return {Name: c.Name}})",
            @"map('dogs', function (d){ return {Name: d.Name}})"
        };
    }
}

Read more about multi map indexes here.

Map-Reduce index

Map-Reduce indexes allow you to perform complex aggregations of data. The first stage, called the map, runs over documents and extracts portions of data according to the defined mapping function(s). Upon completion of the first phase, reduction is applied to the map results and the final outcome is produced.

groupBy(x => {map properties})
        .aggregate(y => {
            return {
                // indexed properties go here e.g:
                // Name: y.Name
            };
        })

Example I

public class Products_ByCategory : AbstractJavaScriptIndexCreationTask
{
    public class Result
    {
        public string Category { get; set; }

        public int Count { get; set; }
    }

    public Products_ByCategory()
    {
        Maps = new HashSet<string>()
        {
            @"map('products', function(p){
                return {
                    Category: load(p.Category, 'Categories').Name,
                    Count: 1
                }
            })"
        };

        Reduce = @"groupBy(x => x.Category)
                    .aggregate(g => {
                        return {
                            Category: g.key,
                            Count: g.values.reduce((count, val) => val.Count + count, 0)
                        };
                    })";
    }
}

Example II

public class Product_Sales_ByDate : AbstractIndexCreationTask
{
    public override IndexDefinition CreateIndexDefinition()
    {
        return new IndexDefinition
        {
            Maps =
            {
                @"from order in docs.Orders
                  from line in order.Lines
                  select new {
                      line.Product, 
                      Date = order.OrderedAt,
                      Profit = line.Quantity * line.PricePerUnit * (1 - line.Discount)
                  };"
            },
            Reduce = 
                @"from r in results
                  group r by new { r.OrderedAt, r.Product }
                  into g
                  select new { 
                      Product = g.Key.Product,
                      Date = g.Key.Date,
                      Profit = g.Sum(r => r.Profit)
                  };",

            OutputReduceToCollection = "DailyProductSales",
            PatternReferencesCollectionName = "DailyProductSales/References",
            PatternForOutputReduceToCollectionReferences = "sales/daily/{Date:yyyy-MM-dd}"
        };
    }
}

Read more about map reduce indexes here.

Information

Supported JavaScript version : ECMAScript 5.1