indexes: Indexing Counters
-
To index counters, create a static index that inherits from
AbstractCountersIndexCreationTask
orAbstractRawJavaScriptCountersIndexCreationTask
. -
Auto-indexes for counters are not available at this time.
-
In this page:
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.map("map");
map
only indexes the counters with
the specified name.
Examples of indexes using each method:
export class MyCounterIndex extends AbstractCountersIndexCreationTask {
constructor() {
super();
this.map = `from counter in docs.counters select new {
Likes = counter.Value,
Name = counter.Name,
User = counter.DocumentId
}`;
}
}
AbstractRawJavaScriptCountersIndexCreationTask
Creating an index inheriting from AbstractCsharpCountersIndexCreationTask
allows
you to write your map and reduce functions in JavaScript.
Learn more about JavaScript indexes here.
class MyCounterIndex extends AbstractRawJavaScriptCountersIndexCreationTask {
public constructor() {
super();
this.maps.add(
"counters.map('Companies', 'HeartRate', function (counter) {\n" +
"return {\n" +
" heartBeat: counter.Value,\n" +
" name: counter.Name,\n" +
" user: 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:
Example of index using CounterNamesFor
:
class Companies_ByCounterNames extends AbstractCountersIndexCreationTask{
constructor() {
super();
this.map = "docs.Companies.Select(e => new {\n"+
"e = e,\n"+
" counterNames = this.CounterNamesFor(e)\n"+
"}).Select(this0 => new {\n"+
" CounterNames = Enumerable.ToArray(this0.counterNames)\n"+
"})"
}
}
Querying the Index
const companies = session
.query({index: "Companies_ByCounterNames"})
.containsAny("counterNames", ["Likes"])
.all();