Indexes: Indexing Counters
-
To index counters, create a static index that inherits from
AbstractCountersIndexCreationTask
orAbstractJavaScriptCountersIndexCreationTask
. -
Auto-indexes for counters are not available at this time.
-
In this page:
Syntax
To index counter values, create an index that inherits from AbstractCountersIndexCreationTask
and use map
as follows.
class MyCounterIndex(AbstractCountersIndexCreationTask):
def __init__(self):
super().__init__()
self.map = (
"from counter in counters "
"select new { likes = counter.Value, name = counter.Name, user = counter.DocumentId }"
)
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 : AbstractCountersIndexCreationTask
{
public HashSet<string> Maps;
protected string Reduce;
}
Property | Type | Description |
---|---|---|
maps | Set[str] |
The set of javascript map functions |
reduce | str |
The javascript reduce function |
Example:
class MyMultiMapCounterIndex(AbstractJavaScriptCountersIndexCreationTask):
def __init__(self):
super().__init__()
self.maps = """
counters.map('Blogposts', 'Likes', function (counter) {
return {
Likes: counter.Value,
Name: counter.Name,
Blog Post: counter.DocumentId
};
})
"""
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:
IEnumerable<string> CounterNamesFor(object doc);
Example of index using CounterNamesFor
:
class Companies_ByCounterNames(AbstractIndexCreationTask):
class Result:
def __init__(self, counter_names: List[str] = None):
self.counter_names = counter_names
def __init__(self):
super().__init__()
self.map = (
"from e in docs.Employees "
"let counterNames = CounterNamesFor(e) "
"select new { counter_names = counterNames.ToArray() }"
)