2017-12-22 5 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); 
    } 

코드를 수정하면 각 가족의 주소도 알 수 있도록 도움주세요.
추가 정보 : 한 가족의 각 구성원은 같은 집에 거주합니다.

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