Put Custom Sorter (Server-Wide) Operation


  • The Lucene indexing engine allows you to create your own Custom Sorters
    where you can define how query results will be ordered based on your specific requirements.

  • Use PutServerWideSortersOperation to deploy a custom sorter to the RavenDB server.
    Once deployed, you can use it to sort query results for all queries made on all databases in your cluster.

  • To deploy a custom sorter that will apply only to the database scoped to your Document Store,
    see put custom sorter.

  • A custom sorter can also be uploaded server-wide from the Studio.

  • In this page:


Put custom sorter server-wide

  • First, create your own sorter class that inherits from the Lucene class Lucene.Net.Search.FieldComparator.

  • Then, send the custom sorter to the server using the PutServerWideSortersOperation.

// Assign the code of your custom sorter as a `string`
string mySorterCode = "<code of custom sorter>";

// Create the `SorterDefinition` object
var customSorterDefinition = new SorterDefinition
{
    // The sorter Name must be the same as the sorter's class name in your code
    Name = "MySorter",
    // The Code must be compilable and include all necessary using statements
    Code = mySorterCode
};

// Define the put sorters operation, pass the sorter definition
// Note: multiple sorters can be passed, see syntax below
var putSortersServerWideOp = new PutServerWideSortersOperation(customSorterDefinition);
 
// Execute the operation by passing it to Maintenance.Server.Send
store.Maintenance.Server.Send(putSortersServerWideOp);
// Assign the code of your custom sorter as a `string`
string mySorterCode = "<code of custom sorter>";

// Create the `SorterDefinition` object
var customSorterDefinition = new SorterDefinition
{
    // The sorter Name must be the same as the sorter's class name in your code
    Name = "MySorter",
    // The Code must be compilable and include all necessary using statements
    Code = mySorterCode
};

// Define the put sorters operation, pass the sorter definition
// Note: multiple sorters can be passed, see syntax below
var putSortersServerWideOp = new PutServerWideSortersOperation(customSorterDefinition);
 
// Execute the operation by passing it to Maintenance.Server.SendAsync
await store.Maintenance.Server.SendAsync(putSortersServerWideOp);

You can now order your query results using the custom sorter.
A query example is available here.

Syntax

public PutServerWideSortersOperation(params SorterDefinition[] sortersToAdd)
Parameter Type Description
sortersToAdd SorterDefinition[] One or more Sorter Definitions to send to the server

public class SorterDefinition
{
    public string Name { get; set; }
    public string Code { get; set; }
}