8

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?

Alwyn
  • 8,079
  • 12
  • 59
  • 107
  • I know that this doesn't answer your question, but I felt it was worth mentioning that RavenDB does automatically create indexes for you if you let it. See [this RavenDB doc page](http://ravendb.net/docs/client-api/querying) and especially the part about dynamic indexes. – ean5533 Dec 12 '12 at 22:18
  • @ean5533 - that applies to dynamic indexes, yes. I believe the OP is concerned about creation of static indexes. – Matt Johnson-Pint Dec 12 '12 at 22:22

2 Answers2

18

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.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • thanks for the explanation. I had an "aha-moment" after reading this! – trailmax May 08 '13 at 23:30
  • This advice doesn't seem right. If I call PutIndex on an index that already exists I am getting an InvalidOperationException - Index Already Exists. I'm using v2.5. Am I doing something wrong ? – Chris Aug 29 '13 at 07:34
  • 2
    you have an option to override it by settings 3rd argument to true – Abhay Naik Oct 11 '13 at 05:24
0

Contemporary answers (RavenDB v4.x) to the main and implied questions would be:

How to check if index exists?

There is no way to check for a particular index, but you can get a list of all indexes (see official docs) like below

IndexDefinition[] indexes = store.Maintenance.Send(new GetIndexesOperation(0, 100));

How to migrate the database?

Yes, you can run index creation on each launch of the app. It'll do for a small app and will be quick if no changes required (see official docs):

IndexCreation.CreateIndexes(typeof(YourClass).Assembly, _store);

However, for a bigger app I'd encourage to create a migration procedure (either a separate tool or just another endpoint in the app) where you handle:

  • Errors in indexes (e.g. see a story of LINQ gotchas). Index errors may happen and some records can fall out of the query results if they fail to be indexed. Get notified on index errors ASAP. See "How to Get Index Errors" in the docs.
  • Have migration procedure in place. Many DB modifications may require to execute a migration script, which subsequently requires DB versioning, ensuring you're running correct version, etc. Check out RavenMigrations NuGet package for that.
Alex Klaus
  • 8,168
  • 8
  • 71
  • 87