Get Users With More Than One Account in the Same Source

UPDATE: The previous query may no longer work as the data model of identity search results has changed. It appears the _uid field is no longer available, having been replaced by _id. If you are experiencing issues using the above query, then try this query instead:

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

This produces the following results:

{
    "aggregations": {
        "accounts": {
            "doc_count": 782,
            "source_id": {
                "doc_count_error_upper_bound": 0,
                "sum_other_doc_count": 0,
                "buckets":
                    {
                        "key": "2c91808780f6c51601812ae95ccb3abd",
                        "doc_count": 4,
                        "identities": {
                            "doc_count_error_upper_bound": 0,
                            "sum_other_doc_count": 0,
                            "buckets": []
                        }
                    },
                    {
                        "key": "2c9180887671ff8c01767b4671fb7d5e",
                        "doc_count": 3,
                        "identities": {
                            "doc_count_error_upper_bound": 0,
                            "sum_other_doc_count": 0,
                            "buckets": [
                                {
                                    "key": "2c9180867b75ce33017b78198e8c0a06",
                                    "doc_count": 2,
                                    "accounts": {
                                        "hits": {
                                            "total": {
                                                "value": 2,
                                                "relation": "eq"
                                            },
                                            "max_score": 1,
                                            "hits": [
                                                {
                                                    "_index": "identities_v35_stg03_group1of2",
                                                    "_type": "_doc",
                                                    "_id": "2c9180867b75ce33017b78198e8c0a06",
                                                    "_nested": {
                                                        "field": "accounts",
                                                        "offset": 0
                                                    },
                                                    "_score": 1,
                                                    "_source": {
                                                        "id": "2c918083814931bf01816cd42009481c",
                                                        "name": "adam.archer",
                                                        "accountId": "E010",
                                                        "source": {
                                                            "id": "2c9180887671ff8c01767b4671fb7d5e",
                                                            "name": "Employees",
                                                            "type": "DelimitedFile"
                                                        },
                                                        "disabled": false,
                                                        "locked": false,
                                                        "privileged": false,
                                                        "manuallyCorrelated": false,
                                                        "entitlementAttributes": {
                                                            "groups": [
                                                                "DevRel"
                                                            ]
                                                        },
                                                        "created": "2022-06-16T14:04:10.121Z"
                                                    }
                                                },
                                                {
                                                    "_index": "identities_v35_stg03_group1of2",
                                                    "_type": "_doc",
                                                    "_id": "2c9180867b75ce33017b78198e8c0a06",
                                                    "_nested": {
                                                        "field": "accounts",
                                                        "offset": 1
                                                    },
                                                    "_score": 1,
                                                    "_source": {
                                                        "id": "2c918083814931bf01816cd4211a481f",
                                                        "name": "adam.archer",
                                                        "accountId": "E008",
                                                        "source": {
                                                            "id": "2c9180887671ff8c01767b4671fb7d5e",
                                                            "name": "Employees",
                                                            "type": "DelimitedFile"
                                                        },
                                                        "disabled": false,
                                                        "locked": false,
                                                        "privileged": false,
                                                        "manuallyCorrelated": false,
                                                        "entitlementAttributes": {
                                                            "groups": [
                                                                "DevRel"
                                                            ]
                                                        },
                                                        "created": "2022-06-16T14:04:10.394Z"
                                                    }
                                                }
                                            ]
                                        }
                                    }
                                }
                            ]
                        }
                    },
                    {
                        "key": "2c91808a7b5adbb6017b63c91b2d0f4f",
                        "doc_count": 3,
                        "identities": {
                            "doc_count_error_upper_bound": 0,
                            "sum_other_doc_count": 0,
                            "buckets": []
                        }
                    }
                ]
            }
        }
    },
    "hits": [
        ...
    ]
}
2 Likes