Details
-
New Feature
-
Resolution: Unresolved
-
Major - P3
-
None
-
None
Description
I am unable to use LINQ and have to resort to huge fluent interfaces like the following, solely because I cannot sort on the text search score.
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
|
})
|
});
|
}
|
If I could modify my WhereText extension (which would also be nice to include with theĀ method to be something like:
public static IMongoQueryable<T> WhereText<T>(this IMongoQueryable<T> query, string search, Func<T, ?> propertyExpression) |
{
|
var filter = Builders<T>.Filter.Text(search); |
return query.Where(_ => { |
filter.Inject());
|
filter.InjectScore(propertyExpression);
|
}
|
Where the propertyExpression is likeĀ p=>p.somePropertyName
I think that would alleviate the issue. Right now, full text search queries are a major time suck to implement.