Sorting on Multiple Fields

岁酱吖の 提交于 2019-11-30 04:44:16

问题


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?


回答1:


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



回答2:


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



回答3:


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



回答4:


searchDescriptor
    .Sort(s => s
          .Ascending(a => a.OrgId)
          .Descending(d => d.CreateTimeInUtc)
    );



回答5:


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


来源:https://stackoverflow.com/questions/25894560/sorting-on-multiple-fields

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!