I am new to using RavenDB and trying to get indexes to work in a simple MVC3 app which allows users to enter geographic locations. I have two models, a UserModel and a LocationModel. The LocationModel stores the UserId when saved and I am trying to create an index on this.
public class Locations_ByUser : AbstractIndexCreationTask<LocationModel>
{
public Locations_ByUser()
{
Map = locations => from location in locations
select new { location.UserId };
}
}
I am registering the index with the following code
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
//ADD THE MODEL BINDER FOR LIST TO STRING
ModelBinders.Binders.Add(typeof(TestAPI.Models.LocationModel), new TestAPI.Classes.LocationModelBinder());
//INIT THE STORE, DO ONCE PER APP START
TestAPI.Classes.DataDocumentStore.Initialize();
//SET THE INDEXES
IndexCreation.CreateIndexes(typeof(Locations_ByUser).Assembly, TestAPI.Classes.DataDocumentStore.Instance);
}
However, when i try to call the index from the mvc app
[HttpGet]
public ActionResult Index()
{
var result = this.DocumentSession.Query<LocationModel>("Locations_ByUser").ToList();
foreach (var userid in result)
{
Console.Out.WriteLine(userid);
}
return View();
}
it returns the following error
Could not find index named: Locations_ByUser
I was wondering if anyone else has encountered this before and could point me in the right direction. Thanks in advance.
The index in RavenDB will actually be named: “Locations/ByUser” when it’s generated.
If you open up the Raven Studio you can see this under the index’s. The _ is replaced with /
Also you don’t need to specify the string value, you can write your query like: