[CSHARP-2707] Ability to include or inject text search score Created: 23/Aug/19  Updated: 29/Jul/22

Status: Backlog
Project: C# Driver
Component/s: Linq, LINQ3
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Richard Collette Assignee: Unassigned
Resolution: Unresolved Votes: 1
Labels: rp-track, size-small
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 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.


Generated at Wed Feb 07 21:43:19 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.