We have a MutiMapIndexCreationTask. There is an internal load that pulls in related data. We know that is not best practice, and the data should be denormalized if possible, but that's not an option at this moment. When the index is defined it appears to work correctly, until one of the related docs is updated. At that point the related document gets out of sync and the index is not updated. This continues to happen until an additional related doc is added to the top level doc as a relationship, at this point everything works as expected.
Here is the Index Definition. The offending property is "LastFirstDisplayName" on a contact.
AddMap<Client>(clients =>
from client in clients
select new
{
ClientId = client.Id_ForIndexing,
ProfileId = (Guid?)null,
ProfileName = (string)null,
ProfileReqNbr = Int64.MaxValue,
Contacts = new object[] { }
}
);
AddMap<Client>(
clients =>
from client in clients
from clientRelationship in client.Contacts
let clientContact = LoadDocument<Contact>("Contacts/" + clientRelationship.ContactId)
select new
{
ClientId = client.Id_ForIndexing,
ProfileId = (Guid?)null,
ProfileName = (string)null,
ProfileReqNbr = 0L,
Contacts = clientContact == null
? new object[] { }
: new[]
{
new
{
ContactId = clientContact.Id_ForIndexing,
ContactName = clientContact.LastFirstDisplayName,
IsReportRecipient = clientRelationship.IsReportRecipient,
IsInvoiceRecipient = clientRelationship.IsInvoiceRecipient
}
}
}
);
AddMap<Client>(clients =>
from client in clients
from profile in client.Profiles
select new
{
ClientId = client.Id_ForIndexing,
ProfileId = profile.Id,
ProfileName = String.Format("{0} {1}", profile.ReqNbr, profile.Name),
ProfileReqNbr = profile.ReqNbr,
Contacts = new object[] { }
}
);
AddMap<Client>(
clients =>
from client in clients
from profile in client.Profiles
from profileRelationship in profile.Contacts
let profileContact = LoadDocument<Contact>("Contacts/" + profileRelationship.ContactId)
select new
{
ClientId = client.Id_ForIndexing,
ProfileId = profile.Id,
ProfileName = String.Format("{0} {1}", profile.ReqNbr, profile.Name),
ProfileReqNbr = profile.ReqNbr,
Contacts = profileContact == null
? new object[] { }
: new[]
{
new
{
ContactId = profileContact.Id_ForIndexing,
ContactName = profileContact.LastFirstDisplayName,
IsReportRecipient = profileRelationship.IsReportRecipient,
IsInvoiceRecipient = profileRelationship.IsInvoiceRecipient
}
}
}
);
Reduce = results =>
from result in results
group result by new
{
result.ClientId,
result.ProfileId
} into grouped
select new ClientContactTreeResult
{
ClientId = grouped.Key.ClientId,
ProfileId = grouped.Key.ProfileId,
ProfileName = grouped.Select(x => x.ProfileName).First(),
ProfileReqNbr = grouped.Select(x => x.ProfileReqNbr).First(),
Contacts = grouped
.SelectMany(x => x.Contacts)
.OrderBy(x => x.ContactName)
.ToArray()
};
Index(x => x.ProfileReqNbr, FieldIndexing.Default);
Sort(x => x.ProfileReqNbr, SortOptions.Long);