<p dir="auto">My domain is the following: a parent doc has some child docs with some string field. I want to calculate a frequency of parents having particular field in the child doc. When this particular field doesn't have duplicates amond same parent - results of query below are OK. But when some parent has multiple children with same value of the field, calculation sums this duplicates and gives wrong results. When I perform equal search query it gives me right count of parent docs.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="    curl -XPUT 127.0.0.1:9200/test_index -d '
    {
      &quot;mappings&quot;: {
        &quot;test_parent&quot;: {
            &quot;properties&quot;: {
                &quot;test_filter_field&quot;: {
                    &quot;type&quot;: &quot;string&quot;
                }
            }
        },
        &quot;test_child&quot;: {
          &quot;_parent&quot;: {
            &quot;type&quot;: &quot;test_parent&quot;
          },
          &quot;properties&quot;: {
              &quot;test_non_nested_field&quot;: {
                  &quot;type&quot;: &quot;string&quot;
              },
              &quot;test_nested_field&quot;: {
                  &quot;type&quot;: &quot;nested&quot;,
                  &quot;properties&quot;: {
                    &quot;nested_filter_field&quot;: {
                        &quot;type&quot;: &quot;string&quot;
                    },
                    &quot;nested_term_field&quot;: {
                        &quot;type&quot;: &quot;string&quot;
                    }
                  }
              }
          }
        }
      }
    }'

    curl -XPUT 127.0.0.1:9200/test_index/test_parent/parent1 -d '
    {
        &quot;test_filter_field&quot; : &quot;1&quot;
    }'

    curl -XPUT 127.0.0.1:9200/test_index/test_child/child4?parent=parent1 -d '
    {
        &quot;test_non_nested_field&quot;: &quot;non_nested_term_field11&quot;
    }'

    curl -XPUT 127.0.0.1:9200/test_index/test_child/child5?parent=parent1 -d '
    {
        &quot;test_non_nested_field&quot;: &quot;non_nested_term_field11&quot;
    }'

    curl -XPOST 127.0.0.1:9200/test_index/test_parent/_search?pretty=true -d '
    {
      &quot;aggs&quot;: {
        &quot;top&quot;: {
          &quot;filter&quot;: {
            &quot;term&quot;: {
              &quot;test_filter_field&quot;: &quot;1&quot;
            }
          },
          &quot;aggs&quot;: {
            &quot;children.nested.filter&quot;: {
              &quot;children&quot;: {
                &quot;type&quot;: &quot;test_child&quot;
              },
              &quot;aggs&quot;: {
                &quot;field&quot;: {
                  &quot;terms&quot;: {
                    &quot;field&quot;: &quot;test_child.test_non_nested_field&quot;
                  }
                }
              }
            }
          }
        }
      }
    }
    '"><pre class="notranslate"><code class="notranslate">    curl -XPUT 127.0.0.1:9200/test_index -d '
    {
      "mappings": {
        "test_parent": {
            "properties": {
                "test_filter_field": {
                    "type": "string"
                }
            }
        },
        "test_child": {
          "_parent": {
            "type": "test_parent"
          },
          "properties": {
              "test_non_nested_field": {
                  "type": "string"
              },
              "test_nested_field": {
                  "type": "nested",
                  "properties": {
                    "nested_filter_field": {
                        "type": "string"
                    },
                    "nested_term_field": {
                        "type": "string"
                    }
                  }
              }
          }
        }
      }
    }'

    curl -XPUT 127.0.0.1:9200/test_index/test_parent/parent1 -d '
    {
        "test_filter_field" : "1"
    }'

    curl -XPUT 127.0.0.1:9200/test_index/test_child/child4?parent=parent1 -d '
    {
        "test_non_nested_field": "non_nested_term_field11"
    }'

    curl -XPUT 127.0.0.1:9200/test_index/test_child/child5?parent=parent1 -d '
    {
        "test_non_nested_field": "non_nested_term_field11"
    }'

    curl -XPOST 127.0.0.1:9200/test_index/test_parent/_search?pretty=true -d '
    {
      "aggs": {
        "top": {
          "filter": {
            "term": {
              "test_filter_field": "1"
            }
          },
          "aggs": {
            "children.nested.filter": {
              "children": {
                "type": "test_child"
              },
              "aggs": {
                "field": {
                  "terms": {
                    "field": "test_child.test_non_nested_field"
                  }
                }
              }
            }
          }
        }
      }
    }
    '
