Search code examples
nest

Sorting on Multiple Fields


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?


Solution

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