2017-12-22 16 views
0

集計にフィールドを追加する方法はありますか。 (たぶん、Top_Hitを持っているかもしれませんが、実際の例を得るためにそれを管理することはできませんでした)ElasticSearchネスト集計追加のフィールドを追加

次の例では、集約で「アドレス」も取得できますか?

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

コードを変更すると、各家族の住所もわかります。
補足情報:1家族の各メンバーが同じ家に住んでいます。

+0

これまでに何を試みましたか? –

答えて

0

私自身が解決策を見つけました。

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); 
    } 
関連する問題