Issue 1:
I include a bash script that recreates the situation.

#!/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"
}
}
}'

Issue 2:
Feature request:

Aggregations that can count only parent documents with matching children documents.

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.

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.

We considered using the msearch query to get these results in a single query and we ended up with something similar to this: https://gist.github.com/chaos-generator/9133118

The sample above runs in 40 seconds give or take.

And along came elastic search 1.0.0 and now we have aggregations, so we simplified our query to this: https://gist.github.com/chaos-generator/9133139

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.

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.

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: https://gist.github.com/chaos-generator/9133321 , but it didn't work as we wanted too:

You can use this gist to simulate the issue we have: https://gist.github.com/chaos-generator/9143655