Sorting
Tip: There are many examples of sorting in the FluentApiTests
source code to use as examples/reference.
Score
By default search results are ordered by Score descending so there's nothing specific that needs to be done to support this. If you use a different sorting operation then the Score
value will be 0 for all results.
Custom sorting
Any field that is a numerical or date based is automatically sortable. To make text based fields sortable you need to explicitly opt-in for that behavior. By default all fields are FieldDefinitionTypes.FullText
which are not sortable. To make a text field sortable it needs to be FieldDefinitionTypes.FullTextSortable
.
You cannot sort on both the score and a custom field.
Sorting is done by either the OrderBy
or OrderByDescending
methods using a SortableField
and a SortType
. The SortType
should typically match the field definition type (i.e. Int, Long, Double, etc...)
- For
FieldDefinitionTypes.FullTextSortable
useSortType.String
- For
FieldDefinitionTypes.DateTime
useSortType.Long
.
Example:
var searcher = indexer.GetSearcher();
var orderedResults = searcher
.CreateQuery("content")
.Field("writerName", "administrator")
.OrderBy(new SortableField("name", SortType.String))
.Execute();
var orderedDescendingResults = searcher
.CreateQuery("content")
.Field("writerName", "administrator")
.OrderByDescending(new SortableField("name", SortType.String))
.Execute();