<p dir="auto">I include a bash script that recreates the situation.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="#!/bin/sh

curl -XDELETE &quot;http://localhost:9200/test&quot;
curl -XPUT &quot;http://localhost:9200/test&quot;

echo

curl -XPUT &quot;http://localhost:9200/test/foo/_mapping&quot; -d '{
    &quot;foo&quot; : { 
        &quot;properties&quot; : {
            &quot;id&quot;: {
                &quot;type&quot; : &quot;multi_field&quot;,
                &quot;path&quot;: &quot;full&quot;,
                &quot;fields&quot; : {
                    &quot;foo_id_in_another_field&quot; : {&quot;type&quot; : &quot;long&quot;, include_in_all:false },
                &quot;id&quot; : {&quot;type&quot; : &quot;long&quot;}
                   }
        }
    }
    }
}'

echo

#foo is a basically a duplicate of the foo document to support search use cases
curl -XPUT &quot;http://localhost:9200/test/bar/_mapping&quot; -d '{
    &quot;bar&quot; : {
        &quot;properties&quot; : {
            &quot;id&quot;: {
                &quot;type&quot; : &quot;multi_field&quot;,
                &quot;path&quot;: &quot;full&quot;,
                &quot;fields&quot; : {
                    &quot;bar_id_in_another_field&quot; : {&quot;type&quot; : &quot;long&quot;, include_in_all:false },
                    &quot;id&quot; : {&quot;type&quot; : &quot;long&quot;}
                   }
                },
        &quot;foo&quot;: {
            &quot;properties&quot;: {
                &quot;id&quot;: {
                    &quot;type&quot; : &quot;multi_field&quot;,
                    &quot;path&quot;: &quot;full&quot;,
                    &quot;fields&quot; : {
                        &quot;foo_id_in_another_field&quot; : {&quot;type&quot; : &quot;long&quot;, include_in_all:false },
                        &quot;id&quot; : {&quot;type&quot; : &quot;long&quot;}
                    }
                }
            }
        }
        }
    }
}'

echo

curl -XPUT &quot;http://localhost:9200/test/foo/1?refresh=true&quot; -d '{
    &quot;foo&quot;: {
                &quot;id&quot;: 1
    }
}'

echo

#failure case appears even when not including the following JSON
# &quot;bar&quot;: {
#   &quot;id&quot;: 2,
#   &quot;foo&quot;: {
#     &quot;id&quot;: 3
#   }
# }
curl -XPUT &quot;http://localhost:9200/test/bar/2?refresh=true&quot; -d '{
    &quot;bar&quot;: {
        &quot;id&quot;: 2
    }
}'

echo

#expect two results, get one (FAIL)
curl -XPOST &quot;http://localhost:9200/test/foo,bar/_search?pretty=true&quot; -d '{
  &quot;size&quot;: 10,
  &quot;query&quot;: {
    &quot;query_string&quot;: {
      &quot;query&quot;: &quot;foo.id:1 OR bar.id:2&quot;
    }
  }
}'

echo

#except one result, get one (PASS)
curl -XPOST &quot;http://localhost:9200/test/bar/_search?pretty=true&quot; -d '{
  &quot;size&quot;: 10,
  &quot;query&quot;: {
    &quot;query_string&quot;: {
      &quot;query&quot;: &quot;foo.id:1 OR bar.id:2&quot;
    }
  }
}'

echo

#expect one result, get one result (PASS)
curl -XPOST &quot;http://localhost:9200/test/foo/_search?pretty=true&quot; -d '{
  &quot;size&quot;: 10,
  &quot;query&quot;: {
    &quot;query_string&quot;: {
      &quot;query&quot;: &quot;foo.id:1 OR bar.id:2&quot;
    }
  }
}'

echo

#expect two results, get tow results (PASS)
curl -XPOST &quot;http://localhost:9200/test/_search?pretty=true&quot; -d '{
  &quot;size&quot;: 10,
  &quot;query&quot;: {
    &quot;query_string&quot;: {
      &quot;query&quot;: &quot;foo.id:1 OR bar.id:2&quot;
    }
  }
}'"><pre class="notranslate"><code class="notranslate">#!/bin/sh

curl -XDELETE "http://localhost:9200/test"
curl -XPUT "http://localhost:9200/test"

echo

