Skip to content

magento 2.4.5-p8 “The bucket doesn’t exist” for custom filter

I add new filter using plugin for MagentoFrameworkSearchRequestConfigFilesystemReader

Now sometimes I have error “The bucket doesn’t exist.” for added bucket. After cache flush it works correct.

Due to caching – most of the time the filters are not displayed at all because there was an error when _getItemsData() for the new filter. The worst thing is that I can’t reproduce it locally. Maybe someone has an idea why the error may occure periodically?

public function afterRead(
    MagentoFrameworkConfigReaderInterface $subject,
    array $result,
    $scope = null
) {
    $aggregations = [
        'vendor_stock_bucket' => [
            'name' => 'vendor_stock_bucket',
            'field' => 'vendor_stock_status',
            'type' => 'termBucket',
            'metrics' => [
                [
                    'type' => 'count'
                ]
            ]
        ]
    ];
    $vendor_stock_status_query = [
        'name' => 'vendor_stock_status_query',
        'type' => 'filteredQuery',
        'filterReference' => [
            [
                'clause' => 'must',
                'ref' => 'vendor_stock_status_filter'
            ]
        ]
    ];
    $filters = [
        'vendor_stock_status_filter' => [
            'type' => 'termFilter',
            'name' => 'vendor_stock_status_filter',
            'field' => 'vendor_stock_status',
            'value' => '$vendor_stock_status$'
        ]
    ];
    $queryReference = [
        [
            'clause' => 'must',
            'ref' => 'vendor_stock_status_query'
        ]
    ];

    $result = array_merge_recursive($result, [
        'catalog_view_container' => [
            'queries' => [
                'catalog_view_container' => [
                    'queryReference' => $queryReference
                ],
                'vendor_stock_status_query' => $vendor_stock_status_query
            ],
            'filters' => $filters
        ]
    ]);

    $result = array_merge_recursive($result, [
        'catalog_view_container' => [
            'aggregations' => $aggregations
        ]
    ]);

    $result = array_merge_recursive($result, [
        'quick_search_container' => [
            'queries' => [
                'quick_search_container' => [
                    'queryReference' => $queryReference
                ],
                'vendor_stock_status_query' => $vendor_stock_status_query 
            ],
            'filters' => $filters
        ]
    ]);

    $result = array_merge_recursive($result, [
        'quick_search_container' => [
            'aggregations' => $aggregations
        ]
    ]);
    
    return $result;
}