第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

ElasticSearch NEST - GroupBy then OrderBy

ElasticSearch NEST - GroupBy then OrderBy

C#
烙印99 2022-08-20 16:51:36
我需要將 linq 查詢轉(zhuǎn)換為 NEST。這就是我的 linq 查詢的樣子:var result = studentList             .GroupBy(student => student.Name)             .Select(group => group.OrderByDescending(student => student.grade).Take(3))             .SelectMany(p => p);它應(yīng)該:按學(xué)生姓名分組按年級(jí)對(duì)每個(gè)組進(jìn)行個(gè)人排序,降序從每個(gè)小組中選出成績(jī)最好的前X名學(xué)生它正在與linq一起工作,但是如何使用NEST做到這一點(diǎn)?我嘗試聚合,子聚合,排序桶,但沒(méi)有成功。我知道如何按名稱分組,但接下來(lái)呢?client.Search<Students>(s => s .Aggregations(a => a     .Terms("group_by_name", ts => ts         .Field(o => o.Name))));如何對(duì)每個(gè)小組進(jìn)行排序,如何從每個(gè)小組的頂尖學(xué)生中取出?
查看完整描述

1 回答

?
侃侃爾雅

TA貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超16個(gè)贊

假設(shè)模型像


public class Student 

{

    public int Id { get; set; }

    public string Name { get; set; }

    public int Grade { get; set; }

}

以下內(nèi)容將按學(xué)生姓名分組,然后按年級(jí)降序排列每個(gè)組中的前 x 個(gè)匹配項(xiàng)


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

    }

}

它打印出來(lái)


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


查看完整回答
反對(duì) 回復(fù) 2022-08-20
  • 1 回答
  • 0 關(guān)注
  • 136 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)