Atlas Search + C# + Sorting

I cannot find a way to add a “Sort” to the MongoDB Search function within C#. Am I missing something or must this be done via BsonDocuments?

Simple Example:

coll.Aggregate(new AggregateOptions()
                                        {
                                            BatchSize = 1000
                                        })
                                        .Search(
                                            compound.ToSearchDefinition(),
                                            indexName: "myCoolIndex",
                                            highlight: new SearchHighlightOptions<T>(Builders<T>.SearchPath.Multi(searchpaths)),
                       ***** SEARCH GO HERE? *****                     
                                            returnStoredSource: true)

Of course I know I can do the below, but is it part of the C# driver and I am just missing it?

new BsonArray
{
    new BsonDocument("$search", 
    new BsonDocument
        {
            { "index", "myCoolIndex" }, 
            { "compound", 
    new BsonDocument("should", 
    new BsonArray
                {
                    new BsonDocument("autocomplete", 
                    new BsonDocument
                        {
                            { "query", "aSearchValue" }, 
                            { "path", "somefieldpath" }
                        }),
                    new BsonDocument("autocomplete", 
                    new BsonDocument
                        {
                            { "query", "aSearchValue" }, 
                            { "path", "a Different Field Path" }
                        })
                }) }, 
            { "highlight", 
    new BsonDocument("path", 
    new BsonArray
                {
                    "some Field Path",
                    "a Different Field Path"
                }) }, 
            { "sort", 
    new BsonDocument("A Field to Sort On", -1) }, 
            { "returnStoredSource", true }
        }),
    new BsonDocument("$addFields", 
    new BsonDocument("SearchHighlightDetails", 
    new BsonDocument("$meta", "searchHighlights")))
}

Is there any plan for the C# driver “.Search” function to accept a FilterDefinition for the “filter” component that can accompany a search or a SortDefinition directly?

Hi @Mark_Mann and welcome to MongoDB community forums!!

Could you confirm if the below links are the ones that you are looking for.
https://jira.mongodb.org/browse/CSHARP-4728
The pull request for which is

The 10 July 2023 release of MongoDB Atlas Search introduced the sort option.

Let us know if you are looking for something else.

Regards
Aasawari

1 Like

This does appear to be what I am looking for. Thank you

2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.