</code></pre></div>
<p dir="auto">It gives me</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="    &quot;aggregations&quot; : {
    &quot;top&quot; : {
      &quot;doc_count&quot; : 1,
      &quot;children.nested.filter&quot; : {
        &quot;doc_count&quot; : 2,
        &quot;field&quot; : {
          &quot;doc_count_error_upper_bound&quot; : 0,
          &quot;sum_other_doc_count&quot; : 0,
          &quot;buckets&quot; : [ {
            &quot;key&quot; : &quot;non_nested_term_field11&quot;,
            &quot;doc_count&quot; : 2  &lt;------ I expect it here to give me 1
          } ]
        }
      }
    }
    }"><pre class="notranslate"><code class="notranslate">    "aggregations" : {
    "top" : {
      "doc_count" : 1,
      "children.nested.filter" : {
        "doc_count" : 2,
        "field" : {
          "doc_count_error_upper_bound" : 0,
          "sum_other_doc_count" : 0,
          "buckets" : [ {
            "key" : "non_nested_term_field11",
            "doc_count" : 2  &lt;------ I expect it here to give me 1
          } ]
        }
      }
    }
    }
</code></pre></div>
<p dir="auto">Here is equal search query, which gives correct result</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="    curl -XPOST 127.0.0.1:9200/test_index/test_parent/_search?pretty=true -d '
    {
        &quot;query&quot;: {
            &quot;has_child&quot;: {
                &quot;query&quot;: {
                    &quot;multi_match&quot;: {
                        &quot;query&quot;: &quot;non_nested_term_field11&quot;,
                        &quot;fields&quot;: [
                            &quot;test_non_nested_field&quot;
                        ]
                    }
                },
                &quot;type&quot;: &quot;test_child&quot;
            }
        }
    }"><pre class="notranslate"><code class="notranslate">    curl -XPOST 127.0.0.1:9200/test_index/test_parent/_search?pretty=true -d '
    {
        "query": {
            "has_child": {
                "query": {
                    "multi_match": {
                        "query": "non_nested_term_field11",
                        "fields": [
                            "test_non_nested_field"
                        ]
                    }
                },
                "type": "test_child"
            }
        }
    }
</code></pre></div>
<p dir="auto">response</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="    {

      &quot;hits&quot; : {
        &quot;total&quot; : 1,  &lt;--- obviously correct
        &quot;max_score&quot; : 1.0,
        &quot;hits&quot; : [
            {
              &quot;_index&quot; : &quot;test_index&quot;,
              &quot;_type&quot; : &quot;test_parent&quot;,
              &quot;_id&quot; : &quot;parent1&quot;,
              &quot;_score&quot; : 1.0,
              &quot;_source&quot;: {
                    &quot;test_filter_field&quot; : &quot;1&quot;
              }
            }
        ]
      }
    }"><pre class="notranslate"><code class="notranslate">    {

      "hits" : {
        "total" : 1,  &lt;--- obviously correct
        "max_score" : 1.0,
        "hits" : [
            {
              "_index" : "test_index",
              "_type" : "test_parent",
              "_id" : "parent1",
              "_score" : 1.0,
              "_source": {
                    "test_filter_field" : "1"
              }
            }
        ]
      }
    }
</code></pre></div>
<p dir="auto">Hi, we're using the Java client API as an embedded node.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="final Settings settings = Settings.settingsBuilder()
                                          .put(&quot;plugin.types&quot;, DeleteByQueryPlugin.class.getName())
                                          .loadFromPath(findConfigFile())
                                          .build();
        this.node = nodeBuilder().settings(settings).node();
        this.node.start();
        this.client = node.client();"><pre class="notranslate"><code class="notranslate">final Settings settings = Settings.settingsBuilder()
                                          .put("plugin.types", DeleteByQueryPlugin.class.getName())
                                          .loadFromPath(findConfigFile())
                                          .build();
        this.node = nodeBuilder().settings(settings).node();
        this.node.start();
        this.client = node.client();
</code></pre></div>
<p dir="auto">We notice that the plugin is never registered.  Is there a way to register the plugin?</p>