Retrieving Users With More Than One Account In Same Source

Hi,

I am trying to retrieve identities with more than one account in the same source using the Search Query Aggregation API. I have followed the discussions on similar posts, however, the output I get after making the API call includes accounts from other sources which were not explicitly stated in the search aggregation query. Secondly, the output also includes several accounts that are not duplicates in any source. How can I ensure the search query aggregation API call, returns accounts from only one source per API call, and also limit the output to return duplicate accounts. Below are the body of the queries I have tried.

Query 1

{
  "query": {
    "query": "*"
  },
  "indices": [
    "identities"
  ],
  "aggregationsDsl": {
    "accounts": {
      "nested": {
        "path": "accounts"
      },
      "aggs": {
        "source_name": {
          "terms": {
            "field": "accounts.source.name.exact",
            "include": ["Exact Source Name"],
            "min_doc_count": 2,
            "size": 1000
          },
          "aggs": {
            "identities": {
              "terms": {
                "field": "_id",
                "min_doc_count": 2,
                "size": 1000
              },
              "aggs": {
                "accounts": {
                  "top_hits": {
                    "size": 10
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Query 2

{
  "query": {
    "query": "*"
  },
  "indices": [
    "identities"
  ],
  "aggregationsDsl": {
    "accounts": {
      "nested": {
        "path": "accounts"
      },
      "aggs": {
        "source_id": {
          "terms": {
            "field": "accounts.source.id",
            "include": ["Exact Source ID"],
            "min_doc_count": 2,
            "size": 1000
          },
          "aggs": {
            "identities": {
              "terms": {
                "field": "_id",
                "min_doc_count": 2,
                "size": 1000
              },
              "aggs": {
                "accounts": {
                  "top_hits": {
                    "size": 10
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}