<p dir="auto">With a multi-level mapping of parent/child relationships on an index, searching with a <code class="notranslate">terms</code> aggregation by <code class="notranslate">_parent</code> on the "middle" child document type (mapped as a child of one type and a parent of another) yields "duplicate" buckets with bogus values for the <code class="notranslate">_parent</code> keys.</p>
<p dir="auto">Here is an example, with document types <code class="notranslate">my_parent</code>, <code class="notranslate">my_child</code>, and <code class="notranslate">my_grandchild</code>.  The query attempts to find, for a given list of parent IDs, the counts of children of those parents, aggregated by <code class="notranslate">color</code>.  When <code class="notranslate">my_grandchild</code> is not part of the mapping, the correct expected result buckets are returned.  When <code class="notranslate">my_grandchild</code> <em>is</em> included in the mapping, the "duplicate" buckets with bogus <code class="notranslate">_parent</code> keys are also included.  The bogus bucket <code class="notranslate">_parent</code> keys are the <code class="notranslate">id</code>s of the <code class="notranslate">my_child</code> documents, though there are no <code class="notranslate">my_grandchild</code> documents, and the <code class="notranslate">my_grandchild</code> document type was not targetted by the query, and would not have matched the <code class="notranslate">_parent</code> values given in the query.</p>
<p dir="auto">To reproduce, run the following script using the <code class="notranslate">WITH_GRANDCHILD</code> environment variable to dictate whether the <code class="notranslate">my_grandchild</code> mapping should be included or not.  For example, run <code class="notranslate">env WITH_GRANDCHILD=true bash script.sh</code> to include the <code class="notranslate">my_grandchild</code> mapping:</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="#!/usr/bin/env bash

ES_HOST=&quot;${ES_HOST:-http://localhost:9200}&quot;
WITH_GRANDCHILD=&quot;${WITH_GRANDCHILD:-false}&quot;

echo &quot;env: ES_HOST=${ES_HOST}&quot;
echo &quot;     WITH_GRANDCHILD=${WITH_GRANDCHILD}&quot;

function es() {
  echo
  echo &quot;es ${1} ${2} ${3}&quot;
  curl -X&quot;${1}&quot; &quot;${ES_HOST}/${2}&quot; -d &quot;${3}&quot;
  echo
}

es DELETE my_index

if [[ X&quot;${WITH_GRANDCHILD}&quot; == X&quot;false&quot; ]]; then
  es PUT my_index '{
    &quot;mappings&quot;: {
      &quot;my_parent&quot;:     {},
      &quot;my_child&quot;:      { &quot;_parent&quot;: { &quot;type&quot;: &quot;my_parent&quot; } }
    }
  }'
else
  es PUT my_index '{
    &quot;mappings&quot;: {
      &quot;my_parent&quot;:     {},
      &quot;my_child&quot;:      { &quot;_parent&quot;: { &quot;type&quot;: &quot;my_parent&quot; } },
      &quot;my_grandchild&quot;: { &quot;_parent&quot;: { &quot;type&quot;: &quot;my_child&quot;  } }
    }
  }'
fi

echo
echo &quot;---&quot;

es PUT 'my_index/my_parent/P1' '{ &quot;text&quot;: &quot;first parent&quot; }'
es PUT 'my_index/my_parent/P2' '{ &quot;text&quot;: &quot;second parent&quot; }'

es PUT 'my_index/my_child/P1_C1?parent=P1' '{ &quot;color&quot;: &quot;red&quot; }'
es PUT 'my_index/my_child/P1_C2?parent=P1' '{ &quot;color&quot;: &quot;yellow&quot; }'
es PUT 'my_index/my_child/P1_C3?parent=P1' '{ &quot;color&quot;: &quot;yellow&quot; }'
es PUT 'my_index/my_child/P2_C1?parent=P2' '{ &quot;color&quot;: &quot;green&quot; }'
es PUT 'my_index/my_child/P2_C2?parent=P2' '{ &quot;color&quot;: &quot;green&quot; }'
es PUT 'my_index/my_child/P3_C1?parent=P3' '{ &quot;color&quot;: &quot;blue&quot; }'

es POST 'my_index/_refresh'

echo
echo &quot;---&quot;

