Return an collection of unique values by id

Hello everyone.
This is my question.
I have search functionality on my online shop.
When users search some products, I'd like to collect all available characteristics values of search results and give my users opportunity to filter search results with this values. For example:

User search 't-shirt', then elastic return an array of products:

[
    {
        product_id: 1,
        product_name: 'Some title',
        product_characteristics: [
            {
                char_id: 1,
                char_name: 'Color',
                char_value: 'Blue'
            },
            {
                char_id: 9,
                char_name: 'Size',
                char_value: 'L'
            },
            {
                char_id: 8,
                char_name: 'Material',
                char_value: 'Lycra'
            }
        ]
    },
    {
        product_id: 5,
        product_name: 'Some title',
        product_characteristics: [
            {
                char_id: 1,
                char_name: 'Color',
                char_value: 'Yellow'
            },
            {
                char_id: 9,
                char_name: 'Size',
                char_value: 'XL'
            }
        ]
    }
]

Except returning search results I'd like to returning unique values of each unique characteristic of this products. Something like this:

{
    1: ['Blue', 'Yellow'],
    8: ['Lycra'],
    9: ['L', 'XL']
}

Then user can filter search results by this filters to choose only blue t-shirts.
I want to get it from elastic, because sometimes search results containes thousands of products and I use elastic pagination to save page loading time, so I don't get all search results on my frontend.

Could anyone give me suggetions?
Thank you!

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