<p dir="auto">ES 1.4 added support for Children aggregation, which is a really great feature.<br>
The next step would be to also support a Parent aggregation</p>
<p dir="auto">Here is a quick example of a typical use case:<br>
Let's say we've got 2 types, one containing Product information (name, description, categories, size, weight, etc) and another one containing Reseller information (location, price, etc..).<br>
There is a parent child relationship between Products and Resellers (Product being the parent)</p>
<p dir="auto">In some cases, one may want to aggregate on Parent property.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="GET parentchild/reseller/_search/
{
  &quot;query&quot;: {
    ... // not relevant here
  },
  &quot;sort&quot;: [
    {
      &quot;price&quot;: {
        &quot;order&quot;: &quot;asc&quot;,
        &quot;mode&quot;: &quot;avg&quot;
      }
    }
  ],
  &quot;aggs&quot;: {
    &quot;location&quot;: {
      &quot;geo_distance&quot;: {
        &quot;unit&quot;: &quot;km&quot;,
        &quot;field&quot;: &quot;location&quot;,
        &quot;origin&quot;: &quot;lat, lon&quot;,
        &quot;ranges&quot;: [
          { &quot;from&quot;: 0, &quot;to&quot;: 100},
          ....
        ]
      }
    },
    &quot;price&quot;: {
      &quot;range&quot;: {
        &quot;field&quot;: &quot;price&quot;,
        &quot;ranges&quot;: [
          { &quot;from&quot;: 0, &quot;to&quot;: 50},
          ...
        ]
      }
    },
    &quot;aggregateOnParent&quot;: {
      &quot;parent&quot;: {},
      &quot;aggs&quot;: {
          &quot;category&quot;: {
            &quot;term&quot;: {
               &quot;field&quot;: &quot;category&quot; // Note that category is a field of Product (the parent)
            }
          }
        }
      }
    }
  }
}"><pre class="notranslate"><code class="notranslate">GET parentchild/reseller/_search/
{
  "query": {
    ... // not relevant here
  },
  "sort": [
    {
      "price": {
        "order": "asc",
        "mode": "avg"
      }
    }
  ],
  "aggs": {
    "location": {
      "geo_distance": {
        "unit": "km",
        "field": "location",
        "origin": "lat, lon",
        "ranges": [
          { "from": 0, "to": 100},
          ....
        ]
      }
    },
    "price": {
      "range": {
        "field": "price",
        "ranges": [
          { "from": 0, "to": 50},
          ...
        ]
      }
    },
    "aggregateOnParent": {
      "parent": {},
      "aggs": {
          "category": {
            "term": {
               "field": "category" // Note that category is a field of Product (the parent)
            }
          }
        }
      }
    }
  }
}
</code></pre></div>
<p dir="auto">I understand that issue <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="57015066" data-permission-text="Title is private" data-url="https://github.com/elastic/elasticsearch/issues/9611" data-hovercard-type="issue" data-hovercard-url="/elastic/elasticsearch/issues/9611/hovercard" href="https://github.com/elastic/elasticsearch/issues/9611">#9611</a> was closed regarding this:</p>
<blockquote>
<p dir="auto">Nested fields need to be queries with nested queries/filters, because multiple documents can match and you need to be able to specify how these multiple scores should be reduced to a single score.<br>
— -- <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/clintongormley/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/clintongormley">@clintongormley</a></p>
</blockquote>
<h1 dir="auto">Proposal</h1>
<p dir="auto">I propose that, when a field name within a query string query is parsed, and it does not match a field mapping, an attempt should be made to match the field name to a nested object mapper. If the attempt is successful, the query text for that field name should then be parsed as a query string query using the same settings as the root level query string query. The resulting query from that parsing will in turn be used to create a ToParentBlockJoinQuery (a nested query) that uses the same default scoring mode that would be applied when manually submitting a nested query ("avg".)</p>
<h1 dir="auto">The syntax</h1>
<p dir="auto">The acceptable syntax for a nested query within a query string query is similar to this:</p>
<p dir="auto"><code class="notranslate">nestedPath:"&lt;query string query&gt;"</code></p>
<p dir="auto">This means that any constructs you would use in a query string query are valid:</p>
<p dir="auto"><code class="notranslate">children:"children.first:peggy"</code><br>
<code class="notranslate">children:"children.first:\"peggy\""</code><br>
<code class="notranslate">children:"children.first:(peggy ruby)"</code><br>
<code class="notranslate">children:"children.first:peggy AND children.last:sue"</code><br>
<code class="notranslate">children:"children.first:pegyg~ +children.last:su?"</code></p>
<p dir="auto">Note that the nested query MUST be surrounded with quotes. I wanted it to be parentheses instead but unfortunately the Lucene QueryParser class does not recognize the field names the way I wanted it to (<code class="notranslate">children:(children.first:peggy)</code> would come out as a TermQuery on <code class="notranslate">children.first</code>, the <code class="notranslate">children</code> field name would be discarded.)</p>
<h1 dir="auto">Other considerations</h1>
<ul dir="auto">
<li>Support for specifying scoring modes within the query string query settings based on nested object paths is a possibility.</li>
<li>Support for inner hits may also be a possibility, in a similar fashion to scoring modes.</li>
</ul>
<p dir="auto">Support for nested queries in query strings at all would be an enhancement, but these options could provide additional enhancements. Example of how they may look:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="{
    &quot;query_string&quot; : {
        &quot;query&quot; : &quot;children:\&quot;children.first:peggy\&quot;&quot;,
        &quot;nested&quot;: [
            {
                &quot;path&quot;: &quot;children&quot;,
                &quot;score_mode&quot;: &quot;max&quot;,
                &quot;inner_hits&quot;: {
                    &lt;inner_hits_options&gt;
                }
            }
        ]
    }
}"><pre class="notranslate"><code class="notranslate">{
    "query_string" : {
        "query" : "children:\"children.first:peggy\"",
        "nested": [
            {
                "path": "children",
                "score_mode": "max",
                "inner_hits": {
                    &lt;inner_hits_options&gt;
                }
            }
        ]
    }
}
</code></pre></div>
<h1 dir="auto">Pull request</h1>
<p dir="auto">For the basic functionality, I have already made the necessary modifications (three changed files, one changed test file to add a test with several assertions) on the 'master' branch of my local clone of the repository. I would like to submit a pull request; please advise as to how you would like that to be done (if I need to rebase onto another branch, etc.)</p>