es GET 'my_index/my_child/_search?search_type=count&amp;format=yaml' '{
  &quot;query&quot;: {
    &quot;terms&quot;: {
      &quot;_parent&quot;: [ &quot;P1&quot;, &quot;P2&quot; ]
    }
  },
  &quot;aggs&quot;: {
    &quot;parents&quot;: {
      &quot;terms&quot;: { &quot;field&quot;: &quot;_parent&quot; },
      &quot;aggs&quot;: {
        &quot;colors&quot;: {
          &quot;terms&quot;: { &quot;field&quot;: &quot;color&quot; }
        }
      }
    }
  }
}'"><pre class="notranslate"><span class="pl-c"><span class="pl-c">#!</span>/usr/bin/env bash</span>

ES_HOST=<span class="pl-s"><span class="pl-pds">"</span><span class="pl-smi">${ES_HOST<span class="pl-k">:-</span>http<span class="pl-k">://</span>localhost<span class="pl-k">:</span>9200}</span><span class="pl-pds">"</span></span>
WITH_GRANDCHILD=<span class="pl-s"><span class="pl-pds">"</span><span class="pl-smi">${WITH_GRANDCHILD<span class="pl-k">:-</span>false}</span><span class="pl-pds">"</span></span>

<span class="pl-c1">echo</span> <span class="pl-s"><span class="pl-pds">"</span>env: ES_HOST=<span class="pl-smi">${ES_HOST}</span><span class="pl-pds">"</span></span>
<span class="pl-c1">echo</span> <span class="pl-s"><span class="pl-pds">"</span>     WITH_GRANDCHILD=<span class="pl-smi">${WITH_GRANDCHILD}</span><span class="pl-pds">"</span></span>

<span class="pl-k">function</span> <span class="pl-en">es()</span> {
  <span class="pl-c1">echo</span>
  <span class="pl-c1">echo</span> <span class="pl-s"><span class="pl-pds">"</span>es <span class="pl-smi">${1}</span> <span class="pl-smi">${2}</span> <span class="pl-smi">${3}</span><span class="pl-pds">"</span></span>
  curl -X<span class="pl-s"><span class="pl-pds">"</span><span class="pl-smi">${1}</span><span class="pl-pds">"</span></span> <span class="pl-s"><span class="pl-pds">"</span><span class="pl-smi">${ES_HOST}</span>/<span class="pl-smi">${2}</span><span class="pl-pds">"</span></span> -d <span class="pl-s"><span class="pl-pds">"</span><span class="pl-smi">${3}</span><span class="pl-pds">"</span></span>
  <span class="pl-c1">echo</span>
}

es DELETE my_index

<span class="pl-k">if</span> [[ X<span class="pl-s"><span class="pl-pds">"</span><span class="pl-smi">${WITH_GRANDCHILD}</span><span class="pl-pds">"</span></span> <span class="pl-k">==</span> X<span class="pl-s"><span class="pl-pds">"</span>false<span class="pl-pds">"</span></span> ]]<span class="pl-k">;</span> <span class="pl-k">then</span>
  es PUT my_index <span class="pl-s"><span class="pl-pds">'</span>{</span>
<span class="pl-s">    "mappings": {</span>
<span class="pl-s">      "my_parent":     {},</span>
<span class="pl-s">      "my_child":      { "_parent": { "type": "my_parent" } }</span>
<span class="pl-s">    }</span>
<span class="pl-s">  }<span class="pl-pds">'</span></span>
<span class="pl-k">else</span>
  es PUT my_index <span class="pl-s"><span class="pl-pds">'</span>{</span>
<span class="pl-s">    "mappings": {</span>
<span class="pl-s">      "my_parent":     {},</span>
<span class="pl-s">      "my_child":      { "_parent": { "type": "my_parent" } },</span>
<span class="pl-s">      "my_grandchild": { "_parent": { "type": "my_child"  } }</span>
<span class="pl-s">    }</span>
<span class="pl-s">  }<span class="pl-pds">'</span></span>
<span class="pl-k">fi</span>

<span class="pl-c1">echo</span>
<span class="pl-c1">echo</span> <span class="pl-s"><span class="pl-pds">"</span>---<span class="pl-pds">"</span></span>

