Indexes: Indexing Counters

Counter names can be indexed using CounterNamesFor.

Indexing and Counters

Please note that re-indexation will only happen when counter is added or deleted from the document. The counter increments will not trigger the process.

Creating Indexes

The CounterNamesFor method available in AbstractIndexCreationTask returns all of the counter names for a document passed as the first argument.

IEnumerable<string> CounterNamesFor(object doc);

public class Companies_ByCounterNames : AbstractIndexCreationTask<Company>
{
    public class Result
    {
        public string[] CounterNames { get; set; }
    }

    public Companies_ByCounterNames()
    {
        Map = employees => from e in employees
                           let counterNames = CounterNamesFor(e)
                           select new Result
                           {
                               CounterNames = counterNames.ToArray()
                           };
    }
}

Example

// return all companies that have 'Likes' counter
List<Company> companies = session
    .Query<Companies_ByCounterNames.Result, Companies_ByCounterNames>()
    .Where(x => x.CounterNames.Contains("Likes"))
    .OfType<Company>()
    .ToList();
// return all companies that have 'Likes' counter
List<Company> companies = await asyncSession
    .Query<Companies_ByCounterNames.Result, Companies_ByCounterNames>()
    .Where(x => x.CounterNames.Contains("Likes"))
    .OfType<Company>()
    .ToListAsync();
// return all companies that have 'Likes' counter
List<Company> companies = session
    .Advanced
    .DocumentQuery<Company, Companies_ByCounterNames>()
    .ContainsAny("CounterNames", new[] { "Likes" })
    .ToList();