简体   繁体   中英

ElasticSearch NEST - GroupBy then OrderBy

I need to convert linq query to NEST. Thats how my linq query looks like:

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

It should:

  1. Group by students names
  2. Order each group individualy by grade, descending
  3. From each group take top X students with best grades

It's working with linq but how to do this with NEST? I was trying aggregations, subagregations, sorting buckets but without success.

I know how to group by names but whats next?

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

How to order each group, how to take from each group top students?

Assuming a model like

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

The following will group by student names, then get the top x hits in each group ordered by grade descending

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

which prints out

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM