How to limit 5000 products in a collection, so that filter and sort work for the collection?

i have some some collections which has more than 5000 products
as filter and sorting not work if the collection contains more than 5000 products
so i want to limit the products to first 5000 and make it work sort and filter.
here is the code

{% capture Product_grid %}
    {%- paginate collection.products  by product_per_page -%}
        {% render 'collection-products',
            product_image_srcsize_desktop: product_image_srcsize_desktop,
            product_image_srcsize_mobile: product_image_srcsize_mobile,
            products: collection.products,
            fallback_msg: No_products_msg,
            columns: product_columns,
            mobile_columns: product_columns_mobile,
            cards_view: section.settings.cards_view
        %}
        {% if section.settings.pagination_type == 'default' %}
            {% render 'collection-pagination', paginate: paginate, anchor: '' %}
        {% else %}
            {% render 'collection-dynamic-pagination',
                paginate: paginate,
                type: section.settings.pagination_type,
                form_id: form_id
            %}
        {% endif %}
    {%- endpaginate -%}
{% endcapture %}

please help

You can’t, if the collection has more than 5000, no filters will be available. You’ll need to split your collections up.