Language
C# Java
Article For
4.1 4.2 5.1 5.3

indexes: Indexing Counters



Usage

In order to index counter values, create an index that inherits from AbstractCountersIndexCreationTask. Next, choose one of these two methods which take the index expression:

this.maps = new Set(["map"]);

this.map("map");

maps indexes all the counters in the indexed documents. addMap only indexes the counters with the specified name.

Examples of indexes using each method:

class MyCounterIndex extends AbstractCountersIndexCreationTask {
     constructor() {
        super();

        this.map = "counters.Companies.HeartRate.Select(counter => new {\n" +
            "    heartBeat = counter.Value,\n" +
            "    name = counter.Name,\n" +
            "    user = counter.DocumentId\n" +
            "})";
    }
}



AbstractJavaScriptCountersIndexCreationTask

Creating an index inheriting from AbstractJavaScriptCountersIndexCreationTask allows you to write your map and reduce functions in JavaScript.
Learn more about JavaScript indexes here.


CounterNamesFor

While indexes inheriting from AbstractIndexCreationTask cannot index counter values, the counterNamesFor() method is available which returns the names of all counters for a specified document:

let counterNames = counterNamesFor(employee);

Example of index using CounterNamesFor:

class Companies_ByCounterNames extends AbstractCountersIndexCreationTask{
     constructor() {
        super();
         this.map = "from e in docs.Employees\n" +
             "let counterNames = counterNamesFor(e)\n" +
             "select new{\n" +
             "   counterNames = counterNames.ToArray()\n" +
             "}";
    }
}

Querying the Index

const companies = session
    .query({index: "Companies_ByCounterNames"})
    .containsAny("counterNames", Lists.newArrayList("Likes"))