# How to group unique statistics records with overlapping fields (account and fingerprint)?

**URL:** <https://discuss.elastic.co/t/how-to-group-unique-statistics-records-with-overlapping-fields-account-and-fingerprint/243667>\
**Category:** Elasticsearch\
**Created:** [August 4, 2020, 8:01am UTC](https://discuss.elastic.co/t/how-to-group-unique-statistics-records-with-overlapping-fields-account-and-fingerprint/243667 "2020-08-04T08:01:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ange007](https://avatars.discourse-cdn.com/v4/letter/a/b3f665/32.png) [@ange007](https://discuss.elastic.co/u/ange007)\
**Post date:** [August 4, 2020, 8:01am UTC](https://discuss.elastic.co/t/how-to-group-unique-statistics-records-with-overlapping-fields-account-and-fingerprint/243667/1 "2020-08-04T08:01:45Z")

</div>

Hi.

There is an index with fields:

- account\_id
- fingerprint
- etc.

Each record may or may not have an **account\_id** , the same is with **fingerprint**. It is necessary to group them by **account\_id** along the way adding to these accounts - records with a **fingerprint** that are present in records from a specific **account\_id**.

The essence of the task is to display a list of unique statistics records for known accounts _(those who have an account\_id + those who do not have it, but have the same fingerprint as some of the records in this group)_ and unknown _(those who only have a fingerprint)_ with pagination.

I try this:

```auto
            'aggs' => [
            "items" => [
                "composite" => [
                    "sources" => [ 
                        [
                            'account_id' => [
                                'terms' => ['field' => 'account_id'],
                            ],
                        ], [
                            'fingerprint' => [
                                'terms' => ['field' => 'fingerprint'],
                            ],
                        ]
                    ],
                ],
                "aggs" => [
                    "hits" => [
                        "top_hits" => [
                            "size" => 100
                        ]
                    ]
                ]

            ],
            
        ]

```

And something like this _(here I know that the code is not entirely correct, I just found it in history)_ :

```auto
'collapse' => [
            'field' => 'account_id',
            'inner_hits' => [ [
                'name' => 'accounts',
                'size' => 1,
                'sort' => [[ 'timestamp' => 'desc'] ]
            ],
            [
                'name' => 'fingerprints',
                'size' => 1,
                'sort' => [[ 'timestamp' => 'desc'] ]
            ] ],
            "max_concurrent_group_searches" => 4 
        ],

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 1, 2020, 8:01am UTC](https://discuss.elastic.co/t/how-to-group-unique-statistics-records-with-overlapping-fields-account-and-fingerprint/243667/2 "2020-09-01T08:01:53Z")

</div>

Closed on September 1, 2020, 8:01am UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