es PUT <span class="pl-s"><span class="pl-pds">'</span>my_index/my_parent/P1<span class="pl-pds">'</span></span> <span class="pl-s"><span class="pl-pds">'</span>{ "text": "first parent" }<span class="pl-pds">'</span></span>
es PUT <span class="pl-s"><span class="pl-pds">'</span>my_index/my_parent/P2<span class="pl-pds">'</span></span> <span class="pl-s"><span class="pl-pds">'</span>{ "text": "second parent" }<span class="pl-pds">'</span></span>

es PUT <span class="pl-s"><span class="pl-pds">'</span>my_index/my_child/P1_C1?parent=P1<span class="pl-pds">'</span></span> <span class="pl-s"><span class="pl-pds">'</span>{ "color": "red" }<span class="pl-pds">'</span></span>
es PUT <span class="pl-s"><span class="pl-pds">'</span>my_index/my_child/P1_C2?parent=P1<span class="pl-pds">'</span></span> <span class="pl-s"><span class="pl-pds">'</span>{ "color": "yellow" }<span class="pl-pds">'</span></span>
es PUT <span class="pl-s"><span class="pl-pds">'</span>my_index/my_child/P1_C3?parent=P1<span class="pl-pds">'</span></span> <span class="pl-s"><span class="pl-pds">'</span>{ "color": "yellow" }<span class="pl-pds">'</span></span>
es PUT <span class="pl-s"><span class="pl-pds">'</span>my_index/my_child/P2_C1?parent=P2<span class="pl-pds">'</span></span> <span class="pl-s"><span class="pl-pds">'</span>{ "color": "green" }<span class="pl-pds">'</span></span>
es PUT <span class="pl-s"><span class="pl-pds">'</span>my_index/my_child/P2_C2?parent=P2<span class="pl-pds">'</span></span> <span class="pl-s"><span class="pl-pds">'</span>{ "color": "green" }<span class="pl-pds">'</span></span>
es PUT <span class="pl-s"><span class="pl-pds">'</span>my_index/my_child/P3_C1?parent=P3<span class="pl-pds">'</span></span> <span class="pl-s"><span class="pl-pds">'</span>{ "color": "blue" }<span class="pl-pds">'</span></span>

es POST <span class="pl-s"><span class="pl-pds">'</span>my_index/_refresh<span class="pl-pds">'</span></span>

<span class="pl-c1">echo</span>
<span class="pl-c1">echo</span> <span class="pl-s"><span class="pl-pds">"</span>---<span class="pl-pds">"</span></span>

es GET <span class="pl-s"><span class="pl-pds">'</span>my_index/my_child/_search?search_type=count&amp;format=yaml<span class="pl-pds">'</span></span> <span class="pl-s"><span class="pl-pds">'</span>{</span>
<span class="pl-s">  "query": {</span>
<span class="pl-s">    "terms": {</span>
<span class="pl-s">      "_parent": [ "P1", "P2" ]</span>
<span class="pl-s">    }</span>
<span class="pl-s">  },</span>
<span class="pl-s">  "aggs": {</span>
<span class="pl-s">    "parents": {</span>
<span class="pl-s">      "terms": { "field": "_parent" },</span>
<span class="pl-s">      "aggs": {</span>
<span class="pl-s">        "colors": {</span>
<span class="pl-s">          "terms": { "field": "color" }</span>
<span class="pl-s">        }</span>
<span class="pl-s">      }</span>
<span class="pl-s">    }</span>
<span class="pl-s">  }</span>
<span class="pl-s">}<span class="pl-pds">'</span></span></pre></div>
<hr>
<p dir="auto">For completeness, here is the expected output of the final query (in YAML format):</p>
<div class="highlight highlight-source-yaml notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="
---
took: 2
timed_out: false
_shards:
  total: 5
  successful: 5
  failed: 0
hits:
  total: 5
  max_score: 0.0
  hits: []
