ElasticSearch NEST-GroupBy然后OrderBy

时间:2019-02-02 08:56:24

标签: c# .net linq elasticsearch nest

我要LINQ查询转换为NEST。那怎么我的LINQ查询如下:

var result = studentList
            .GroupBy(student => student.Name)
            .Select(group => group.OrderByDescending(student => student.grade).Take(3))
            .SelectMany(p => p);

它应该:

  1. 按学生姓名分组
  2. 按等级,降序分别对每个组进行排序
  3. 从每个组中选出成绩最好的前X名学生

它与linq一起工作,但是如何与NEST一起使用呢? 我试图聚合,subagregations,分拣水桶但没有成功。

我知道如何按名称分组,但是接下来呢?

client.Search<Students>(s => s
.Aggregations(a => a
    .Terms("group_by_name", ts => ts
        .Field(o => o.Name))));

如何订购每个小组,如何从每个小组的顶尖学生中接生?

1 个答案:

答案 0 :(得分:1)

假设一个模型

public class Student 
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Grade { get; set; }
}

以下内容将按学生姓名分组,然后按成绩降序获得各组中的热门x命中

private static void Main()
{
    var defaultIndex = "students";
    var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

    var settings = new ConnectionSettings(pool)
        .DefaultIndex(defaultIndex);

    var client = new ElasticClient(settings);

    if (client.IndexExists(defaultIndex).Exists)
        client.DeleteIndex(defaultIndex);

    var createIndexResponse = client.CreateIndex(defaultIndex, c => c
        .Settings(s => s
            .NumberOfShards(1)
            .NumberOfReplicas(0)
        )
        .Mappings(m => m
            .Map<Student>(mm => mm
                .AutoMap()
            )
        )
    );

    var students = Enumerable.Range(1, 20).Select(i =>
        new Student 
        {
            Id = i,
            Name = i % 2 == 0 ? "Foo" : "Bar",
            Grade = i
        }
    );

    var bulkResponse = client.Bulk(b => b
        .IndexMany(students)
        .Refresh(Refresh.WaitFor) // refresh, so that documents indexed are available to search immediately
    );

    var topX = 10;

    var searchResponse = client.Search<Student>(s => s
        .Aggregations(a => a
            .Terms("student_name", t => t
                .Field(f => f.Name.Suffix("keyword"))
                .Aggregations(aa => aa
                    .TopHits("top_grades", th => th
                        .Sort(so => so
                            .Descending(f => f.Grade)
                        )
                        .Size(topX)
                    )
                )
            )
        )
    );

    var studentNames = searchResponse.Aggregations.Terms("student_name");

    foreach(var bucket in studentNames.Buckets)
    {
        var header = $"Student Name: {bucket.Key}";
        Console.WriteLine(header);
        Console.WriteLine(new string('-', header.Length));
        foreach(var hit in bucket.TopHits("top_grades").Documents<Student>())
        {
            Console.WriteLine($"Id: {hit.Id}, Grade: {hit.Grade}");
        }
        Console.WriteLine();
    }
}

打印出来的

Student Name: Bar
-----------------
Id: 19, Grade: 19
Id: 17, Grade: 17
Id: 15, Grade: 15
Id: 13, Grade: 13
Id: 11, Grade: 11
Id: 9, Grade: 9
Id: 7, Grade: 7
Id: 5, Grade: 5
Id: 3, Grade: 3
Id: 1, Grade: 1

Student Name: Foo
-----------------
Id: 20, Grade: 20
Id: 18, Grade: 18
Id: 16, Grade: 16
Id: 14, Grade: 14
Id: 12, Grade: 12
Id: 10, Grade: 10
Id: 8, Grade: 8
Id: 6, Grade: 6
Id: 4, Grade: 4
Id: 2, Grade: 2