private IAggregateFluent<ProductTypeSearchResult> CreateSearchQuery(string query,
|
IReadOnlyCollection<string> productFamilyIds, int
|
skipCount, int limitCount)
|
{
|
var filter = Builders<ProductType>.Filter.Text(query);
|
return _productTypes
|
.Aggregate()
|
.Match(filter)
|
.Match(productType =>
|
!(string.IsNullOrEmpty(productType.ShopUrl) && string.IsNullOrEmpty(productType.TypeUrl)) &&
|
(productFamilyIds == null || productFamilyIds.Count == 0 ||
|
productFamilyIds.Contains(productType.ProductFamilyId))
|
)
|
.AppendStage<ProductType>("{$addFields: {score: {$meta:'textScore'}}}")
|
.Sort(Sort)
|
.Skip(skipCount)
|
.Limit(limitCount)
|
.Lookup<ProductType, Product, SearchAugmentedProductType>(
|
_products,
|
productType => productType.ProductTypeId,
|
product => product.ProductTypeId,
|
searchAugmentedProductType => searchAugmentedProductType.Products)
|
.Lookup<SearchAugmentedProductType, Delivery, Delivery, IEnumerable<Delivery>,
|
SearchAugmentedProductType>(
|
_deliveries,
|
new BsonDocument { { "products", "$products" } },
|
DeliveriesLookupPipeline,
|
searchAugmentedProductType => searchAugmentedProductType.Deliveries)
|
.Project(pt => new ProductTypeSearchResult
|
{
|
Description = pt.ExternalProductTypeDescription,
|
Id = pt.Id,
|
Name = pt.Name,
|
ProductFamilyId = pt.ProductFamilyId,
|
//Testing for == null does not work if the field does not exist. Have to use null coalescing operator.
|
//Driver version 2.8.1
|
IsExternalUrl = string.IsNullOrEmpty(pt.ShopUrl ?? ""),
|
Url = pt.ShopUrl ?? pt.TypeUrl,
|
Score = pt.Score,
|
// ToList() and ToArray() cannot used as there is no equivalent expression in MongoDb.
|
// We must return an IEnumerable<T>
|
Deliveries = pt.Deliveries.Select(d => new DeliverySearchResult
|
{
|
DeliveryId = d.DeliveryId,
|
Language = d.Language,
|
Platform = d.Platform,
|
OperatingSystem = d.OperatingSystem,
|
ReleaseInfo = d.ReleaseInfo
|
})
|
});
|
}
|