Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RavenDb check if index exists

How do I check whether an index exists? - before calling:

IndexCreation.CreateIndexes(typeof(MyIndexClass).Assembly, documentStore);

All the examples I have seen (including the ones in the sample project) had the index re-created every time the client is started which doesn't seem right.

Also what is the general strategy? It seems like there's the normal CRUD operation, then there's administration commands such as the indexing one above. Do people just create a console app that does the admin and deploy/run that separately from the main app?

like image 292
Alwyn Avatar asked Dec 12 '12 22:12

Alwyn


1 Answers

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.

like image 142
Matt Johnson-Pint Avatar answered Oct 18 '22 07:10

Matt Johnson-Pint