Does Nest support sorting on multiple fields? For example, say I want to sort first by FieldA ascending and then by FieldB descending.
My current approach looks some
You are adding multiple fields on the same sort descriptor, which is overriding the previous value. Instead, you need to specify a new sort descriptor for each field:
searchDescriptor
.Sort(s => s
.OnField("FieldA")
.Ascending()
)
.Sort(s => s
.OnField("FieldB")
.Descending()
)
for sorting on multiple dynamic fields, SortDescriptor can be useful.
var sortDescriptor = new SortDescriptor<{YourType}>();
sortDescriptor.Field(f => f.YourFieldName, Nest.SortOrder.Ascending);
// If Field Name is Dynamic
sortDescriptor.Field(
"someOtherFieldName",
Nest.SortOrder.Descending);
var searchResponse = await client.SearchAsync<{YourType}>(x => x
.Query(y => query)
.Sort(s => sortDescriptor)
.From(from)
.Size(size)
);
You can use it like this. You also can use the names from your model.
EsClient.Search<Business>(s => s
.Query (qq=> {...})
.Sort(sort => sort.OnField(s => s.Name).Descending())
.Sort(sort => sort.OnField(s => s.Age).Descending())
)
I recent version of Nest the correct way of sortin on multiple fileds would be:
.Sort(s => s
Field(f => f
.Field("FieldA")
.Order(SortOrder.Ascending)
)
.Field(f => f
.Field("FieldB")
.Order(SortOrder.Ascending)
)
);
searchDescriptor
.Sort(s => s
.Ascending(a => a.OrgId)
.Descending(d => d.CreateTimeInUtc)
);