First off I’m just getting started with RavenDB so please be patient as I explain the issue. I’m trying to create my first map, map, reduce, transform index. Yeah I know I’m trying to do a lot but I have most of it working.
So first off I do this in my global.asax to make sure that all “ID” properties are used as the identifier on documents.
_documentStore.Conventions.FindIdentityProperty = p => p.Name == "ID";
OK now lets take a 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 in the RavenDB website I get mixed results. I have duplicate projections. I have the two projections that I expect but two copies of them. Here are what the “ID” looks like on the projection results.
- problems/194
- problems/195
- 194
- 195
I’m confused but then I went back and looked at the “maps”. My code translated into something different in the created index. Here is what the first map looks like when created initially.
docs.Problems
.Select(problem => new {ID = problem.__document_id, SolutionCount = 0})
Even being new to RavenDB I see the problem. It’s using the “__document_id” field when I want to use the “ID” field. I change the map in then save the index to the following.
docs.Problems
.Select(problem => new {ID = problem.ID, SolutionCount = 0})
Once I do that my projection looks exactly as I expected and as I want it.
- 194
- 195
My question is what do I need to do in my code to get my index to create using “ID” over “__document_id”?
I don’t see what’s wrong here. I#ve put together a test using your code and I get what I have expected: