Indexes: Indexing Counters



Syntax

In order to index counter values, create an index that inherits from AbstractCountersIndexCreationTask. Next, use these method which take the index expression:

protected void addMap(String map)

addMap only indexes the counters with the specified name.

Examples of indexes using each 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" +
                "})";
    }
}



AbstractJavaScriptCountersIndexCreationTask

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

public class AbstractJavaScriptCountersIndexCreationTask extends AbstractIndexCreationTaskBase<CountersIndexDefinition> 
{
    private final CountersIndexDefinition _definition = new CountersIndexDefinition();

    public Set<String> getMaps() {
        return _definition.getMaps();
    }

    public void setMaps(Set<String> maps) {
        _definition.setMaps(maps);
    }

    public Map<String, IndexFieldOptions> getFields() {
        return _definition.getFields();
    }

    public void setFields(Map<String, IndexFieldOptions> fields) {
        _definition.setFields(fields);
    }

    protected String getReduce() {
        return _definition.getReduce();
    }

    protected void setReduce(String reduce) {
        _definition.setReduce(reduce);
    }

    @Override
    public boolean isMapReduce() {
        return getReduce() != null;
    }

    /**
     * @return If not null than each reduce result will be created as a document in the specified collection name.
     */
    protected String getOutputReduceToCollection() {
        return _definition.getOutputReduceToCollection();
    }
}
Property Type Description
Maps Set<String> The set of javascript map functions
Reduce String The javascript reduce function

Example:

public static class MyMultiMapCounterIndex extends AbstractJavaScriptCountersIndexCreationTask {
    public MyMultiMapCounterIndex() {
        setMaps(Collections.singleton("counters.map('Blogposts', 'Likes', function (counter) {\n" +
            "return {\n" +
            "    ikes: counter.Value,\n" +
            "    name: counter.Name,\n" +
            "    bolg post: counter.DocumentId\n" +
            "};\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",  Arrays.asList("companies_likes"))
    .selectFields(Company.class, "likes")
    .toList();