title: Querying Multi Level Nested fields on Elastic Search
body: <p>I'm new to Elastic Search and to the non-SQL paradigm.
  I've been following ES tutorial, but there is one thing I couldn't put to work.</p>
  
  <p>In the following code (I'me using <a href="http://packages.python.org/pyes/" rel="nofollow noreferrer">PyES</a> to interact with ES) I create a single document, with a nested field (subjects), that contains another nested field (concepts).</p>
  
  <pre><code>from pyes import *
  
  conn = ES('127.0.0.1:9200')  # Use HTTP
  
  # Delete and Create a new index.
  conn.indices.delete_index("documents-index")
  conn.create_index("documents-index")
  
  # Create a single document.
  document = {
      "docid": 123456789,
      "title": "This is the doc title.",
      "description": "This is the doc description.",
      "datepublished": 2005,
      "author": ["Joe", "John", "Charles"],
      "subjects": [{
                      "subjectname": 'subject1',
                      "subjectid": [210, 311, 1012, 784, 568],
                      "subjectkey": 2,
                      "concepts": [
                                      {"name": "concept1", "score": 75},
                                      {"name": "concept2", "score": 55}
                                    ]
                  },
                  {
                      "subjectname": 'subject2',
                      "subjectid": [111, 300, 141, 457, 748],
                      "subjectkey": 0,
                      "concepts": [
                                      {"name": "concept3", "score": 88},
                                      {"name": "concept4", "score": 55},
                                      {"name": "concept5", "score": 66}
                                    ]
                  }],
      }
  
  
  # Define the nested elements.
  mapping1 = {
              'subjects': {
                  'type': 'nested'
              }
          }
  mapping2 = {
              'concepts': {
                  'type': 'nested'
              }
          }
  conn.put_mapping("document", {'properties': mapping1}, ["documents-index"])
  conn.put_mapping("subjects", {'properties': mapping2}, ["documents-index"])
  
  
  # Insert document in 'documents-index' index.
  conn.index(document, "documents-index", "document", 1)
  
  # Refresh connection to make queries.
  conn.refresh()
  </code></pre>
  
  <p>I'm able to query <em>subjects</em> nested field:</p>
  
  <pre><code>query1 = {
      "nested": {
          "path": "subjects",
          "score_mode": "avg",
          "query": {
              "bool": {
                  "must": [
                      {
                          "text": {"subjects.subjectname": "subject1"}
                      },
                      {
                          "range": {"subjects.subjectkey": {"gt": 1}}
                      }
                  ]
              }
          }
      }
  }
  
  
  results = conn.search(query=query1)
  for r in results:
      print r  # as expected, it returns the entire document.
  </code></pre>
  
  <p>but I can't figure out how to query based on <em>concepts</em> nested field.</p>
  
  <p>ES <a href="http://www.elasticsearch.org/guide/reference/query-dsl/nested-query.html" rel="nofollow noreferrer">documentation</a> refers that </p>
  
  <blockquote>
    <p>Multi level nesting is automatically supported, and detected,
    resulting in an inner nested query to automatically match the relevant
    nesting level (and not root) if it exists within another nested query.</p>
  </blockquote>
  
  <p>So, I tryed to build a query with the following format:</p>
  
  <pre><code>query2 = {
          "nested": {
              "path": "concepts",
              "score_mode": "avg",
              "query": {
                  "bool": {
                      "must": [
                          {
                              "text": {"concepts.name": "concept1"}
                          },
                          {
                             "range": {"concepts.score": {"gt": 0}}
                          }
                      ]
                  }
              }
          }
  }
  </code></pre>
  
  <p>which returned 0 results.</p>
  
  <p>I can't figure out what is missing and I haven't found any example with queries based on two levels of nesting.</p>