Indexes: Indexing Counters


  • To index counters, create a static index that inherits from AbstractCountersIndexCreationTask.

  • Auto-indexes for counters are not available at this time.

  • In this page:


Syntax

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:

protected void addMap(String map)

addMap only indexes the counters with the specified name.

Examples of indexes using the method:

public static class MyCounterIndex extends AbstractCountersIndexCreationTask {
    public MyCounterIndex() {
        map = "counters.Companies.HeartRate.Select(counter => new {\n" +
                "    heartBeat = counter.Value,\n" +
                "    name = counter.Name,\n" +
                "    user = counter.DocumentId\n" +
                "})";
    }
}


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:

List<String> CounterNamesFor(Object doc);

Example of index using counterNamesFor():

public static class Companies_ByCounterNames extends AbstractIndexCreationTask {
    public Companies_ByCounterNames() {
        map = "from e in docs.Employees\n" +
            "let counterNames = CounterNamesFor(e)\n" +
            "select new\n" +
            "{\n" +
            "   counterNames = counterNames.ToArray()\n" +
            "}";
    }
}

Querying the Index

// return all companies that have 'Likes' counter
List<Company> companies = session
    .query(Company.class, Companies_ByCounterNames.class)
    .containsAny("counterNames", Lists.newArrayList("Likes"))
    .toList();