title: NEST ElasticSearch c# how to Filter on Nested Object
body: <p>We have a "Contact" Type structure that contains a "Relationships" nested structure. I am trying to use the NEST ElasticSearch .Net client to search and get a count of the number of contacts a staff member has saved.  But cannot get the syntax down.</p>
  
  <p>Here is what I have for the structure of "Contact":</p>
  
  <pre><code>    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&lt;Relationship&gt; 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; }
          }
  
      }
  </code></pre>
  
  <p>Now what I am trying to use as the code to return results or just a count is the following (<em>Note I left the results going into a List more from trial an error as I'm still trying to figure out how to just get a count as well</em>):</p>
  
  <pre><code>    public int GetFriendCount(int staffID)
      {
          List&lt;Data&gt; list = new List&lt;Data&gt;();
  
          base.ConnectStatus = null;
  
          var results = this.Client.Search&lt;Data&gt;(s =&gt; s
              //.SearchType(Elasticsearch.Net.SearchType.Count)
              .Query(q =&gt; q.MatchAll())
              .Filter(f =&gt;
                      f.Nested(n =&gt; n
                      .Path("relationships")
                      .Filter(f2 =&gt; f2 .Term("relationships.staffID", staffID))
                      )
                  )
               //    .Nested
              );
  
          if (results != null)
          {
              base.ConnectStatus = results.ConnectionStatus;
  
              if (results.Documents.Count&lt;Data&gt;() &gt; 0)
                  list = results.Documents.ToList&lt;Data&gt;();
          }
  
          return list.Count;
      }
  </code></pre>