aggregations:
  parents:
    doc_count_error_upper_bound: 0
    sum_other_doc_count: 0
    buckets:
    - key: &quot;P1&quot;
      doc_count: 3
      colors:
        doc_count_error_upper_bound: 0
        sum_other_doc_count: 0
        buckets:
        - key: &quot;yellow&quot;
          doc_count: 2
        - key: &quot;red&quot;
          doc_count: 1
    - key: &quot;P2&quot;
      doc_count: 2
      colors:
        doc_count_error_upper_bound: 0
        sum_other_doc_count: 0
        buckets:
        - key: &quot;green&quot;
          doc_count: 2"><pre class="notranslate">---
<span class="pl-ent">took</span>: <span class="pl-c1">2</span>
<span class="pl-ent">timed_out</span>: <span class="pl-c1">false</span>
<span class="pl-ent">_shards</span>:
  <span class="pl-ent">total</span>: <span class="pl-c1">5</span>
  <span class="pl-ent">successful</span>: <span class="pl-c1">5</span>
  <span class="pl-ent">failed</span>: <span class="pl-c1">0</span>
<span class="pl-ent">hits</span>:
  <span class="pl-ent">total</span>: <span class="pl-c1">5</span>
  <span class="pl-ent">max_score</span>: <span class="pl-c1">0.0</span>
  <span class="pl-ent">hits</span>: <span class="pl-s">[]</span>
<span class="pl-ent">aggregations</span>:
  <span class="pl-ent">parents</span>:
    <span class="pl-ent">doc_count_error_upper_bound</span>: <span class="pl-c1">0</span>
    <span class="pl-ent">sum_other_doc_count</span>: <span class="pl-c1">0</span>
    <span class="pl-ent">buckets</span>:
    - <span class="pl-ent">key</span>: <span class="pl-s"><span class="pl-pds">"</span>P1<span class="pl-pds">"</span></span>
      <span class="pl-ent">doc_count</span>: <span class="pl-c1">3</span>
      <span class="pl-ent">colors</span>:
        <span class="pl-ent">doc_count_error_upper_bound</span>: <span class="pl-c1">0</span>
        <span class="pl-ent">sum_other_doc_count</span>: <span class="pl-c1">0</span>
        <span class="pl-ent">buckets</span>:
        - <span class="pl-ent">key</span>: <span class="pl-s"><span class="pl-pds">"</span>yellow<span class="pl-pds">"</span></span>
          <span class="pl-ent">doc_count</span>: <span class="pl-c1">2</span>
        - <span class="pl-ent">key</span>: <span class="pl-s"><span class="pl-pds">"</span>red<span class="pl-pds">"</span></span>
          <span class="pl-ent">doc_count</span>: <span class="pl-c1">1</span>
    - <span class="pl-ent">key</span>: <span class="pl-s"><span class="pl-pds">"</span>P2<span class="pl-pds">"</span></span>
      <span class="pl-ent">doc_count</span>: <span class="pl-c1">2</span>
      <span class="pl-ent">colors</span>:
        <span class="pl-ent">doc_count_error_upper_bound</span>: <span class="pl-c1">0</span>
        <span class="pl-ent">sum_other_doc_count</span>: <span class="pl-c1">0</span>
        <span class="pl-ent">buckets</span>:
        - <span class="pl-ent">key</span>: <span class="pl-s"><span class="pl-pds">"</span>green<span class="pl-pds">"</span></span>
          <span class="pl-ent">doc_count</span>: <span class="pl-c1">2</span></pre></div>
<hr>
<p dir="auto">And here is the bogus output of the final query (with <code class="notranslate">WITH_GRANDCHILD=true</code> set):</p>
<div class="highlight highlight-source-yaml notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="
---
took: 2
timed_out: false
_shards:
  total: 5
  successful: 5
  failed: 0
hits:
  total: 5
  max_score: 0.0
  hits: []
