title: How to search in ElasticSearch nested objects using NEST
body: <p>I'm trying to search my elasticsearch nested objects by using NEST c# client.
  My index name is <code>people</code> and my type is <code>person</code> in which the <code>car</code> field is <code>nested</code>.  </p>
  
  <p>This is my class:   </p>
  
  <pre><code>using Nest;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  
  namespace WebApplication5
  {
  public class person
  {
      public int id { get; set; }
      public String fname  { get; set; }
      public String mname { get; set; }
      public String lname { get; set; }
      public String houseno { get; set; }
      [ElasticProperty(Type = FieldType.Nested)]
      public IList&lt;NestedType&gt; car { get; set; } 
      public class NestedType
      {
          public String carname { get; set; }
          public int car_no { get; set; }
          public String color { get; set; }
      }
  }
  
  } 
  </code></pre>
  
  <p>Now my web application looks something like this:  </p>
  
  <pre><code>using Elasticsearch.Net;
  using Microsoft.AspNet.SignalR;
  using Microsoft.AspNet.SignalR.Configuration;
  using Nest;
  using System;
  using System.Collections.Generic;
  using System.Data;
  using System.Diagnostics;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  
  namespace WebApplication5
  {
  public partial class WebForm1 : System.Web.UI.Page
  {
      Stopwatch stopwatch = new Stopwatch();
      Uri node;
      ConnectionSettings settings;
      ElasticClient client;
      IList&lt;person&gt; list;
      protected void Page_Load(object sender, EventArgs e)
      {
          node = new Uri("http://localhost:9200");
          settings = new ConnectionSettings(node, defaultIndex:   "people");
          client = new ElasticClient(settings);
          list = new List&lt;person&gt;();
  
      }
  
      public IList&lt;person&gt; Search(ref long totalResult, int from,  int size, string searchKeyword)
      {
          list.Clear();
          stopwatch.Start();
  
          try
          {
  
              var result = client.Search&lt;person&gt; /*   */
  
  
    stopwatch.Start();
    totalResult = result.Total;
    list = result.Hits.Select(t =&gt; t.Source).ToList&lt;person&gt;();
    }
          catch (Exception ex)
          {
              string msg = ex.Message;
          }
   return 
              list;
      }
   protected void TextBox1_TextChanged(object sender, EventArgs e)
      {
          long totalResult = 0;
          IList&lt;person&gt; List = new List&lt;person&gt;();
          List = Search(ref totalResult, 0, 1000,TextBox1.Text);
          Label1.Text = "" + List.Count + " result(s)"+ " in "+ stopwatch.Elapsed.TotalSeconds+"seconds";
          GridView1.DataSource = List;
          GridView1.DataBind();
          List.Clear();
  
      }
    }
  }
  </code></pre>
  
  <p>Here <code>searchKeyword</code> is the the word that i need to search.<br>
  I enter it in a search box(webform) in the browser.<br>
  The searchKeyword may contain any value which needs to be matched to the value of any field in my table <code>person</code>.<br>
  If the searchKeyword matches with a nested document then the exact nested document should be returned.<br>
  I don't know there is something wrong with my nested query or i actually can't figure out how to use a nested query to do this. Everytime i run the application and search for a value which is actually present in the table,i get zero results.  Please help me figure out how to write the <code>query</code> to search in the nested fields and help me in checking if i have written the <code>class</code> correctly. 
  The query part is Marked as /*    */.
  Can anyone help me resolve this?</p>
  
  <p><strong>UPDATE</strong>
  This is my index mapping :</p>
  
  <pre><code>POST- people/person/_mapping
  {
  "person":{
  "properties":{
    "car": {
      "type": "nested"
      }
    }
   }
  }
  </code></pre>
  
  <p>And my records look like this:</p>
  
  <pre><code>POST-people/person
  {
  "id":1, 
  "fname":"aditi",  
  "mname":"ananya", 
  "lname":"xyz", 
  "houseno":"abc",
  "car":
   [
    {
     "carname":"fiat",
     "carno":1234,
     "color":"white"
    },
  
    {
     "carname":"maruti",
     "carno":5678,
     "color":"silver"
    }
   ]
  }
  
  
  POST-people/person
  {
  "id":2, 
  "fname":"robin",  
  "mname":"kumar", 
  "lname":"sharma", 
  "houseno":"efg",
  "car":
    [
     {
        "carname":"audi",
        "carno":4321,
        "color":"black"
     },
  
     {
        "carname":"honda",
        "carno":8765,
        "color":"red"
     },
  
     {
        "carname":"merecedez",
        "carno":0101,
        "color":"purple"
     }
   ]
  }
  </code></pre>
  
  <p>Which means i have 2 records.  </p>
  
  <p><strong>SECOND UPDATE</strong><br>
  I tried this query and it works fine. Though this is not my final query. </p>
  
  <pre><code>POST-people/person/_search
  {
  "_source":false,
  "query": {
  "filtered": {
    "query": {"match_all": {}},
    "filter": {
      "nested": {
        "path": "car",
        "query":{
          "filtered": {
            "query": { "match_all": {}},
            "filter": {
              "and": [
                {"term": {"car.carname":"mercedez"}},
                {"term": {"car.color":"purple"}}
              ]
            }
          }
        },
  "inner_hits":{}
      }
    }
   }
  }
  }
  </code></pre>
  
  <p><strong>THIRD UPDATE</strong><br>
  Ok, so now In this case my final query should be :</p>
  
  <pre><code>{"_source":false,
  "query": {
   "filtered": {
    "query": {"match_all": {}},
    "filter": {
      "nested": {
        "path": "car",
        "filter": {
          "term": {
            "car.carname": "audi"
          }
        }, 
        "inner_hits" : {}
      }
     }
    }
   }
  }
  </code></pre>
  
  <p>How do i write this query in .net?<br>
  Here,the "_source" and "inner_hits" are important for me because i want to return only the matching nested document and nothing else(i.e. i want to return only the matched nested document and not the other nested documents).<br>
  So, can you please help me write the corresponding search query for this?<br>
  Moreover, here i'm matching in the <code>car.carname</code> field, but i want my application should be able to match for all other sub-fields of the <code>car</code> field like <code>car.carno</code> and <code>car.color</code> and even for all the other top level fields like <code>id</code>, <code>fname</code> etc.  </p>
  
  <p><strong>FOURTH UPDATE</strong><br>
  Here i have written the search query in .net for my final query that i had mentioned my in <strong>THIRD UPDATE</strong> (please look into my third update).<br>
  Can you please check if it is correct?
  The corresponding final query in .net which i have written is:</p>
  
  <pre><code>(s =&gt; s
   .Source(false)
     .Query(query =&gt; query.Filtered(filtered =&gt; filtered
      .Query(q =&gt; q.MatchAll())
        .Filter(f =&gt; f.Nested(nf =&gt; nf
          .InnerHits()
          .Path(p =&gt; p.car)
          .Query(qq =&gt; qq.Match(m =&gt; m.OnField(g=&gt;g.car.First().carname).Query("audi"))))))));  
  </code></pre>
  
  <p>This is what i have written by studying your queries(Thanx a lot for that :) ).  Do check this and tell me if there is anything wrong. But yeah,i'm still unable to retrieve any results. </p>