Sorting on Multiple Fields

前端 未结 5 1490
执笔经年
执笔经年 2021-01-04 19:31

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

相关标签:
5条回答
  • 2021-01-04 19:36

    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()
        )
    
    0 讨论(0)
  • 2021-01-04 19:36

    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)
            );
    
    0 讨论(0)
  • 2021-01-04 19:45

    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())
        )
    
    0 讨论(0)
  • 2021-01-04 19:49

    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)
        )
    );
    
    0 讨论(0)
  • 2021-01-04 19:59
    searchDescriptor
        .Sort(s => s
              .Ascending(a => a.OrgId)
              .Descending(d => d.CreateTimeInUtc)
        );
    
    0 讨论(0)
提交回复
热议问题