有没有一种方法可以在聚合中添加其他字段。 (也许使用了Top_Hit,但我无法对其进行管理以获取有效的示例)

对于以下示例,是否可以在聚合中获取“地址”?

    class Person
    {
        public string First_Name { get; set; }
        public string Last_Name { get; set; }
        public string Address { get; set; }
        public int Salary { get; set; }

        public Person(string first_name, string last_name, string address, int salary)
        {
            this.First_Name = first_name;
            this.Last_Name = last_name;
            this.Address = address;
            this.Salary = salary;
        }
        public Person() { }
    }
    private void button4_Click(object sender, EventArgs e)
    {
        var defaultIndex = "persons";

        var settings = new ConnectionSettings(new Uri("http://localhost:9200"))
            .InferMappingFor<Person>(i => i.IndexName(defaultIndex))
            .DefaultIndex(defaultIndex);

        client = new ElasticClient(settings);

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

        client.CreateIndex(defaultIndex, c => c
            .Settings(s => s
                .NumberOfShards(1)
            )
            .Mappings(m => m
                .Map<Person>(mm => mm
                    .AutoMap()
                )
            )
        );

        Person al = new Person("Al", "Bundy", "House A", 1000);
        Person bud = new Person("Bud", "Bundy", "House A", 500);
        Person peggy = new Person("Peggy", "Bundy", "House A", 0);
        Person kelly = new Person("Kelly", "Bundy", "House A", 100);
        Person marcy = new Person("Marcy", "Darcy", "House B", 4000);
        Person jefferson = new Person("Jefferson", "Darcy", "House B", 0);

        client.IndexMany(new[] { al, peggy, kelly, bud, marcy, jefferson });
        client.Refresh(defaultIndex);

        // query the index
        var result = client.Search<Person>(s => s
            .Aggregations(a => a
                .Terms("Families", ts => ts
                    .Field(o => o.Last_Name.Suffix("keyword")) // use the keyword sub-field for terms aggregation
                    .Size(100)
                    .Aggregations(aa => aa
                        .ValueCount("PersonCount", vc => vc
                        .Field(o => o.Salary)
                        )
                    )
                )
            )
            .Size(0)
        );


        var names = result.Aggs.Terms("Families");
        string retval = string.Empty;
        foreach (var name in names.Buckets)
        {
            var cnt = name.ValueCount("PersonCount");
            var address = "";    // how do I get the Address
            retval += "* family: '" + name.Key + "' living in: " + address + " Number of persons: " + cnt.Value + Environment.NewLine;
        }
        MessageBox.Show(retval);
    }

请帮助修改代码,这样我也可以获得每个家庭的地址。
额外信息:一个家庭的每个成员都住在同一个房子里。

最佳答案

我自己找到了解决方案。

    class Person
    {
        public string First_Name { get; set; }
        public string Last_Name { get; set; }
        public string Address { get; set; }
        public int Salary { get; set; }

        public Person(string first_name, string last_name, string address, int salary)
        {
            this.First_Name = first_name;
            this.Last_Name = last_name;
            this.Address = address;
            this.Salary = salary;
        }
        public Person() { }
    }
    private void button4_Click(object sender, EventArgs e)
    {
        var defaultIndex = "persons";

        var settings = new ConnectionSettings(new Uri("http://localhost:9200"))
            .InferMappingFor<Person>(i => i.IndexName(defaultIndex))
            .DefaultIndex(defaultIndex);

        client = new ElasticClient(settings);

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

        client.CreateIndex(defaultIndex, c => c
            .Settings(s => s
                .NumberOfShards(1)
            )
            .Mappings(m => m
                .Map<Person>(mm => mm
                    .AutoMap()
                )
            )
        );

        Person al = new Person("Al", "Bundy", "House A", 1000);
        Person bud = new Person("Bud", "Bundy", "House A", 500);
        Person peggy = new Person("Peggy", "Bundy", "House A", 0);
        Person kelly = new Person("Kelly", "Bundy", "House A", 100);
        Person marcy = new Person("Marcy", "Darcy", "House B", 4000);
        Person jefferson = new Person("Jefferson", "Darcy", "House B", 0);

        client.IndexMany(new[] { al, peggy, kelly, bud, marcy, jefferson });
        client.Refresh(defaultIndex);

        // query the index
        var result = client.Search<Person>(s => s
            .Aggregations(a => a
                .Terms("Families", ts => ts
                    .Field(o => o.Last_Name.Suffix("keyword")) // use the keyword sub-field for terms aggregation
                    .Size(100)
                    .Aggregations(abc => abc
                        .TopHits("ExtraFields", th => th
                        .Size(1)
                        )
                    )
                )
            )
            .Size(0)
        );


        var names = result.Aggs.Terms("Families");
        string retval = string.Empty;
        foreach (var name in names.Buckets)
        {
            var extraFields = name.TopHits("ExtraFields");
            var hits = extraFields.Hits<Person>();
            foreach (var hit in hits)
            {
                Person p = hit.Source;
                var address = p.Address;
                retval += "* family: '" + name.Key + "' living in: " + address + " Number of persons: " + extraFields.Total + Environment.NewLine;
            }
        }
        MessageBox.Show(retval);
    }

关于c# - ElasticSearch Nest Aggregation添加其他字段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47944164/

10-10 16:31