Indexes: Sorting & Collation
Starting from version 4.0, RavenDB automatically determines sorting based on an indexed value. All values will have a capability to be sorted lexicographically
. Numerical values will also be sortable by their double
and long
value.
Date 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
). This requires no user intervention.
Example
Please read our dedicated article describing sorting
capabilities when queries are executed. It can be found here.
Collation
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 static class Products_ByName extends AbstractIndexCreationTask {
public Products_ByName() {
map = "docs.Products.Select(product => new { " +
" Name = product.Name " +
"})";
analyze("Name", "Raven.Server.Documents.Indexes.Persistence.Lucene.Analyzers.Collation.Cultures.SvCollationAnalyzer, Raven.Server");
}
}
private static class Products_ByName extends AbstractJavaScriptIndexCreationTask {
public Products_ByName() {
setMaps(Sets.newHashSet("map('products', function (u){\n" +
" return {\n" +
" Name: u.Name,\n" +
" _: {$value: u.Name, $name:'AnalyzedName'}\n" +
" };\n" +
" })"));
IndexFieldOptions indexFieldOptions = new IndexFieldOptions();
indexFieldOptions.setIndexing(FieldIndexing.SEARCH);
indexFieldOptions.setAnalyzer("StandardAnalyzer");
HashMap<String, IndexFieldOptions> fields = new HashMap<>();
fields.put("AnalyzedName", indexFieldOptions);
setFields(fields);
}
}
In general, you can sort using Raven.Database.Indexing.Collation.Cultures.<two-letters-culture-name>CollationAnalyzer
, and all the cultures supported by the .NET framework are supported.