aggregations:
  parents:
    doc_count_error_upper_bound: 0
    sum_other_doc_count: 0
    buckets:
    - key: &quot;P1&quot;
      doc_count: 3
      colors:
        doc_count_error_upper_bound: 0
        sum_other_doc_count: 0
        buckets:
        - key: &quot;yellow&quot;
          doc_count: 2
        - key: &quot;red&quot;
          doc_count: 1
    - key: &quot;P2&quot;
      doc_count: 2
      colors:
        doc_count_error_upper_bound: 0
        sum_other_doc_count: 0
        buckets:
        - key: &quot;green&quot;
          doc_count: 2
    - key: &quot;P1_C1&quot;
      doc_count: 1
      colors:
        doc_count_error_upper_bound: 0
        sum_other_doc_count: 0
        buckets:
        - key: &quot;red&quot;
          doc_count: 1
    - key: &quot;P1_C2&quot;
      doc_count: 1
      colors:
        doc_count_error_upper_bound: 0
        sum_other_doc_count: 0
        buckets:
        - key: &quot;yellow&quot;
          doc_count: 1
    - key: &quot;P1_C3&quot;
      doc_count: 1
      colors:
        doc_count_error_upper_bound: 0
        sum_other_doc_count: 0
        buckets:
        - key: &quot;yellow&quot;
          doc_count: 1
    - key: &quot;P2_C1&quot;
      doc_count: 1
      colors:
        doc_count_error_upper_bound: 0
        sum_other_doc_count: 0
        buckets:
        - key: &quot;green&quot;
          doc_count: 1
    - key: &quot;P2_C2&quot;
      doc_count: 1
      colors:
        doc_count_error_upper_bound: 0
        sum_other_doc_count: 0
        buckets:
        - key: &quot;green&quot;
          doc_count: 1"><pre class="notranslate">---
<span class="pl-ent">took</span>: <span class="pl-c1">2</span>
<span class="pl-ent">timed_out</span>: <span class="pl-c1">false</span>
<span class="pl-ent">_shards</span>:
  <span class="pl-ent">total</span>: <span class="pl-c1">5</span>
  <span class="pl-ent">successful</span>: <span class="pl-c1">5</span>
  <span class="pl-ent">failed</span>: <span class="pl-c1">0</span>
<span class="pl-ent">hits</span>:
  <span class="pl-ent">total</span>: <span class="pl-c1">5</span>
  <span class="pl-ent">max_score</span>: <span class="pl-c1">0.0</span>
  <span class="pl-ent">hits</span>: <span class="pl-s">[]</span>
