Skip to main content

Server-wide Custom Sorters

Add server-wide custom sorter - via the Client API

Use PutServerWideSortersOperation to deploy one or more server-wide custom sorters.
Deploying a custom sorter with an existing name replaces the previous version.
Once deployed, you can use it to sort query results in queries made on any database in your cluster.

// 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 = "MyServerWideCustomSorter",

// 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);

You can now order query results using the server-wide custom sorter on any database.

List<Product> products = session
.Query<Product>()
.Where(x => x.UnitsInStock > 10)
// Order by field 'UnitsInStock', pass the name of your custom sorter class
.OrderBy(x => x.UnitsInStock, "MyServerWideCustomSorter")
.ToList();

// Results will be sorted by the 'UnitsInStock' value
// according to the logic from 'MyServerWideCustomSorter' class

Add server-wide custom sorter - via Studio

The server-wide custom sorters view

Server-wide custom sorter viewServer-wide custom sorter view

  1. Go to Manage Server > Server-Wide Sorters.
  2. Click to add a new server-wide custom sorter. See Add a server-wide custom sorter below.
  3. The server-wide custom sorters are listed here.
  4. Click to edit this server-wide custom sorter.
  5. Click to delete this server-wide custom sorter.

Add a server-wide custom sorter

Add server-wide custom sorterAdd server-wide custom sorter

  1. The sorter name must be the same as the class name of your sorter.
  2. You can load the sorter's code from a *.cs file.
  3. Or, you can enter the code manually in this editor.
    The sorter code must be compilable and include all necessary using statements.
    The sorter's class should inherit from Lucene.Net.Search.FieldComparator.
    Learn more in How to write a custom sorter.
  4. Save your server-wide sorter or discard.

Delete server-wide custom sorter

In addition to deleting a server-wide custom sorter via The server-wide custom sorters view in Studio,
you can use DeleteServerWideSorterOperation to delete a server-wide custom sorter.
Once removed, the sorter will no longer be available for ordering query results in any database in the cluster.

// Define the delete sorter operation, pass the name of the sorter to delete
var deleteSorterOp = new DeleteServerWideSorterOperation("MyServerWideCustomSorter");

// Execute the operation by passing it to Maintenance.Server.Send
store.Maintenance.Server.Send(deleteSorterOp);

Test custom sorter

To test a server-wide sorter, go to the Custom Sorters view in a specific database and use the test option from there.
See Test custom sorter.

Syntax

PutServerWideSortersOperation

Deploys one or more custom sorters to the server-wide scope.

public PutServerWideSortersOperation(params SorterDefinition[] sortersToAdd)
ParameterTypeDescription
sortersToAddSorterDefinition[]One or more sorter definitions to deploy. At least one is required.
public sealed class SorterDefinition
{
// The sorter's name. Must match the class name in the source code.
public string Name { get; set; }

// The complete C# source code of the sorter, including all `using` statements.
// Must be compilable.
public string Code { get; set; }
}

DeleteServerWideSorterOperation

Removes a custom sorter from the server-wide scope.

public DeleteServerWideSorterOperation(string sorterName)
ParameterTypeDescription
sorterNamestringThe name of the sorter to remove.

In this article