The inconsistent index results
A user reported an issue with RavenDB. They got unexpected results in their production database, but when they imported the data locally and tested things, everything worked.
Here is the simplified version of their index:
This is a multi map index that covers multiple collections and aggregate data across them. In this case, the issue was that in production, for some of the results, the CompanyName field was null.
The actual index was more complex, but once we trimmed it down in size to something more manageable, it became obvious what the problem is. Let’s look at the problematic line:
CompanyName = g.First().CompanyName,
The problem is with the First() call. There is no promise of ordering in the grouping results, and you are getting the first item there. If the item happened to be the one from the Company map, the index will appear to work and you’ll get the right company name. However, if the result from the User map will show up first, we’ll have null in the CompanyName.
We don’t make any guarantees about the order of elements in the grouping, but in practice it is often (don’t rely on it) depends on the order of updates in the documents. So you can update the user after the company and see the changes in the index.
The right way to index this data is to do so explicitly, like so:
CompanyName = g.First(x => x.CompanyName != null).CompanyName,
Woah, already finished? 🤯
If you found the article interesting, don’t miss a chance to try our database solution – totally for free!