Firstly, I am just starting with RavenDB, so please be patient when I explain the problem. I am trying to create my first map, map, reduce, convert index. Yes, I know that I am trying to do a lot, but I have most of the work.
So, first I do this in my global.asax to make sure that all the “ID” properties are used as the document identifier.
_documentStore.Conventions.FindIdentityProperty = p => p.Name == "ID";
Now let's look at the index.
public class ProblemListViewIndex : AbstractMultiMapIndexCreationTask<ProblemListView> { public ProblemListViewIndex() { AddMap<Problem>(problems => from problem in problems select new { ID = problem.ID, SolutionCount = 0, }); AddMap<Solution>(solutions => from solution in solutions select new { ID = solution.ProblemID, SolutionCount = 1, }); Reduce = results => from result in results group result by result.ID into g select new { ID = g.Key, SolutionCount = g.Sum(x => x.SolutionCount), }; Indexes.Add(x => x.ID, FieldIndexing.Analyzed); TransformResults = (database, results) => from result in results let problem = database.Load<Problem>("problems/" + result.ID.ToString()) let user = database.Load<User>("users/" + problem.PostedByID.ToString()) select new { ID = result.ID, PostedByID = problem.PostedByID, PostedByName = user.DisplayName, SolutionCount = result.SolutionCount, }; } }
So, everything looks good, and when I test the index on the RavenDB website, I get mixed results. I have recurring forecasts. I have two forecasts that I expect, but two copies of them. This is what the “ID” looks like on the projection results.
- problems / 194
- problems / 195
- 194
- 195
I am confused, but then I returned and looked at the "cards". My code has been translated to something else in the generated index. Here's what the first map looks like when you create it first.
docs.Problems .Select(problem => new {ID = problem.__document_id, SolutionCount = 0})
Even being new to RavenDB, I see a problem. It uses the __document_id field when I want to use the ID field. I modify the map and then save the index as follows.
docs.Problems .Select(problem => new {ID = problem.ID, SolutionCount = 0})
As soon as I do this, my forecast looks exactly as I expected, and how I want it.
My question is, what do I need to do in my code to create my index using "ID" over "__document_id"?