<span class="pl-ent">aggregations</span>:
  <span class="pl-ent">parents</span>:
    <span class="pl-ent">doc_count_error_upper_bound</span>: <span class="pl-c1">0</span>
    <span class="pl-ent">sum_other_doc_count</span>: <span class="pl-c1">0</span>
    <span class="pl-ent">buckets</span>:
    - <span class="pl-ent">key</span>: <span class="pl-s"><span class="pl-pds">"</span>P1<span class="pl-pds">"</span></span>
      <span class="pl-ent">doc_count</span>: <span class="pl-c1">3</span>
      <span class="pl-ent">colors</span>:
        <span class="pl-ent">doc_count_error_upper_bound</span>: <span class="pl-c1">0</span>
        <span class="pl-ent">sum_other_doc_count</span>: <span class="pl-c1">0</span>
        <span class="pl-ent">buckets</span>:
        - <span class="pl-ent">key</span>: <span class="pl-s"><span class="pl-pds">"</span>yellow<span class="pl-pds">"</span></span>
          <span class="pl-ent">doc_count</span>: <span class="pl-c1">2</span>
        - <span class="pl-ent">key</span>: <span class="pl-s"><span class="pl-pds">"</span>red<span class="pl-pds">"</span></span>
          <span class="pl-ent">doc_count</span>: <span class="pl-c1">1</span>
    - <span class="pl-ent">key</span>: <span class="pl-s"><span class="pl-pds">"</span>P2<span class="pl-pds">"</span></span>
      <span class="pl-ent">doc_count</span>: <span class="pl-c1">2</span>
      <span class="pl-ent">colors</span>:
        <span class="pl-ent">doc_count_error_upper_bound</span>: <span class="pl-c1">0</span>
        <span class="pl-ent">sum_other_doc_count</span>: <span class="pl-c1">0</span>
        <span class="pl-ent">buckets</span>:
        - <span class="pl-ent">key</span>: <span class="pl-s"><span class="pl-pds">"</span>green<span class="pl-pds">"</span></span>
          <span class="pl-ent">doc_count</span>: <span class="pl-c1">2</span>
    - <span class="pl-ent">key</span>: <span class="pl-s"><span class="pl-pds">"</span>P1_C1<span class="pl-pds">"</span></span>
      <span class="pl-ent">doc_count</span>: <span class="pl-c1">1</span>
      <span class="pl-ent">colors</span>:
        <span class="pl-ent">doc_count_error_upper_bound</span>: <span class="pl-c1">0</span>
        <span class="pl-ent">sum_other_doc_count</span>: <span class="pl-c1">0</span>
        <span class="pl-ent">buckets</span>:
        - <span class="pl-ent">key</span>: <span class="pl-s"><span class="pl-pds">"</span>red<span class="pl-pds">"</span></span>
          <span class="pl-ent">doc_count</span>: <span class="pl-c1">1</span>
    - <span class="pl-ent">key</span>: <span class="pl-s"><span class="pl-pds">"</span>P1_C2<span class="pl-pds">"</span></span>
      <span class="pl-ent">doc_count</span>: <span class="pl-c1">1</span>
      <span class="pl-ent">colors</span>:
        <span class="pl-ent">doc_count_error_upper_bound</span>: <span class="pl-c1">0</span>
        <span class="pl-ent">sum_other_doc_count</span>: <span class="pl-c1">0</span>
        <span class="pl-ent">buckets</span>:
        - <span class="pl-ent">key</span>: <span class="pl-s"><span class="pl-pds">"</span>yellow<span class="pl-pds">"</span></span>
          <span class="pl-ent">doc_count</span>: <span class="pl-c1">1</span>
    - <span class="pl-ent">key</span>: <span class="pl-s"><span class="pl-pds">"</span>P1_C3<span class="pl-pds">"</span></span>
      <span class="pl-ent">doc_count</span>: <span class="pl-c1">1</span>
      <span class="pl-ent">colors</span>:
        <span class="pl-ent">doc_count_error_upper_bound</span>: <span class="pl-c1">0</span>
        <span class="pl-ent">sum_other_doc_count</span>: <span class="pl-c1">0</span>
        <span class="pl-ent">buckets</span>:
        - <span class="pl-ent">key</span>: <span class="pl-s"><span class="pl-pds">"</span>yellow<span class="pl-pds">"</span></span>
          <span class="pl-ent">doc_count</span>: <span class="pl-c1">1</span>
    - <span class="pl-ent">key</span>: <span class="pl-s"><span class="pl-pds">"</span>P2_C1<span class="pl-pds">"</span></span>
      <span class="pl-ent">doc_count</span>: <span class="pl-c1">1</span>
      <span class="pl-ent">colors</span>:
        <span class="pl-ent">doc_count_error_upper_bound</span>: <span class="pl-c1">0</span>
        <span class="pl-ent">sum_other_doc_count</span>: <span class="pl-c1">0</span>
        <span class="pl-ent">buckets</span>:
        - <span class="pl-ent">key</span>: <span class="pl-s"><span class="pl-pds">"</span>green<span class="pl-pds">"</span></span>
          <span class="pl-ent">doc_count</span>: <span class="pl-c1">1</span>
    - <span class="pl-ent">key</span>: <span class="pl-s"><span class="pl-pds">"</span>P2_C2<span class="pl-pds">"</span></span>
      <span class="pl-ent">doc_count</span>: <span class="pl-c1">1</span>
      <span class="pl-ent">colors</span>:
        <span class="pl-ent">doc_count_error_upper_bound</span>: <span class="pl-c1">0</span>
        <span class="pl-ent">sum_other_doc_count</span>: <span class="pl-c1">0</span>
        <span class="pl-ent">buckets</span>:
        - <span class="pl-ent">key</span>: <span class="pl-s"><span class="pl-pds">"</span>green<span class="pl-pds">"</span></span>
          <span class="pl-ent">doc_count</span>: <span class="pl-c1">1</span></pre></div>
<p dir="auto">I would like to be able to check over the node API if a node has a certain plugin installed or retrieve the list of installed plugins.</p>
<p dir="auto">For more details see google groups post:</p>
<p dir="auto"><a href="https://groups.google.com/forum/#!topic/elasticsearch/QVRR9Q6VuG0" rel="nofollow">https://groups.google.com/forum/#!topic/elasticsearch/QVRR9Q6VuG0</a></p>