RavenDB version 2.5. Other versions:

Customizing results order using SortOptions

Indexes in RavenDB are lexicographically sorted by default, so all queries return results which are ordered lexicographically. When putting a static index in RavenDB, you can specify custom sorting requirements, to ensure results are sorted the way you want them to.

Native types

Dates are written to the index in a form which preserves lexicography order, and is readable by both human and machine (like so: 2011-04-04T11:28:46.0404749+03:00), so this requires no user intervention, too.

Numerical values, on the other hand, are stored as text and therefore require the user to specify explicitly what is the number type used so a correct sorting mechanism is enforced. This is quite easily done, by declaring the required sorting setup in SortOptions in the index definition:

    public class SampleIndex1 : AbstractIndexCreationTask<Customer, Customer>
    {
    	public SampleIndex1()
    	{
    		Map = users => from user in users
    					   select new
    						   {
    							   user.Age
    						   };
    
    		Sort(x => x.Age, SortOptions.Short);
    	}
    }
    
    

The index outlined above will allow sorting by value on the user's age (1, 2, 3, 11, etc). If we wouldn't specify this option, it would have been sorted lexically (1, 11, 2, 3, etc).

The default SortOptions value is String. Appropriate values available for all numeric types (Byte, Double, Float, Int, Long and Short).

Specifing the Sort in the index definitoin won't make results from this index be ordered unless you call OrderBy on the query itself.

Usage example

In the following query we are using the OrderBy method in order to indicate that we want to sort on Age:

    List<Customer> customers = session.Query<Customer>()
    	.OrderBy(customer => customer.Age)
    	.ToList();
    
    

So, by default it will sort on Age as it was a string. By specifing Sort(x => x.Age, SortOptions.Short) in the index dedinition, we inidicate that the sort order should be in a numerical order.

Collation support

RavenDB supports using collations for documents sorting and indexing. You can setup a specific collation for an index field, so you can sort based of culture specific rules.

The following is an example of an index definition which allows sorting based on the Swedish lexical sorting rules:

    public class SampleIndex2 : AbstractIndexCreationTask<Customer, Customer>
    {
    	public SampleIndex2()
    	{
    		Map = users => from doc in users select new { doc.Name };
    
    		Sort(x => x.Name, SortOptions.String);
    
    		Analyzers.Add(x => x.Name, "Raven.Database.Indexing.Collation.Cultures.SvCollationAnalyzer, Raven.Database");
    	}
    }
    
    

In general, you can sort using [two-letters-culture-name]CollationAnalyzer, and all the cultures supported by the .NET framework are supported.

Comments add new comment

The comments section is for user feedback or community content. If you seek assistance or have any questions, please post them at our support forums.

Evgeny Rumyantsev
REPLY Posted by Evgeny Rumyantsev on

Sorry, how set direct desc in Sort method?

Ayende Rahien
REPLY Posted by Ayende Rahien on

Evgeny, There is not way to specify that when indexing. Instead, you specify the direction of sort you want during the query itself, using OrderByDescending().

Sascha
REPLY Posted by Sascha on

So if we sort the index - only ascending as you say - and then query with OrderByDescending does raven take advantage of the index sorting?

Ayende Rahien
REPLY Posted by Ayende Rahien on

You don't need to sort the index, it is already sorted for you.

What you can do is specify the sort option, such it will be sorted as a number, for example.

And yes, OrderBy AND OrderByDescending will use the index sorting.

Jakub Konecki
REPLY Posted by Jakub Konecki on

Collation support example for v2.5 doesn't seem to be using Swedish culture at all.

Wallace Turner
REPLY Posted by Wallace Turner on

>Dates are written to the index in a form which preserves lexicography order, and is readable by both human and machine

Does that mean the indexes should use SortOptions.String? eg Sort(x=>x.CreatedOn, SortOptions.String);

where CreateOn is a DateTime ?

Wallace Turner
REPLY Posted by Wallace Turner on

>Dates are written to the index in a form which preserves lexicography order, and is readable by both human and machine

Does that mean the indexes should use SortOptions.String? eg Sort(x=>x.CreatedOn, SortOptions.String);

where CreateOn is a DateTime ?

Wallace Turner
REPLY Posted by Wallace Turner on

>Dates are written to the index in a form which preserves lexicography order, and is readable by both human and machine

Does that mean the indexes should use SortOptions.String? eg Sort(x=>x.CreatedOn, SortOptions.String);

where CreateOn is a DateTime ?

Wallace Turner
REPLY Posted by Wallace Turner on

>Dates are written to the index in a form which preserves lexicography order, and is readable by both human and machine

Does that mean the indexes should use SortOptions.String? eg Sort(x=>x.CreatedOn, SortOptions.String);

where CreateOn is a DateTime ?

Wallace Turner
REPLY Posted by Wallace Turner on

>Dates are written to the index in a form which preserves lexicography order, and is readable by both human and machine

Does that mean the indexes should use SortOptions.String? eg Sort(x=>x.CreatedOn, SortOptions.String);

where CreateOn is a DateTime ?

Wallace Turner
REPLY Posted by Wallace Turner on

>Dates are written to the index in a form which preserves lexicography order, and is readable by both human and machine

Does that mean the indexes should use SortOptions.String? eg Sort(x=>x.CreatedOn, SortOptions.String);

where CreateOn is a DateTime ?

Wallace Turner
REPLY Posted by Wallace Turner on

>Dates are written to the index in a form which preserves lexicography order, and is readable by both human and machine

Does that mean the indexes should use SortOptions.String? eg Sort(x=>x.CreatedOn, SortOptions.String);

where CreateOn is a DateTime ?

Wallace Turner
REPLY Posted by Wallace Turner on

>Dates are written to the index in a form which preserves lexicography order, and is readable by both human and machine

Does that mean the indexes should use SortOptions.String? eg Sort(x=>x.CreatedOn, SortOptions.String);

where CreateOn is a DateTime ?

SUBMIT COMMENT