This content is archived. See latest version here.

Last updated: Sep 26 2013

Introduction

Search results are by default sorted by relevance, given that a search query exists. By using the OrderBy, ThenBy, OrderByDescending and ThenByDescending methods it is possible to instead sort by a specific field.

Example:

C#
client.Search<Article>()
    .OrderBy(x => x.PublicationDate)
    .ThenBy(x => x.Author.Name)

Sorting is supported on numerical types such as int and double, DateTime and strings.

Null values and SortMissing

By default documents with null values in a field on which the search results are being sorted on will be sorted first when using the OrderBy and ThenBy methods and last when using the OrderByDescending and ThenByDescending methods. For all types except strings this can be changed by supplying a second parameter of the SortMissing enum type.

C#
client.Search<Article>()
    .OrderBy(x => x.PublicationDate, SortMissing.Last)

Sorting by geographical distance

The OrderBy and OrderByDescending methods both have an overload for ordering by geographical distance for fields of type GeoLocation. For more information see the topic for Geo Search.

Comments