title: Treat child as field of parent in elastic search query
body: <p>I am reading the docs for elasticsearch and this [page][1] talks about mapping a child to a parent type using <code>_parent</code>.</p>
  
  <p>If I have childs called <code>email</code> attached to parents called <code>account</code>:</p>
  
  <p>Fields in each type: </p>
  
  <pre><code>account (http://localhost:9200/myapp/account/1)
  ========
  id
  name
  some_other_info
  state
  
  email (http://localhost:9200/myapp/email/1?parent=1)
  ========
  id
  email
  </code></pre>
  
  <ul>
  <li><p>How can I search on the <code>name</code> field of <code>account</code> and the <code>email</code> field of <code>email</code> provided that the <code>state</code> of <code>account</code> is <code>active</code>?</p></li>
  <li><p>Is there a way to get all the children (of a certain type or of any type) a parent owns?</p></li>
  <li><p>When indexing a child document, is it possible to to pass the parent as an object property in the JSON data as opposed to it being part of the query string?</p></li>
  </ul>
  
  <hr>
  
  <p>After trying imotov's suggestion, I came up with this query:</p>
  
  <p>This is executed on <code>http://localhost:9200/myapp/account/_search</code></p>
  
  <pre><code>{
    "query": {
      "bool": {
        "must": [
          {
            "prefix": {
              "name": "a"
            }
          },
          {
            "term": {
              "statuses": "active"
            }
          }
        ],
        "should": [
          {
            "has_child": {
              "type": "emailaddress",
              "query": {
                "prefix": {
                  "email": "a"
                }
              }
            }
          }
        ]
      }
    }
  }
  </code></pre>
  
  <p>The problem is that the above does not give me any accounts where the email matches.</p>
  
  <p>The effect I want is essentially this:</p>
  
  <ul>
  <li>There is one search box</li>
  <li>Users start typing and the search box autocompletes.</li>
  <li>The user's query is checked against the name of the <code>account</code> or any of the <code>emailaddress</code> type.</li>
  <li>If <code>accounts</code> were matched, just return them. If <code>emailaddress</code> were match, return its parent account.</li>
  <li>Limit to a maximum of x (say 10) accounts for each search.</li>
  </ul>
  
  <p>So, I basically need to be able to <code>OR</code> the search between 2 types and return the parent type of matches.</p>
  
  <hr>
  
  <p>Test data:</p>
  
  <pre><code>curl -XPUT http://localhost:9200/test/account/1 -d '{
      "name": "John Smith",
      "statuses": "active"
  }'
  
  curl -XPUT http://localhost:9200/test/account/2 -d '{
      "name": "Peter Smith",
      "statuses": "active"
  }'
  
  curl -XPUT http://localhost:9200/test/account/3 -d '{
      "name": "Andy Smith",
      "statuses": "active"
  }'
  
  //Set up mapping for parent/child relationship
  
  curl -XPUT 'http://localhost:9200/test/email/_mapping' -d '{
      "emails" : {
          "_parent" : {"type" : "account"}
      }
  }'
  
  curl -XPUT http://localhost:9200/test/email/1?parent=1 -d '{
      "email": "john@smith.com"
  }'
  
  curl -XPUT http://localhost:9200/test/email/2?parent=1 -d '{
      "email": "admin@mycompany.com"
  }'
  
  curl -XPUT http://localhost:9200/test/email/3?parent=1 -d '{
      "email": "abcd@efg.com"
  }'
  
  curl -XPUT http://localhost:9200/test/email/4?parent=2 -d '{
      "email": "peter@peter.com"
  }'
  
  curl -XPUT http://localhost:9200/test/email/5?parent=3 -d '{
      "email": "andy@yahoo.com"
  }'
  
  curl -XPUT http://localhost:9200/test/email/6?parent=3 -d '{
      "email": "support@mycompany.com"
  }'
  </code></pre>
  
  <hr>
  
  <p>imotov's solution worked for me. Another solution I have found is to query <code>account</code>s for <code>status = active</code>, then run a <code>bool</code> filter on the result and use <code>has_child</code> on the child type and <code>prefix</code> on <code>name</code> inside the <code>bool</code> filter.</p>