Put Custom Sorter 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 PutSortersOperation to deploy a custom sorter to the RavenDB server.
    Once deployed, you can use it to sort query results for all queries made on the database
    that is scoped to your Document Store.

  • To deploy a custom sorter that will apply cluster-wide, to all databases, see put server-wide sorter.

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

  • In this page:


Put custom sorter

  • 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 PutSortersOperation.

// 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 putSortersOp = new PutSortersOperation(customSorterDefinition);
 
// Execute the operation by passing it to Maintenance.Send
store.Maintenance.Send(putSortersOp);
// 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 putSortersOp = new PutSortersOperation(customSorterDefinition);
 
// Execute the operation by passing it to Maintenance.SendAsync
await store.Maintenance.SendAsync(putSortersOp);

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

Syntax

public PutSortersOperation(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; }
}