2015-03-02 90 views
1

我們有一個「聯繫」類型結構,其中包含「關係」嵌套結構。我正在嘗試使用NEST ElasticSearch .Net客戶端來搜索並獲得工作人員保存的聯繫人數量。但無法獲得語法。NEST ElasticSearch c#如何過濾嵌套對象

這裏是我有「聯繫」的結構:

public class FieldName : FieldNameBase 
    { 
     public const string IndexTypeName = "contact"; 

     public const string Id = @"Id"; 
     public const string Name = @"name"; 
     public const string Status = @"status"; 
     public const string FirstName = @"firstName"; 
     public const string LastName = @"lastName"; 
     public const string Email = @"email"; 
     public const string AvatarUrl = @"avatarUrl"; 

     public class Relationships 
     { 
      public const string StaffID = @"staffID"; 
      public const string ContactID = @"contactID"; 
      public const string FacebookUid = @"facebookUid"; 
      public const string Name = @"name"; 
      public const string Email = @"email"; 
      public const string Level = @"level"; 
      public const string AvatarUrl = @"avatarUrl"; 
     } 
    } 

    [ElasticType(Name = FieldName.IndexTypeName, IdProperty = FieldName.Id)] 
    public class Data : AccountData 
    { 
     [ElasticProperty(Name = FieldNameBase.Id, IncludeInAll = false, Index = FieldIndexOption.NotAnalyzed)] 
     public string Id { get; set; } 

     [ElasticProperty(Name = FieldNameBase.AccountID)] 
     public int AccountID { get; set; } 

     [ElasticProperty(Name = FieldName.FirstName, IncludeInAll = false, Index = FieldIndexOption.Analyzed)] 
     public string FirstName { get; set; } 

     [ElasticProperty(Name = FieldName.LastName, IncludeInAll = false, Index = FieldIndexOption.Analyzed)] 
     public string LastName { get; set; } 

     [ElasticProperty(Name = FieldName.Name, IncludeInAll = true, Index = FieldIndexOption.Analyzed)] 
     public string Name { get; set; } 

     [ElasticProperty(Name = FieldName.AvatarUrl, IncludeInAll = true, Index = FieldIndexOption.NotAnalyzed)] 
     public string AvatarUrl { get; set; } 

     [ElasticProperty(Name = FieldName.Email, IncludeInAll = true, Index = FieldIndexOption.NotAnalyzed)] 
     public string Email { get; set; } 

     [ElasticProperty(Name = FieldName.Phone, IncludeInAll = false, Index = FieldIndexOption.NotAnalyzed)] 
     public string Phone { get; set; } 

     [ElasticProperty(Type = FieldType.Nested)] 
     public List<Relationship> Relationships { get; set; } 

     public class Relationship 
     { 
      [ElasticProperty(Name = FieldName.Relationships.StaffID)] 
      public int? StaffID { get; set; } 

      [ElasticProperty(Name = FieldName.Relationships.ContactID, IncludeInAll=false, Index = FieldIndexOption.NotAnalyzed)] 
      public string ContactID { get; set; } 

      [ElasticProperty(Name = FieldName.Relationships.FacebookUid, IncludeInAll = false, Index = FieldIndexOption.NotAnalyzed)] 
      public string FacebookUid { get; set; } 

      [ElasticProperty(Name = FieldName.Relationships.Name, IncludeInAll = false, Index = FieldIndexOption.Analyzed)] 
      public string Name { get; set; } 

      [ElasticProperty(Name = FieldName.Relationships.Email, IncludeInAll = false, Index = FieldIndexOption.NotAnalyzed)] 
      public string Email { get; set; } 

      [ElasticProperty(Name = FieldName.Relationships.Level, IncludeInAll = false, Index = FieldIndexOption.Analyzed)] 
      public string Level { get; set; } 

      [ElasticProperty(Name = FieldName.Relationships.AvatarUrl, IncludeInAll = false, Index = FieldIndexOption.NotAnalyzed)] 
      public string AvatarUrl { get; set; } 
     } 

    } 

現在我想做的代碼使用返回的結果,或只是一個計數以下(注我離開結果進入一個列表更從審判我仍然試圖找出如何只得到計數以及)錯誤:

public int GetFriendCount(int staffID) 
    { 
     List<Data> list = new List<Data>(); 

     base.ConnectStatus = null; 

     var results = this.Client.Search<Data>(s => s 
      //.SearchType(Elasticsearch.Net.SearchType.Count) 
      .Query(q => q.MatchAll()) 
      .Filter(f => 
        f.Nested(n => n 
        .Path("relationships") 
        .Filter(f2 => f2 .Term("relationships.staffID", staffID)) 
        ) 
       ) 
      // .Nested 
      ); 

     if (results != null) 
     { 
      base.ConnectStatus = results.ConnectionStatus; 

      if (results.Documents.Count<Data>() > 0) 
       list = results.Documents.ToList<Data>(); 
     } 

     return list.Count; 
    } 

回答

2
 var results = this.Client.Search<Data>(s => s 
      .Query(q => q 
       .Nested(n => n 
        .Path("relationships") 
        .Filter(f => f.Term("relationships.staffID", staffID)) 
       ) 
      ) 
     ); 
相關問題