curl -XPUT "http://localhost:9200/test/foo/_mapping" -d '{
    "foo" : { 
        "properties" : {
            "id": {
                "type" : "multi_field",
                "path": "full",
                "fields" : {
                    "foo_id_in_another_field" : {"type" : "long", include_in_all:false },
                "id" : {"type" : "long"}
                   }
        }
    }
    }
}'

echo

#foo is a basically a duplicate of the foo document to support search use cases
curl -XPUT "http://localhost:9200/test/bar/_mapping" -d '{
    "bar" : {
        "properties" : {
            "id": {
                "type" : "multi_field",
                "path": "full",
                "fields" : {
                    "bar_id_in_another_field" : {"type" : "long", include_in_all:false },
                    "id" : {"type" : "long"}
                   }
                },
        "foo": {
            "properties": {
                "id": {
                    "type" : "multi_field",
                    "path": "full",
                    "fields" : {
                        "foo_id_in_another_field" : {"type" : "long", include_in_all:false },
                        "id" : {"type" : "long"}
                    }
                }
            }
        }
        }
    }
}'

echo

curl -XPUT "http://localhost:9200/test/foo/1?refresh=true" -d '{
    "foo": {
                "id": 1
    }
}'

echo

#failure case appears even when not including the following JSON
# "bar": {
#   "id": 2,
#   "foo": {
#     "id": 3
#   }
# }
curl -XPUT "http://localhost:9200/test/bar/2?refresh=true" -d '{
    "bar": {
        "id": 2
    }
}'

echo

#expect two results, get one (FAIL)
curl -XPOST "http://localhost:9200/test/foo,bar/_search?pretty=true" -d '{
  "size": 10,
  "query": {
    "query_string": {
      "query": "foo.id:1 OR bar.id:2"
    }
  }
}'

echo

#except one result, get one (PASS)
curl -XPOST "http://localhost:9200/test/bar/_search?pretty=true" -d '{
  "size": 10,
  "query": {
    "query_string": {
      "query": "foo.id:1 OR bar.id:2"
    }
  }
}'

echo

#expect one result, get one result (PASS)
curl -XPOST "http://localhost:9200/test/foo/_search?pretty=true" -d '{
  "size": 10,
  "query": {
    "query_string": {
      "query": "foo.id:1 OR bar.id:2"
    }
  }
}'

echo

#expect two results, get tow results (PASS)
curl -XPOST "http://localhost:9200/test/_search?pretty=true" -d '{
  "size": 10,
  "query": {
    "query_string": {
      "query": "foo.id:1 OR bar.id:2"
    }
  }
}'
</code></pre></div>
<p dir="auto">Feature request:<br>
Aggregations that can count only parent documents with matching children documents.</p>
<p dir="auto">I've been working on a BI system with ES 0.90 and we needed count "users" which have certain attributes, for instance let's say gender and star sign. A user is a parent-level document and the attributes are child documents.</p>
<p dir="auto">From the sample above, we were doing so by creating a query for each combination of male / female and the star signs and querying individually, as one can imagine, this was slow, but the results are exactly what we want. We could run this in roughly 2 minutes.</p>
<p dir="auto">We considered using the msearch query to get these results in a single query and we ended up with something similar to this: <a href="https://gist.github.com/chaos-generator/9133118">https://gist.github.com/chaos-generator/9133118</a><br>
The sample above runs in 40 seconds give or take.</p>
<p dir="auto">And along came elastic search 1.0.0 and now we have aggregations, so we simplified our query to this: <a href="https://gist.github.com/chaos-generator/9133139">https://gist.github.com/chaos-generator/9133139</a><br>
This runs lightning fast and we get the results in 200ms on average, which is ideal for us, BUT we get the total number of documents with the attributes, rather than the count on the parent documents.</p>
<p dir="auto">Our problem, as you can see in the msearch gist, is that we have a parent level document and child documents, which would only be updated if another document with the exact same attributes came in, this means that a parent level user document can have three child documents that will have gender and star sign, but I only want to count the parent document, rather than each individual child document.</p>
<p dir="auto">As we don't know in advance the attributes our users will be searching, we cannot use a script in index time to help us do this aggregation. We tried to use a script in search time like this:  <a href="https://gist.github.com/chaos-generator/9133321">https://gist.github.com/chaos-generator/9133321</a> , but it didn't work as we wanted too:</p>
<p dir="auto">You can use this gist to simulate the issue we have: <a href="https://gist.github.com/chaos-generator/9143655">https://gist.github.com/chaos-generator/9143655</a></p>