Checking for Existing Index In RavenDB
For my current personal project (i.e., code I am writing for my wife) I am using RavenDB for a few reasons:
- It’s time to break out of the OO/RDBMS impedance mismatch
- Ayende is a freaking monster
- It seems the most promising of the document db’s out there
In order to do queries by anything other than an id, you need to create an index. The problem space for this site is theater and here is the code I want to write.
private static readonly IndexDefinition FindByNameIndex = new IndexDefinition
{
Map = shows => from show in shows select new { show.Title }
};
public static void ConfigureIndices(DocumentStore documentStore)
{
var showsIndex = documentStore.DatabaseCommands.GetIndex("ShowsByTitle");
if(showsIndex !=null)
documentStore.DatabaseCommands.PutIndex("ShowsByTitle", FindByNameIndex);
}
What I find odd about the API at this point is that the GetIndex method throws an InvalidOperationException index doesn’t exist (which is fine, no problems there) but that there doesn’t seem to be a way to check if the index does exist, something like:
bool indexExists = documentStore.DatabaseCommands.DoesIndexExist("ShowsByTitle");
I could just wrap the current call in a try/catch, but that seems a little strange for what seems to be a standard use case of checking to see if something exists.

Leave a Reply