RavenDb check if index exists

You don't have to check for existence. The server will automatically compare the index definition you send and check to see if it already exists. If one exists with the same name and definition, then it is left alone. If one exists with the same name, but the definition has changed, then the old one is dropped and the new one is created.

Usually one would create indexes in the same application, on application startup. For web apps that could be in global.asax, and for console/desktop apps it would just be the first part of the startup code.

But sometimes that's not possible, such as if you have many different databases, as multi-tenant applications often do. In those cases, you would create indexes when you create each tenant database, and you might need to update or create more indexes when you roll out a version upgrade.

Also, I should mention that you can create indexes a few different ways.

// scans the assembly for all indexes and creates them
IndexCreation.CreateIndexes(assembly, documentStore);

// scans a MEF catalog for all indexes and creates them
IndexCreation.CreateIndexes(catalog, documentStore);

// puts a single index the HARD way
documentStore.DatabaseCommands.PutIndex(...);

// puts a single index the easy way
documentStore.ExecuteIndex(new YourIndexCreationTask());

There are a few others, but you get the idea.

And just to be thorough, if you really did want to check for index existance, you could use:

documentStore.DatabaseCommands.GetIndex("YourIndex") != null

But that will only check by name, not by definition. And you don't need it.