Sorting on Multiple Fields

user3749920 picture user3749920 · Sep 17, 2014 · Viewed 7.9k times · Source

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 something like this:

searchDescriptor.Sort(s =>s.OnField("FieldA").Ascending().OnField("FieldB").Descending());

But the "FieldB".Descending() part seems to be the only sort option that is sent to elasticsearch.

Does anyone know if there is another way to accomplish this?

Answer

Greg Marzouka picture Greg Marzouka · Sep 17, 2014

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()
    )