Search code examples
c#indexingravendb

Get RavenDB index stats without a query


I'm trying to find out programatically if an index is stale or not, and ideally how far behind it is.

My example is an index to see whether users have permissions, based on this index;

public class PermissionType_GrantedTo : AbstractIndexCreationTask<Person, GrantedPermissionResult>
{
    ...
}

The best I've managed so far is this -- a 'take 0 results' query on the indexes results -- and I don't like it;

RavenQueryStatistics stats;
var results = await session.Query<GrantedPermissionResult>()
    .Statistics(out stats)
    .Take(0)
    .ToListAsync();

stale = stats.IsStale;
log($"{(stale ? "Stale" : "Fresh")}");

There's got to be a way to get the current state of an index, right?


Solution

  • If all you need to know is whether a given index is stale or not, you can do the following:

    var indexName = new GrantedPermissionResult().IndexName;
    var isStale = documentStore.DatabaseCommands.GetStatistics().StaleIndexes.Contains(indexName);
    

    If you want other statistics about any index, you can find it here:

    documentStore.DatabaseCommands.GetStatistics().Indexes