Один из вариантов - смоделировать наоборот, то есть
public class Location()
{
public string Id {get;set;}
public string Name {get;set;}
}
public class Address()
{
public string Id {get;set;}
public string StreetName {get;set;}
public string PostalCode {get;set;}
// Locations is a list with the Location documents IDs
public List<string> Locations {get;set;}
}
И тогда индекс может быть:
from address in docs.Addresses
select new {
StreetAddress = address.StreetName,
PostalCode = address.PostalCode
}
А затем используйте следующий Запрос :
from index 'AddressesIndex' as x
load x.Locations as locations[]
select {
StreetName: x.StreetName,
PostalCode: x.PostalCode,
Locations: locations.map(x => x.Name)
}
==========================================================================
Второй вариант заключается в использовании тех же моделей , которые указаны в описании вопроса, и определите
мульти-карта-уменьшение индекс.
Индекс multi-map-Reduce будет:
Первая карта:
from locationDoc in docs.Locations
from addressId in locationDoc.Addresses
let address = LoadDocument(addressId, "Addresses")
select new {
StreetName = address.StreetName,
PostalCode = (string)null,
LocationNames = new []{locationDoc.Name},
}
Вторая карта:
from address in docs.Addresses
select new{
StreetName = address.StreetName,
PostalCode = address.PostalCode,
LocationNames = (string)null
}
Сокращение:
from result in results
group result by new { result.StreetName } into g
select new {
StreetName = g.Key.StreetName,
PostalCode = g.Select(x => x.PostalCode).FirstOrDefault(x => x != null),
LocationNames = g.SelectMany(x => x.LocationNames)
}