[CSHARP-3285] Per-document scoring metadata for $search Created: 08/Dec/20  Updated: 27/May/22  Resolved: 07/Jan/21

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Backlog - Core Eng Program Management Team Assignee: Dmitry Lukyanov (Inactive)
Resolution: Won't Do Votes: 0
Labels: FY22Q1
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
depends on SERVER-49744 Per-document scoring metadata for $se... Closed
Server Compat: 4.4, 4.9

 Description   
Downstream Change Summary

This patch adds support for a new $meta keyword for $search: '$searchScoreDetails'. This keyword accepts a BSONObj payload from mongot.

Description of Linked Ticket

Add document level metadata to each $search result to explain mongot scoring calculation.



 Comments   
Comment by James Kovacs [ 07/Jan/21 ]

As noted above, $searchScoreDetails can be supported using the existing ProjectionDefinitionBuilder<T>.Meta method. The following sample code demonstrates adding both searchHighlights and searchScoreDetails to a $search aggregation pipeline.

using System;
using MongoDB.Bson;
using MongoDB.Driver;
 
namespace CSharp3285
{
    class Program
    {
        static void Main(string[] args)
        {
            var searchStage = @"
            {
                $search : {
                    text : {
                        query : 'pizza',
                        path : 'description'
                    }
                },
                highlight: { path: 'description' },
                scoreDetails: true
            }";
            var projection = Builders<BsonDocument>.Projection.Meta("highlights", "searchHighlights")
                                                              .Meta("scoreDetails", "searchScoreDetails");
            var pipeline = new EmptyPipelineDefinition<BsonDocument>()
                                .AppendStage<BsonDocument, BsonDocument, BsonDocument>(searchStage)
                                .Project(projection);
            Console.WriteLine(pipeline);
        }
    }
}

The following is the resulting output:

[{ "$search" : { "text" : { "query" : "pizza", "path" : "description" } }, "highlight" : { "path" : "description" }, "scoreDetails" : true }, { "$project" : { "highlights" : { "$meta" : "searchHighlights" }, "scoreDetails" : { "$meta" : "searchScoreDetails" } } }]

This pipeline can be executed against an Atlas cluster via collection.Aggregate(pipeline).

Comment by Dmitry Lukyanov (Inactive) [ 07/Jan/21 ]

We don't have a typed builder for $meta, instead we allow passing just a string value: https://github.com/mongodb/mongo-csharp-driver/blob/master/src/MongoDB.Driver/ProjectionDefinitionBuilder.cs#L152 Related tests: https://github.com/mongodb/mongo-csharp-driver/blob/master/tests/MongoDB.Driver.Tests/ProjectionDefinitionBuilderTests.cs#L140. So, we already support this feature using the generic Meta method which accepts an arbitrary string value. Therefore no driver-related work is required.

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